Skip to content

Commit

Permalink
Support params.query in update() (#189)
Browse files Browse the repository at this point in the history
* Support params.query in update()

* Fix code and add tests
  • Loading branch information
TimNZ authored and daffl committed Mar 27, 2018
1 parent c08f06f commit de2874e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 53 deletions.
53 changes: 27 additions & 26 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ class Service {

return result[0];
})
.then(select(params, this.id))
.catch(error => {
throw new errors.NotFound(`No record found for id '${id}'`, error);
});
.then(select(params, this.id))
.catch(error => {
throw new errors.NotFound(`No record found for id '${id}'`, error);
});
}

// returns either the model intance for an id or all unpaginated
Expand Down Expand Up @@ -187,26 +187,26 @@ class Service {

return this._getOrFind(id, params)
.then(results => this.getModel(params).update(omit(data, this.id), options))
.then(results => {
if (id === null) {
return results[1];
}
.then(results => {
if (id === null) {
return results[1];
}

if (!results[1].length) {
throw new errors.NotFound(`No record found for id '${id}'`);
}
if (!results[1].length) {
throw new errors.NotFound(`No record found for id '${id}'`);
}

return results[1][0];
})
.then(select(params, this.id))
.catch(utils.errorHandler);
return results[1][0];
})
.then(select(params, this.id))
.catch(utils.errorHandler);
}

// By default we will just query for the one id. For multi patch
// we create a list of the ids of all items that will be changed
// to re-query them after the update
const ids = id === null ? this._find(params)
.then(mapIds) : Promise.resolve([ id ]);
.then(mapIds) : Promise.resolve([ id ]);

return ids
.then(idList => {
Expand All @@ -217,19 +217,20 @@ class Service {
});

return Model.update(omit(data, this.id), options)
.then(() => {
if (params.$returning !== false) {
return this._getOrFind(id, findParams);
} else {
return Promise.resolve([]);
}
});
.then(() => {
if (params.$returning !== false) {
return this._getOrFind(id, findParams);
} else {
return Promise.resolve([]);
}
});
})
.then(select(params, this.id))
.catch(utils.errorHandler);
}

update (id, data, params) {
const where = Object.assign({}, filterQuery(params.query || {}).query);
const options = Object.assign({ raw: this.raw }, params.sequelize);

if (Array.isArray(data)) {
Expand All @@ -239,7 +240,7 @@ class Service {
// Force the {raw: false} option as the instance is needed to properly
// update

return this._get(id, { sequelize: { raw: false } }).then(instance => {
return this._get(id, { sequelize: { raw: false }, query: where }).then(instance => {
if (!instance) {
throw new errors.NotFound(`No record found for id '${id}'`);
}
Expand All @@ -255,8 +256,8 @@ class Service {

return instance.update(copy, {raw: false}).then(() => this._get(id, {sequelize: options}));
})
.then(select(params, this.id))
.catch(utils.errorHandler);
.then(select(params, this.id))
.catch(utils.errorHandler);
}

remove (id, params) {
Expand Down
72 changes: 45 additions & 27 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,37 @@ describe('Feathers Sequelize Service', () => {
assert.equal(people.data[0].name, name);
assert.equal(people.data[0].age, null);
})
.then(() => people.remove(person.id))
.catch((err) => { people.remove(person.id); throw (err); })
.then(() => people.remove(person.id))
.catch((err) => { people.remove(person.id); throw (err); })
);
});

it('correctly persists updates (#125)', () => {
const updateName = 'Ryan';

return people.update(_ids.Kirsten, { name: updateName })
.then((data) => people.get(_ids.Kirsten))
.then(updatedPerson => {
assert.equal(updatedPerson.name, updateName);
});
.then((data) => people.get(_ids.Kirsten))
.then(updatedPerson => {
assert.equal(updatedPerson.name, updateName);
});
});

it('corrently updates records using optional query param', () => {
const updateAge = 40;
const updateName = 'Kirtsten';
return people.update(_ids.Kirsten, { name: updateName, age: updateAge }, {query: {name: 'Kirsten'}})
.then((data) => people.get(_ids.Kirsten))
.then(updatedPerson => {
assert.equal(updatedPerson.age, updateAge);
});
});

it('fails update when query prevents result in no record match for id', () => {
const updateAge = 50;
const updateName = 'Kirtsten';
return people.update(_ids.Kirsten, { name: updateName, age: updateAge }, {query: {name: 'John'}})
.then((data) => assert(false, 'Should have thrown an error'))
.catch(err => assert(err.message.indexOf('No record found') >= 0));
});
});

Expand All @@ -225,25 +243,25 @@ describe('Feathers Sequelize Service', () => {

beforeEach(() =>
people.create({ name: 'Kirsten', age: 30 })
.then(result => {
_data.Kirsten = result;
_ids.Kirsten = result.id;
return orders.create([
{ name: 'Order 1', personId: result.id },
{ name: 'Order 2', personId: result.id },
{ name: 'Order 3', personId: result.id }
]);
})
.then(() => people.create({ name: 'Ryan', age: 30 }))
.then(result => {
_data.Ryan = result;
_ids.Ryan = result.id;
return orders.create([
{ name: 'Order 4', personId: result.id },
{ name: 'Order 5', personId: result.id },
{ name: 'Order 6', personId: result.id }
]);
})
.then(result => {
_data.Kirsten = result;
_ids.Kirsten = result.id;
return orders.create([
{ name: 'Order 1', personId: result.id },
{ name: 'Order 2', personId: result.id },
{ name: 'Order 3', personId: result.id }
]);
})
.then(() => people.create({ name: 'Ryan', age: 30 }))
.then(result => {
_data.Ryan = result;
_ids.Ryan = result.id;
return orders.create([
{ name: 'Order 4', personId: result.id },
{ name: 'Order 5', personId: result.id },
{ name: 'Order 6', personId: result.id }
]);
})
);

afterEach(() =>
Expand Down Expand Up @@ -304,8 +322,8 @@ describe('Feathers Sequelize Service', () => {
assert.equal(result.data.length, 0);
});
})
.then(() => people.remove(person.id))
.catch((err) => { people.remove(person.id); throw (err); });
.then(() => people.remove(person.id))
.catch((err) => { people.remove(person.id); throw (err); });
});
});
});
Expand Down

0 comments on commit de2874e

Please sign in to comment.