From 98cdc14e662a49564a0b0092fdc825ac45d63af4 Mon Sep 17 00:00:00 2001 From: bwgjoseph Date: Tue, 22 Jun 2021 17:57:33 +0800 Subject: [PATCH] fix: operator not mapped for multi-operation Close #4 --- src/index.ts | 12 +++++++----- test/methods.test.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8290d8d..bc2fb1a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,7 +29,7 @@ const _select = (data: any, params: any, ...args: any[]) => { /** * Allow `$ignoreCase` as additional filters option - * See {@link https://ottomanjs.com/classes/findoptions.html#optional-ignorecase ignoreCase} + * See {@link https://ottomanjs.com/classes/findoptions.html findOptions} */ const filterQueryOpts = { filters: ['$ignoreCase'], @@ -362,9 +362,10 @@ class OttomanService extends AdapterService implements InternalServi const { filters, query } = this.filterQuery(params); const cOptions = this._getOptions(filters); - if (id == null) { + if (id === null) { + const cQuery = this._mapQueryOperator(query); const entries = await this._find({ ...params, paginate: false }) as T[]; - const { message } = await this.Model.updateMany(query, data, cOptions); + const { message } = await this.Model.updateMany(cQuery, data, cOptions); if (message && message.success > 0) { return entries.map((e) => ({ ...e, ...data })); @@ -391,10 +392,11 @@ class OttomanService extends AdapterService implements InternalServi async _remove(id: NullableId, params: Params = {}): Promise { const { query } = this.filterQuery(params); - if (id == null) { + if (id === null) { + const cQuery = this._mapQueryOperator(query); // get all current data before removing const allData = await this._find({ ...params, paginate: false }) as T[]; - await this.Model.removeMany(query, this._options.ottoman); + await this.Model.removeMany(cQuery, this._options.ottoman); return allData; } diff --git a/test/methods.test.ts b/test/methods.test.ts index 153096e..bd02f73 100644 --- a/test/methods.test.ts +++ b/test/methods.test.ts @@ -73,6 +73,23 @@ const customTestSuite = (app: any, serviceName: string): void => { service.options.multi = []; }); + it('.patch + multi + $ne', async () => { + 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: 10 }, { + query: { name: { $ne: 'Dave' } }, + }); + + assert.strictEqual(data.length, 1); + assert.strictEqual(data[0].name, 'David'); + assert.strictEqual(data[0].age, 10); + + service.options.multi = []; + }); + it('.remove + multi + $select', async () => { service.options.multi = ['remove']; @@ -94,6 +111,22 @@ const customTestSuite = (app: any, serviceName: string): void => { service.options.multi = []; }); + it('.remove + multi + $ne', async () => { + 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: { name: { $ne: 'Dave' } }, + }); + + assert.strictEqual(data.length, 1); + assert.strictEqual(data[0].name, 'David'); + + service.options.multi = []; + }); + it('.find + $ignoreCase filter', async () => { await service.create({ name: 'Dave', age: 29, created: true });