Skip to content

Commit

Permalink
fix: operator not mapped for multi-operation
Browse files Browse the repository at this point in the history
Close #4
  • Loading branch information
bwgjoseph committed Jun 22, 2021
1 parent 63a8ce5 commit 98cdc14
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -362,9 +362,10 @@ class OttomanService<T = any> extends AdapterService<T> 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 }));
Expand All @@ -391,10 +392,11 @@ class OttomanService<T = any> extends AdapterService<T> implements InternalServi
async _remove(id: NullableId, params: Params = {}): Promise<T | T[]> {
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;
}
Expand Down
33 changes: 33 additions & 0 deletions test/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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 });

Expand Down

0 comments on commit 98cdc14

Please sign in to comment.