Skip to content
Merged
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
20 changes: 18 additions & 2 deletions lib/waterline/query/dql/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,25 @@ module.exports = function(criteria, cb) {
});

function after() {
callbacks.afterDestroy(self, result, function(err) {

// If no result was returned, default to empty array
if (!result) {
result = [];
}

// If values is not an array, return an array
if (!Array.isArray(result)) {
result = [result];
}

// Unserialize each value
var transformedValues = result.map(function(value) {
return self._transformer.unserialize(value);
});

callbacks.afterDestroy(self, transformedValues, function(err) {
if (err) return cb(err);
cb(null, result);
cb(null, transformedValues);
});
}

Expand Down
8 changes: 4 additions & 4 deletions test/integration/model/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Model', function() {

waterline.loadCollection(Collection);

var adapterDef = { destroy: function(con, col, options, cb) { return cb(null, true); }};
var adapterDef = { destroy: function(con, col, options, cb) { return cb(null, [{ id: 1, first_name: 'foo', last_name: 'bar' }]); }};

var connections = {
'my_foo': {
Expand All @@ -46,12 +46,12 @@ describe('Model', function() {
// TEST METHODS
////////////////////////////////////////////////////

it('should pass status from the adapter destroy method', function(done) {
it('should pass data from the adapter destroy method', function(done) {
var person = new collection._model({ id: 1, first_name: 'foo', last_name: 'bar' });

person.destroy(function(err, status) {
person.destroy(function(err, records) {
assert(!err);
assert(status === true);
assert.equal(records[0].last_name, 'bar');
done();
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/unit/query/query.destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('Collection Query', function() {
waterline.loadCollection(Model);

// Fixture Adapter Def
var adapterDef = { destroy: function(con, col, options, cb) { return cb(null, options); }};
var adapterDef = { destroy: function(con, col, options, cb) { return cb(null, [{ pkColumn: 1, name: 'foo' }]); }};

var connections = {
'foo': {
Expand All @@ -111,10 +111,10 @@ describe('Collection Query', function() {
});


it('should use the custom primary key when a single value is passed in', function(done) {
it('should transform column names when values are sent back', function(done) {
query.destroy(1, function(err, values) {
assert(!err);
assert(values.where.pkColumn === 1);
assert.equal(values[0].myPk, 1);
done();
});
});
Expand Down