Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle invalid inputs for destroyAll #1597

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/connectors/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,15 @@ Memory.prototype.destroyAll = function destroyAll(model, where, options, callbac
var filter = null;
var count = 0;
if (where) {
for (var key in where) {
var p = this._models[model].properties[key];
if (key !== 'and' && key !== 'or' && p == null) {
callback(new Error(
`Unknown property ${key} used in a "where" condition for model ${model}.`
));
}
}

filter = applyFilter({where: where});
Object.keys(cache).forEach(function(id) {
if (!filter || filter(this.fromDb(model, cache[id]))) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function selectFields(fields) {
}

/**
* Remove undefined values from the queury object
* Remove undefined values from the query object
* @param query
* @param handleUndefined {String} either "nullify", "throw" or "ignore" (default: "ignore")
* @returns {exports.map|*}
Expand Down
58 changes: 56 additions & 2 deletions test/manipulation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,10 @@ describe('manipulation', function() {
});

describe('destroy', function() {
beforeEach(function resetFixtures(done) {
Person.destroyAll(done);
});

it('should destroy record', function(done) {
Person.create(function(err, p) {
if (err) return done(err);
Expand Down Expand Up @@ -1685,8 +1689,58 @@ describe('manipulation', function() {
.catch(done);
});

// TODO: implement destroy with filtered set
it('should destroy filtered set of records');
it('should destroy filtered set of records', function() {
return givenNamedPeople(['John', 'Jane', 'Mary'])
.then(() =>
Person.destroyAll({name: 'Jane'}))
.then(() => Person.find({fields: {name: true}})
.then(users => {
let userData = [];
users.forEach(user => userData.push(user.__data));
userData.should.deepEqual([
{name: 'John'},
{name: 'Mary'},
]);
}));
});

it('should reject unknown properties', function() {
return givenNamedPeople(['John', 'Jane', 'Mary'])
.then(() =>
Person.destroyAll({names: 'John'}).should.be.rejectedWith(/Unknown property/))
.then(() => Person.find({fields: {name: true}, order: 'name ASC'})
.then(users => {
let userData = [];
users.forEach(user => userData.push(user.__data));
userData.should.deepEqual([
{name: 'Jane'},
{name: 'John'},
{name: 'Mary'},
]);
}));
});

it('should reject where object inside a filter object', function() {
return givenNamedPeople(['John', 'Jane', 'Mary'])
.then(() =>
Person.destroyAll({where: {name: 'John'}}).should.be.rejectedWith(/Unknown property/))
.then(() => Person.find({fields: {name: true}})
.then(users => {
let userData = [];
users.forEach(user => userData.push(user.__data));
userData.should.deepEqual([
{name: 'John'},
{name: 'Jane'},
{name: 'Mary'},
]);
}));
});

function givenNamedPeople(namesList) {
return Promise.all(
namesList.map(name => Person.create({name}))
);
}
});

bdd.describeIf(connectorCapabilities.reportDeletedCount !== false &&
Expand Down