@opra/mongodb 1.0.0-alpha.36 → 1.0.0-alpha.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -348,5 +348,89 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
348
348
  const r = await this._dbUpdateMany(filter, update, mongoOptions);
349
349
  return r.matchedCount;
350
350
  }
351
+ async _executeCommand(command, commandFn) {
352
+ try {
353
+ const result = await super._executeCommand(command, async () => {
354
+ /** Call before[X] hooks */
355
+ if (command.crud === 'create')
356
+ await this._beforeCreate(command);
357
+ else if (command.crud === 'update' && command.byId) {
358
+ await this._beforeUpdate(command);
359
+ }
360
+ else if (command.crud === 'update' && !command.byId) {
361
+ await this._beforeUpdateMany(command);
362
+ }
363
+ else if (command.crud === 'delete' && command.byId) {
364
+ await this._beforeDelete(command);
365
+ }
366
+ else if (command.crud === 'delete' && !command.byId) {
367
+ await this._beforeDeleteMany(command);
368
+ }
369
+ /** Call command function */
370
+ return commandFn();
371
+ });
372
+ /** Call after[X] hooks */
373
+ if (command.crud === 'create')
374
+ await this._afterCreate(command, result);
375
+ else if (command.crud === 'update' && command.byId) {
376
+ await this._afterUpdate(command, result);
377
+ }
378
+ else if (command.crud === 'update' && !command.byId) {
379
+ await this._afterUpdateMany(command, result);
380
+ }
381
+ else if (command.crud === 'delete' && command.byId) {
382
+ await this._afterDelete(command, result);
383
+ }
384
+ else if (command.crud === 'delete' && !command.byId) {
385
+ await this._afterDeleteMany(command, result);
386
+ }
387
+ return result;
388
+ }
389
+ catch (e) {
390
+ Error.captureStackTrace(e, this._executeCommand);
391
+ await this.onError?.(e, this);
392
+ throw e;
393
+ }
394
+ }
395
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
396
+ async _beforeCreate(command) {
397
+ // Do nothing
398
+ }
399
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
400
+ async _beforeUpdate(command) {
401
+ // Do nothing
402
+ }
403
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
404
+ async _beforeUpdateMany(command) {
405
+ // Do nothing
406
+ }
407
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
408
+ async _beforeDelete(command) {
409
+ // Do nothing
410
+ }
411
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
412
+ async _beforeDeleteMany(command) {
413
+ // Do nothing
414
+ }
415
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
416
+ async _afterCreate(command, result) {
417
+ // Do nothing
418
+ }
419
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
420
+ async _afterUpdate(command, result) {
421
+ // Do nothing
422
+ }
423
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
424
+ async _afterUpdateMany(command, affected) {
425
+ // Do nothing
426
+ }
427
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
428
+ async _afterDelete(command, affected) {
429
+ // Do nothing
430
+ }
431
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
432
+ async _afterDeleteMany(command, affected) {
433
+ // Do nothing
434
+ }
351
435
  }
352
436
  exports.MongoEntityService = MongoEntityService;
@@ -344,4 +344,88 @@ export class MongoEntityService extends MongoService {
344
344
  const r = await this._dbUpdateMany(filter, update, mongoOptions);
345
345
  return r.matchedCount;
346
346
  }
