Skip to content

Commit

Permalink
perf: remove redundant call to _select on multi patch
Browse files Browse the repository at this point in the history
Add test cases that is not covered by adapterTest

Close #2
  • Loading branch information
bwgjoseph committed Jun 8, 2021
1 parent 1456540 commit 23d2305
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class OttomanService<T = any> extends AdapterService<T> implements InternalServi

async _update(id: Id, data: T, params: Params = {}): Promise<T> {
if (id === null) {
return Promise.reject(new BadRequest('Not replacing multiple records. Did you mean `patch`?'));
return Promise.reject(new BadRequest('You can not replace multiple instances. Did you mean \'patch\'?'));
}

const { filters, query } = this.filterQuery(params);
Expand Down Expand Up @@ -344,7 +344,7 @@ class OttomanService<T = any> extends AdapterService<T> implements InternalServi
const { message } = await this.Model.updateMany(query, data, cOptions);

if (message && message.success > 0) {
return entries.map((e) => _select({ ...e, ...data }, params, this.id));
return entries.map((e) => ({ ...e, ...data }));
}

throw new NotFound(`No record found for query ${query}`);
Expand Down
90 changes: 88 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,93 @@ describe('Feathers Ottoman Service', () => {
testSuite(app, errors, 'posts-customid', 'customid');
});

it('should reject updateMany', () => {
assert.rejects(app.service('posts').update(null, {}));
it('.update + multi', async () => {
try {
await app.service('posts').update(null, {});
throw new Error('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'BadRequest',
'You can not replace multiple instances. Did you mean \'patch\'');
}
});

it('.create + multi + $select', async () => {
const service = app.service('posts');
service.options.multi = ['create'];

const items = [
{
name: 'Gerald',
age: 18,
},
{
name: 'Herald',
age: 18,
},
];

const data = await service.create(items, {
query: { $select: ['name'] },
});

assert.ok(Array.isArray(data), 'data is an array');
assert.ok(typeof data[0].id !== 'undefined', 'id is set');
assert.strictEqual(data[0].name, 'Gerald', 'first name matches');
assert.ok(typeof data[1].id !== 'undefined', 'id is set');
assert.strictEqual(data[1].name, 'Herald', 'second name macthes');
assert.ok(!data[0].age, 'data.age is falsy');
assert.ok(!data[1].age, 'data.age is falsy');

await service.remove(data[0].id);
await service.remove(data[1].id);

service.options.multi = [];
});

it('.patch + multi + $select', async () => {
const service = app.service('posts');
service.options.multi = ['patch'];

await service.create({ name: 'Dave', age: 29, created: true });
await service.create({ name: 'David', age: 3, created: true });

const data = await service.patch(null, { age: 2 }, {
query: { created: true, $select: ['age'] },
});

assert.strictEqual(data.length, 2, 'returned two entries');
assert.strictEqual(data[0].age, 2, 'First entry age was updated');
assert.strictEqual(data[1].age, 2, 'Second entry age was updated');
assert.ok(!data[0].name, 'data.name is falsy');
assert.ok(!data[1].name, 'data.name is falsy');

await service.remove(data[0].id);
await service.remove(data[1].id);

service.options.multi = [];
});

it('.remove + multi + $select', async () => {
const service = app.service('posts');
service.options.multi = ['remove'];

await service.create({ name: 'Dave', age: 29, created: true });
await service.create({ name: 'David', age: 3, created: true });

const data = await service.remove(null, {
query: { created: true, $select: ['name'] },
});

const names = data.map((person: any) => person.name);

assert.strictEqual(data.length, 2, 'returned two entries');
assert.ok(names.includes('Dave'), 'Dave removed');
assert.ok(names.includes('David'), 'David removed');
assert.ok(!data[0].age, 'data.age is falsy');
assert.ok(!data[1].age, 'data.age is falsy');

await service.remove(null);

service.options.multi = [];
});
});

0 comments on commit 23d2305

Please sign in to comment.