Skip to content

Commit

Permalink
Catch bad filters (and test); allow for non-objects;
Browse files Browse the repository at this point in the history
Allow non-objects to add/update;
Add timeout for Firefox on test groups in server-add/server-update
  • Loading branch information
brettz9 committed Mar 27, 2016
1 parent 16c117a commit c1fb72a
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 28 deletions.
6 changes: 3 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
- Deprecated: on `schema.indexes`, in place of the index `key` property,
`keyPath` should be used.
- API addition: Add Server aliases, `put` and `delete`.
- API change: Allow ``{item:...}`` without `key` for sake of unambiguity
- API change: Allow add/update {item:...} to be of any value including
- API change: Allow `{item:...}` without `key` for sake of unambiguity
- API change: Allow `add`/`update` items to be of any value including
`undefined` or `null`
- API change: Allow Mongoifying of add/update/remove keys
- API change: Disallow key in `count()` if null;
Expand All @@ -27,7 +27,7 @@
- Fix: Ensure there is a promise rejection for a bad schema callback,
bad `IDBKeyRange`-related call, bad `createObjectStore`, bad index
creation, bad function on `modify()` object, bad `modify` result,
bad `map()` function, and bad `put()`.
bad `map()` function, bad `filter`, and bad `update()`.
- Fix: Ensure `limit()` arguments are numeric
- Fix: Actually implement documented chaining of (short) event handlers
- Fix: Allow empty string index in `Server.query()`.
Expand Down
37 changes: 26 additions & 11 deletions dist/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
var dbCache = {};
var serverEvents = ['abort', 'error', 'versionchange'];

function isObject(item) {
return item && (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object';
}

function mongoDBToKeyRangeArgs(opts) {
var keys = Object.keys(opts).sort();
if (keys.length === 1) {
Expand Down Expand Up @@ -149,15 +153,23 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
var matchFilter = true;
var result = 'value' in cursor ? cursor.value : cursor.key;

filters.forEach(function (filter) {
if (!filter || !filter.length) {
// Invalid filter do nothing
} else if (filter.length === 2) {
matchFilter = matchFilter && result[filter[0]] === filter[1];
} else {
matchFilter = matchFilter && filter[0](result);
}
});
try {
filters.forEach(function (filter) {
if (!filter || !filter.length) {
// Invalid filter do nothing
} else if (filter.length === 2) {
matchFilter = matchFilter && result[filter[0]] === filter[1];
} else {
matchFilter = matchFilter && filter[0](result);
}
});
} catch (err) {
// Could be filter on non-object or error in filter function
reject(err);
return {
v: void 0
};
}

if (matchFilter) {
counter++;
Expand Down Expand Up @@ -399,7 +411,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
records.some(function (record) {
var req = void 0,
key = void 0;
if (hasOwn.call(record, 'item')) {
if (isObject(record) && hasOwn.call(record, 'item')) {
key = record.key;
record = record.item;
if (key != null) {
Expand All @@ -425,6 +437,9 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
}

req.onsuccess = function (e) {
if (!isObject(record)) {
return;
}
var target = e.target;
var keyPath = target.source.keyPath;
if (keyPath === null) {
Expand Down Expand Up @@ -469,7 +484,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr

records.some(function (record) {
var key = void 0;
if (hasOwn.call(record, 'item')) {
if (isObject(record) && hasOwn.call(record, 'item')) {
key = record.key;
record = record.item;
if (key != null) {
Expand Down
2 changes: 1 addition & 1 deletion dist/db.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/db.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/db.min.js.map

Large diffs are not rendered by default.

34 changes: 23 additions & 11 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
const dbCache = {};
const serverEvents = ['abort', 'error', 'versionchange'];

function isObject (item) {
return item && typeof item === 'object';
}

function mongoDBToKeyRangeArgs (opts) {
const keys = Object.keys(opts).sort();
if (keys.length === 1) {
Expand Down Expand Up @@ -118,15 +122,20 @@
let matchFilter = true;
let result = 'value' in cursor ? cursor.value : cursor.key;

filters.forEach(function (filter) {
if (!filter || !filter.length) {
// Invalid filter do nothing
} else if (filter.length === 2) {
matchFilter = matchFilter && (result[filter[0]] === filter[1]);
} else {
matchFilter = matchFilter && filter[0](result);
}
});
try {
filters.forEach(function (filter) {
if (!filter || !filter.length) {
// Invalid filter do nothing
} else if (filter.length === 2) {
matchFilter = matchFilter && (result[filter[0]] === filter[1]);
} else {
matchFilter = matchFilter && filter[0](result);
}
});
} catch (err) { // Could be filter on non-object or error in filter function
reject(err);
return;
}

if (matchFilter) {
counter++;
Expand Down Expand Up @@ -336,7 +345,7 @@
const store = transaction.objectStore(table);
records.some(function (record) {
let req, key;
if (hasOwn.call(record, 'item')) {
if (isObject(record) && hasOwn.call(record, 'item')) {
key = record.key;
record = record.item;
if (key != null) {
Expand All @@ -362,6 +371,9 @@
}

req.onsuccess = function (e) {
if (!isObject(record)) {
return;
}
const target = e.target;
let keyPath = target.source.keyPath;
if (keyPath === null) {
Expand Down Expand Up @@ -396,7 +408,7 @@

records.some(function (record) {
let key;
if (hasOwn.call(record, 'item')) {
if (isObject(record) && hasOwn.call(record, 'item')) {
key = record.key;
record = record.item;
if (key != null) {
Expand Down
38 changes: 38 additions & 0 deletions tests/specs/bad-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,44 @@
});
});
});
it('should catch bad filters (with add)', function (done) {
db.open({server: this.dbName}).then(function (s) {
var caught = false;
s.names.add(null).then(function () {
s.names.query().filter('name', 'Alex').execute().catch(function (err) {
expect(err.name).to.equal('TypeError');
caught = true;
return s.names.query().filter(function () {
throw new Error('Bad filter function');
}).execute();
}).catch(function (err) {
expect(caught).to.equal(true);
expect(err.name).to.equal('Error');
s.close();
done();
});
});
});
});
it('should catch bad filters (with update)', function (done) {
db.open({server: this.dbName}).then(function (s) {
var caught = false;
s.names.update(null).then(function () {
s.names.query().filter('name', 'Alex').execute().catch(function (err) {
expect(err.name).to.equal('TypeError');
caught = true;
return s.names.query().filter(function () {
throw new Error('Bad filter function');
}).execute();
}).catch(function (err) {
expect(caught).to.equal(true);
expect(err.name).to.equal('Error');
s.close();
done();
});
});
});
});
});

describe('delete', function () {
Expand Down
1 change: 1 addition & 0 deletions tests/specs/server-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
});

describe('server.add-non-autoincrement key', function () {
this.timeout(5000);
var indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;

Expand Down
1 change: 1 addition & 0 deletions tests/specs/server-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
});

describe('server.update-custom-keys', function () {
this.timeout(5000);
var indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;

Expand Down

0 comments on commit c1fb72a

Please sign in to comment.