Skip to content

Commit

Permalink
fix: handle query ById + ottoman operator ($ne, $in)
Browse files Browse the repository at this point in the history
Close #5
  • Loading branch information
bwgjoseph committed Jun 22, 2021
1 parent 98cdc14 commit 7e12ac1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ class OttomanService<T = any> extends AdapterService<T> implements InternalServi
* @returns reconstructed query
*/
_getQuery(id: string, params: Params): Query {
const { query } = this.filterQuery(params);
let { query } = this.filterQuery(params);
query = this._mapQueryOperator(query);

if (id) {
// Pass in both `id` and `query.id`
Expand Down
64 changes: 60 additions & 4 deletions test/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ const customTestSuite = (app: any, serviceName: string): void => {
service.options.whitelist = [];
});

it('.update + multi', async () => {
it('.get + id + query (id + $ne)', async () => {
const data = await service.create({ name: 'Dave', age: 29, created: true });

try {
await service.update(null, {});
await service.get(data.id, {
query: { id: 'other', name: { $ne: 'Dave' } },
});
throw new Error('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'BadRequest',
'You can not replace multiple instances. Did you mean \'patch\'');
assert.strictEqual(error.name, 'NotFound',
`No record found for id ${data.id}`);
}
});

Expand Down Expand Up @@ -54,6 +58,44 @@ const customTestSuite = (app: any, serviceName: string): void => {
service.options.multi = [];
});

it('.update + multi', async () => {
try {
await service.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('.update + id + query (id + $ne)', async () => {
const data = await service.create({ name: 'Dave', age: 29, created: true });

try {
await service.update(data.id, { name: 'joseph' }, {
query: { id: 'other', name: { $ne: 'Dave' } },
});
throw new Error('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'NotFound',
`No record found for id ${data.id}`);
}
});

it('.patch + id + query (id + $ne)', async () => {
const data = await service.create({ name: 'Dave', age: 29, created: true });

try {
await service.patch(data.id, { name: 'joseph' }, {
query: { id: 'other', name: { $ne: 'Dave' } },
});
throw new Error('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'NotFound',
`No record found for id ${data.id}`);
}
});

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

Expand Down Expand Up @@ -90,6 +132,20 @@ const customTestSuite = (app: any, serviceName: string): void => {
service.options.multi = [];
});

it('.remove + id + query (id + $ne)', async () => {
const data = await service.create({ name: 'Dave', age: 29, created: true });

try {
await service.remove(data.id, {
query: { id: 'other', name: { $ne: 'Dave' } },
});
throw new Error('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'NotFound',
`No record found for id ${data.id}`);
}
});

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

Expand Down

0 comments on commit 7e12ac1

Please sign in to comment.