347
+ async _executeCommand(command, commandFn) {
348
+ try {
349
+ const result = await super._executeCommand(command, async () => {
350
+ /** Call before[X] hooks */
351
+ if (command.crud === 'create')
352
+ await this._beforeCreate(command);
353
+ else if (command.crud === 'update' && command.byId) {
354
+ await this._beforeUpdate(command);
355
+ }
356
+ else if (command.crud === 'update' && !command.byId) {
357
+ await this._beforeUpdateMany(command);
358
+ }
359
+ else if (command.crud === 'delete' && command.byId) {
360
+ await this._beforeDelete(command);
361
+ }
362
+ else if (command.crud === 'delete' && !command.byId) {
363
+ await this._beforeDeleteMany(command);
364
+ }
365
+ /** Call command function */
366
+ return commandFn();
367
+ });
368
+ /** Call after[X] hooks */
369
+ if (command.crud === 'create')
370
+ await this._afterCreate(command, result);
371
+ else if (command.crud === 'update' && command.byId) {
372
+ await this._afterUpdate(command, result);
373
+ }
374
+ else if (command.crud === 'update' && !command.byId) {
375
+ await this._afterUpdateMany(command, result);
376
+ }
377
+ else if (command.crud === 'delete' && command.byId) {
378
+ await this._afterDelete(command, result);
379
+ }
380
+ else if (command.crud === 'delete' && !command.byId) {
381
+ await this._afterDeleteMany(command, result);
382
+ }
383
+ return result;
384
+ }
385
+ catch (e) {
386
+ Error.captureStackTrace(e, this._executeCommand);
387
+ await this.onError?.(e, this);
388
+ throw e;
389
+ }
390
+ }
391
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
392
+ async _beforeCreate(command) {
393
+ // Do nothing
394
+ }
395
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
396
+ async _beforeUpdate(command) {
397
+ // Do nothing
398
+ }
399
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
400
+ async _beforeUpdateMany(command) {
401
+ // Do nothing
402
+ }
403
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
404
+ async _beforeDelete(command) {
405
+ // Do nothing
406
+ }
407
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
408
+ async _beforeDeleteMany(command) {
409
+ // Do nothing
410
+ }
411
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
412
+ async _afterCreate(command, result) {
413
+ // Do nothing
414
+ }
415
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
416
+ async _afterUpdate(command, result) {
417
+ // Do nothing
418
+ }
419
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
420
+ async _afterUpdateMany(command, affected) {
421
+ // Do nothing
422
+ }
423
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
424
+ async _afterDelete(command, affected) {
425
+ // Do nothing
426
+ }
427
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
428
+ async _afterDeleteMany(command, affected) {
429
+ // Do nothing
430
+ }
347
431
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "1.0.0-alpha.36",
3
+ "version": "1.0.0-alpha.38",
4
4
  "description": "Opra MongoDB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -11,8 +11,8 @@
11
11
  "valgen": "^5.9.0"
12
12
  },
13
13
  "peerDependencies": {
14
- "@opra/common": "^1.0.0-alpha.36",
15
- "@opra/core": "^1.0.0-alpha.36",
14
+ "@opra/common": "^1.0.0-alpha.38",
15
+ "@opra/core": "^1.0.0-alpha.38",
16
16
  "mongodb": ">= 6.0.0"
17
17
  },
18
18
  "type": "module",
@@ -172,4 +172,15 @@ export declare class MongoEntityService<T extends mongodb.Document> extends Mong
172
172
  * @param {MongoEntityService.UpdateManyCommand<T>} command
173
173
  */
174
174
  protected _updateMany(command: MongoEntityService.UpdateManyCommand<T>): Promise<number>;
175
+ protected _executeCommand(command: MongoEntityService.CommandInfo, commandFn: () => any): Promise<any>;
176
+ protected _beforeCreate(command: MongoEntityService.CreateCommand): Promise<void>;
177
+ protected _beforeUpdate(command: MongoEntityService.UpdateOneCommand<T>): Promise<void>;
178
+ protected _beforeUpdateMany(command: MongoEntityService.UpdateManyCommand<T>): Promise<void>;
179
+ protected _beforeDelete(command: MongoEntityService.DeleteCommand<T>): Promise<void>;
180
+ protected _beforeDeleteMany(command: MongoEntityService.DeleteCommand<T>): Promise<void>;
181
+ protected _afterCreate(command: MongoEntityService.CreateCommand, result: PartialDTO<T>): Promise<void>;
182
+ protected _afterUpdate(command: MongoEntityService.UpdateOneCommand<T>, result?: PartialDTO<T>): Promise<void>;
183
+ protected _afterUpdateMany(command: MongoEntityService.UpdateManyCommand<T>, affected: number): Promise<void>;
184
+ protected _afterDelete(command: MongoEntityService.DeleteCommand<T>, affected: number): Promise<void>;
185
+ protected _afterDeleteMany(command: MongoEntityService.DeleteCommand<T>, affected: number): Promise<void>;
175
186
  }