proto.io 0.0.174 → 0.0.176

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.
Files changed (46) hide show
  1. package/dist/adapters/file/database.d.ts +2 -2
  2. package/dist/adapters/file/database.js +1 -0
  3. package/dist/adapters/file/database.js.map +1 -1
  4. package/dist/adapters/file/database.mjs +3 -2
  5. package/dist/adapters/file/database.mjs.map +1 -1
  6. package/dist/adapters/file/filesystem.d.ts +2 -2
  7. package/dist/adapters/file/google-cloud-storage.d.ts +2 -2
  8. package/dist/adapters/storage/progres.d.ts +3 -2
  9. package/dist/adapters/storage/progres.js +51 -29
  10. package/dist/adapters/storage/progres.js.map +1 -1
  11. package/dist/adapters/storage/progres.mjs +53 -31
  12. package/dist/adapters/storage/progres.mjs.map +1 -1
  13. package/dist/client.d.ts +3 -3
  14. package/dist/client.js +1 -1
  15. package/dist/client.mjs +3 -3
  16. package/dist/index.d.ts +3 -3
  17. package/dist/index.js +58 -314
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs +62 -318
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/internals/{index-CGX3qcjQ.d.ts → index-B0DE6MVr.d.ts} +2 -2
  22. package/dist/internals/index-B0DE6MVr.d.ts.map +1 -0
  23. package/dist/internals/{index-CE5tdYK8.d.ts → index-BZcW5wgM.d.ts} +32 -68
  24. package/dist/internals/index-BZcW5wgM.d.ts.map +1 -0
  25. package/dist/internals/{index-uwXdnxqN.d.ts → index-Bh60s4Tq.d.ts} +32 -2
  26. package/dist/internals/index-Bh60s4Tq.d.ts.map +1 -0
  27. package/dist/internals/{index--ifyu-GL.mjs → index-CTsc042s.mjs} +2 -2
  28. package/dist/internals/{index--ifyu-GL.mjs.map → index-CTsc042s.mjs.map} +1 -1
  29. package/dist/internals/{index-S_gTMQBh.js → index-Cd6nNb--.js} +31 -43
  30. package/dist/internals/index-Cd6nNb--.js.map +1 -0
  31. package/dist/internals/{index-C3fbOqmn.mjs → index-DHQhYGsJ.mjs} +2 -2
  32. package/dist/internals/{index-C3fbOqmn.mjs.map → index-DHQhYGsJ.mjs.map} +1 -1
  33. package/dist/internals/{index-R0gbIGc-.mjs → index-tEsKpX2d.mjs} +32 -44
  34. package/dist/internals/index-tEsKpX2d.mjs.map +1 -0
  35. package/dist/internals/{random-DVOUDDGg.js → random-CvVZek_8.js} +13 -1
  36. package/dist/internals/random-CvVZek_8.js.map +1 -0
  37. package/dist/internals/{random-DPRG8oW6.mjs → random-DGiu1IIq.mjs} +15 -3
  38. package/dist/internals/random-DGiu1IIq.mjs.map +1 -0
  39. package/package.json +1 -1
  40. package/dist/internals/index-CE5tdYK8.d.ts.map +0 -1
  41. package/dist/internals/index-CGX3qcjQ.d.ts.map +0 -1
  42. package/dist/internals/index-R0gbIGc-.mjs.map +0 -1
  43. package/dist/internals/index-S_gTMQBh.js.map +0 -1
  44. package/dist/internals/index-uwXdnxqN.d.ts.map +0 -1
  45. package/dist/internals/random-DPRG8oW6.mjs.map +0 -1
  46. package/dist/internals/random-DVOUDDGg.js.map +0 -1
package/dist/client.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { P as ProtoClient } from './internals/index-uwXdnxqN.js';
2
- export { c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-uwXdnxqN.js';
1
+ import { P as ProtoClient } from './internals/index-Bh60s4Tq.js';
2
+ export { c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-Bh60s4Tq.js';
3
3
  export { Decimal } from 'decimal.js';
4
- export { D as DeserializeOptions, S as SerializeOptions, d as TSerializable, e as deserialize, s as serialize } from './internals/index-CE5tdYK8.js';
4
+ export { D as DeserializeOptions, S as SerializeOptions, d as TSerializable, e as deserialize, s as serialize } from './internals/index-BZcW5wgM.js';
5
5
  import '@o2ter/utils-js';
6
6
  import 'socket.io-client';
7
7
  import '@socket.io/component-emitter';
package/dist/client.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('./internals/index-S_gTMQBh.js');
5
+ var index = require('./internals/index-Cd6nNb--.js');
6
6
  var Decimal = require('decimal.js');
7
7
  require('./internals/index-DXuW8UiB.js');
8
8
  require('lodash');
package/dist/client.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { c as ProtoClient } from './internals/index-R0gbIGc-.mjs';
2
- export { e as classExtends, d as deserialize, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser, s as serialize } from './internals/index-R0gbIGc-.mjs';
1
+ import { c as ProtoClient } from './internals/index-tEsKpX2d.mjs';
2
+ export { e as classExtends, d as deserialize, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser, s as serialize } from './internals/index-tEsKpX2d.mjs';
3
3
  export { Decimal } from 'decimal.js';
4
- import './internals/index--ifyu-GL.mjs';
4
+ import './internals/index-CTsc042s.mjs';
5
5
  import 'lodash';
6
6
  import './internals/private-BUpLAMZi.mjs';
7
7
  import '@o2ter/utils-js';
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import * as socket_io from 'socket.io';
2
2
  import { Router } from 'express';
3
3
  import { Server } from '@o2ter/server-js';
4
- import { T as TSchema, _ as _TValue, P as ProtoService, a as ProtoServiceOptions, b as ProtoServiceKeyOptions } from './internals/index-CE5tdYK8.js';
5
- export { D as DeserializeOptions, S as SerializeOptions, c as TFileStorage, d as TSerializable, e as deserialize, s as serialize } from './internals/index-CE5tdYK8.js';
4
+ import { T as TSchema, _ as _TValue, P as ProtoService, a as ProtoServiceOptions, b as ProtoServiceKeyOptions } from './internals/index-BZcW5wgM.js';
5
+ export { D as DeserializeOptions, S as SerializeOptions, c as TFileStorage, d as TSerializable, e as deserialize, s as serialize } from './internals/index-BZcW5wgM.js';
6
6
  import Decimal from 'decimal.js';
7
7
  export { Decimal } from 'decimal.js';
8
- export { P as ProtoClient, c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-uwXdnxqN.js';
8
+ export { P as ProtoClient, c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-Bh60s4Tq.js';
9
9
  import '@o2ter/utils-js';
10
10
  import 'jsonwebtoken';
11
11
  import 'lodash';
package/dist/index.js CHANGED
@@ -4,10 +4,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _ = require('lodash');
6
6
  var serverJs = require('@o2ter/server-js');
7
- var random = require('./internals/random-DVOUDDGg.js');
7
+ var random = require('./internals/random-CvVZek_8.js');
8
8
  var _private = require('./internals/private-CSB1Ep4g.js');
9
9
  var utilsJs = require('@o2ter/utils-js');
10
- var index = require('./internals/index-S_gTMQBh.js');
10
+ var index = require('./internals/index-Cd6nNb--.js');
11
11
  var index$1 = require('./internals/index-DXuW8UiB.js');
12
12
  var jwt = require('jsonwebtoken');
13
13
  var node_buffer = require('node:buffer');
@@ -134,6 +134,7 @@ const dispatcher = (proto, options, disableSecurity) => {
134
134
  async insert(options, attrs) {
135
135
  random.QueryValidator.recursiveCheck(attrs);
136
136
  const _validator = await validator();
137
+ _validator.validateCountMatches(options.className, options.countMatches ?? []);
137
138
  const _includes = _validator.decodeIncludes(options.className, options.includes ?? ['*']);
138
139
  const _matches = _validator.decodeMatches(options.className, options.matches ?? {}, _includes);
139
140
  if (!_validator.validateCLPs(options.className, 'create'))
@@ -145,6 +146,7 @@ const dispatcher = (proto, options, disableSecurity) => {
145
146
  className: options.className,
146
147
  includes: _includes,
147
148
  matches: _matches,
149
+ countMatches: options.countMatches ?? [],
148
150
  objectIdSize: proto[_private.PVK].options.objectIdSize
149
151
  }, _attrs), { lockTable: options.className, retry: true });
150
152
  }
@@ -158,6 +160,7 @@ const dispatcher = (proto, options, disableSecurity) => {
158
160
  async insertMany(options, values) {
159
161
  random.QueryValidator.recursiveCheck(values);
160
162
  const _validator = await validator();
163
+ _validator.validateCountMatches(options.className, options.countMatches ?? []);
161
164
  const _includes = _validator.decodeIncludes(options.className, options.includes ?? ['*']);
162
165
  const _matches = _validator.decodeMatches(options.className, options.matches ?? {}, _includes);
163
166
  if (!_validator.validateCLPs(options.className, 'create'))
@@ -169,6 +172,7 @@ const dispatcher = (proto, options, disableSecurity) => {
169
172
  className: options.className,
170
173
  includes: _includes,
171
174
  matches: _matches,
175
+ countMatches: options.countMatches ?? [],
172
176
  objectIdSize: proto[_private.PVK].options.objectIdSize
173
177
  }, _attrs), { lockTable: options.className, retry: true });
174
178
  }
@@ -248,46 +252,6 @@ const dispatcher = (proto, options, disableSecurity) => {
248
252
  };
249
253
  };
250
254
 
251
- //
252
- // proxy.ts
253
- //
254
- // The MIT License
255
- // Copyright (c) 2021 - 2024 O2ter Limited. All rights reserved.
256
- //
257
- // Permission is hereby granted, free of charge, to any person obtaining a copy
258
- // of this software and associated documentation files (the "Software"), to deal
259
- // in the Software without restriction, including without limitation the rights
260
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
261
- // copies of the Software, and to permit persons to whom the Software is
262
- // furnished to do so, subject to the following conditions:
263
- //
264
- // The above copyright notice and this permission notice shall be included in
265
- // all copies or substantial portions of the Software.
266
- //
267
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
268
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
269
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
270
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
271
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
272
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
273
- // THE SOFTWARE.
274
- //
275
- const proxy = (x) => {
276
- const self = x;
277
- const proxy = _.create(self);
278
- for (const name of _.uniq(_.flatMap(utilsJs.prototypes(proxy), x => Object.getOwnPropertyNames(x)))) {
279
- if (name === 'constructor')
280
- continue;
281
- if (_.isFunction(self[name])) {
282
- proxy[name] = self[name].bind(self);
283
- }
284
- else {
285
- Object.defineProperty(proxy, name, { get: () => self[name] });
286
- }
287
- }
288
- return proxy;
289
- };
290
-
291
255
  //
292
256
  // options.ts
293
257
  //
@@ -394,285 +358,43 @@ class _ProtoQuery extends index.TQuery {
394
358
  });
395
359
  }
396
360
  async insert(attrs, options) {
397
- const context = options?.context ?? {};
398
- const silent = _.castArray(options?.silent ?? []);
399
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
400
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
401
- const object = this._proto.Object(this.className);
402
- for (const [key, value] of _.toPairs(attrs)) {
403
- object[_private.PVK].mutated[key] = { $set: value };
404
- }
405
- if (_.isFunction(beforeSave))
406
- await beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
407
361
  const result = this._objectMethods(await this._dispatcher(options).insert({
408
362
  className: this.className,
409
363
  includes: this[_private.PVK].options.includes,
410
364
  matches: this[_private.PVK].options.matches,
411
- }, _.fromPairs([...object._set_entries()])));
365
+ countMatches: this[_private.PVK].options.countMatches,
366
+ }, attrs));
412
367
  if (!result)
413
368
  throw Error('Unable to insert document');
414
- if (_.isFunction(afterSave)) {
415
- try {
416
- await afterSave(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
417
- }
418
- catch (e) {
419
- console.error(e);
420
- }
421
- }
422
369
  return result;
423
370
  }
424
371
  async insertMany(values, options) {
425
- const context = options?.context ?? {};
426
- const silent = _.castArray(options?.silent ?? []);
427
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
428
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
429
- if (_.isFunction(beforeSave) || _.isFunction(afterSave)) {
430
- const objects = _.map(values, attr => {
431
- const object = this._proto.Object(this.className);
432
- for (const [key, value] of _.toPairs(attr)) {
433
- object[_private.PVK].mutated[key] = { $set: value };
434
- }
435
- return object;
436
- });
437
- if (_.isEmpty(objects))
438
- return 0;
439
- if (_.isFunction(beforeSave)) {
440
- await Promise.all(_.map(objects, object => beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
441
- }
442
- await this._dispatcher(options).insertMany({
443
- className: this.className,
444
- includes: this[_private.PVK].options.includes,
445
- matches: this[_private.PVK].options.matches,
446
- }, _.map(objects, x => _.fromPairs([...x._set_entries()])));
447
- if (_.isFunction(afterSave)) {
448
- await Promise.all(_.map(objects, async (object) => {
449
- try {
450
- await afterSave(proxy(Object.setPrototypeOf({ object: object.clone(), context }, options?.session ?? this._proto)));
451
- }
452
- catch (e) {
453
- console.error(e);
454
- }
455
- }));
456
- }
457
- return objects.length;
458
- }
459
372
  return this._dispatcher(options).insertMany({
460
373
  className: this.className,
461
374
  includes: this[_private.PVK].options.includes,
462
375
  matches: this[_private.PVK].options.matches,
376
+ countMatches: this[_private.PVK].options.countMatches,
463
377
  }, values);
464
378
  }
465
379
  async updateOne(update, options) {
466
- const context = options?.context ?? {};
467
- const silent = _.castArray(options?.silent ?? []);
468
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
469
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
470
- if (_.isFunction(beforeSave)) {
471
- const object = this._objectMethods(_.first(await utilsJs.asyncIterableToArray(this._dispatcher(options).find({ ...this._queryOptions, limit: 1 }))));
472
- if (!object)
473
- return undefined;
474
- object[_private.PVK].mutated = update;
475
- await beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
476
- update = object[_private.PVK].mutated;
477
- }
478
- const result = this._objectMethods(await this._dispatcher(options).updateOne(this._queryOptions, update));
479
- if (result && _.isFunction(afterSave)) {
480
- try {
481
- await afterSave(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
482
- }
483
- catch (e) {
484
- console.error(e);
485
- }
486
- }
487
- return result;
380
+ return this._objectMethods(await this._dispatcher(options).updateOne(this._queryOptions, update));
488
381
  }
489
382
  async updateMany(update, options) {
490
- const context = options?.context ?? {};
491
- const silent = _.castArray(options?.silent ?? []);
492
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
493
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
494
- if (_.isFunction(beforeSave) || _.isFunction(afterSave)) {
495
- const objects = this._objectMethods(await utilsJs.asyncIterableToArray(this._dispatcher(options).find(this._queryOptions)));
496
- if (_.isEmpty(objects))
497
- return 0;
498
- if (_.isFunction(beforeSave)) {
499
- await Promise.all(_.map(objects, object => beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
500
- }
501
- if (!_.isFunction(afterSave)) {
502
- return this._dispatcher(options).updateMany({
503
- ...this._queryOptions,
504
- filter: { _id: { $in: _.map(objects, x => x.objectId) } },
505
- }, update);
506
- }
507
- const updated = _.compact(await Promise.all(_.map(objects, x => this._dispatcher(options).updateOne({
508
- ...this._queryOptions,
509
- filter: { _id: { $eq: x.objectId } },
510
- }, update))));
511
- await Promise.all(_.map(updated, async (object) => {
512
- try {
513
- await afterSave(proxy(Object.setPrototypeOf({ object: object.clone(), context }, options?.session ?? this._proto)));
514
- }
515
- catch (e) {
516
- console.error(e);
517
- }
518
- }));
519
- return updated.length;
520
- }
521
383
  return this._dispatcher(options).updateMany(this._queryOptions, update);
522
384
  }
523
385
  async upsertOne(update, setOnInsert, options) {
524
- const context = options?.context ?? {};
525
- const silent = _.castArray(options?.silent ?? []);
526
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
527
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
528
- if (_.isFunction(beforeSave)) {
529
- let object = this._objectMethods(_.first(await utilsJs.asyncIterableToArray(this._dispatcher(options).find({ ...this._queryOptions, limit: 1 }))));
530
- if (object) {
531
- object[_private.PVK].mutated = update;
532
- }
533
- else {
534
- object = this._proto.Object(this.className);
535
- for (const [key, value] of _.toPairs(setOnInsert)) {
536
- object[_private.PVK].mutated[key] = { $set: value };
537
- }
538
- }
539
- await beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
540
- if (object.objectId) {
541
- update = object[_private.PVK].mutated;
542
- }
543
- else {
544
- setOnInsert = {};
545
- for (const [key, update] of _.toPairs(object[_private.PVK].mutated)) {
546
- const [op, value] = index$1.decodeUpdateOp(update);
547
- if (op === '$set') {
548
- setOnInsert[key] = value;
549
- }
550
- }
551
- }
552
- }
553
386
  const result = this._objectMethods(await this._dispatcher(options).upsertOne(this._queryOptions, update, setOnInsert));
554
387
  if (!result)
555
388
  throw Error('Unable to upsert document');
556
- if (_.isFunction(afterSave)) {
557
- try {
558
- await afterSave(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
559
- }
560
- catch (e) {
561
- console.error(e);
562
- }
563
- }
564
389
  return result;
565
390
  }
566
391
  async upsertMany(update, setOnInsert, options) {
567
- const context = options?.context ?? {};
568
- const silent = _.castArray(options?.silent ?? []);
569
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
570
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
571
- if (_.isFunction(beforeSave) || _.isFunction(afterSave)) {
572
- const objects = this._objectMethods(await utilsJs.asyncIterableToArray(this._dispatcher(options).find(this._queryOptions)));
573
- if (!_.isEmpty(objects) && _.isFunction(beforeSave)) {
574
- await Promise.all(_.map(objects, object => beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
575
- }
576
- if (_.isEmpty(objects)) {
577
- const result = await this._dispatcher(options).insert({
578
- className: this.className,
579
- includes: this[_private.PVK].options.includes,
580
- matches: this[_private.PVK].options.matches,
581
- }, setOnInsert);
582
- if (!result)
583
- throw Error('Unable to insert document');
584
- if (_.isFunction(afterSave)) {
585
- try {
586
- await afterSave(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
587
- }
588
- catch (e) {
589
- console.error(e);
590
- }
591
- }
592
- return { updated: 0, inserted: 1 };
593
- }
594
- if (!_.isFunction(afterSave)) {
595
- return {
596
- inserted: 0,
597
- updated: await this._dispatcher(options).updateMany({
598
- ...this._queryOptions,
599
- filter: { _id: { $in: _.map(objects, x => x.objectId) } },
600
- }, update),
601
- };
602
- }
603
- const updated = _.compact(await Promise.all(_.map(objects, x => this._dispatcher(options).updateOne({
604
- ...this._queryOptions,
605
- filter: { _id: { $eq: x.objectId } },
606
- }, update))));
607
- await Promise.all(_.map(updated, async (object) => {
608
- try {
609
- await afterSave(proxy(Object.setPrototypeOf({ object: object.clone(), context }, options?.session ?? this._proto)));
610
- }
611
- catch (e) {
612
- console.error(e);
613
- }
614
- }));
615
- return { updated: updated.length, inserted: 0 };
616
- }
617
392
  return this._dispatcher(options).upsertMany(this._queryOptions, update, setOnInsert);
618
393
  }
619
394
  async deleteOne(options) {
620
- const context = options?.context ?? {};
621
- const silent = _.castArray(options?.silent ?? []);
622
- const beforeDelete = _.includes(silent, 'beforeDelete') ? null : this._proto[_private.PVK].triggers?.beforeDelete?.[this.className];
623
- const afterDelete = _.includes(silent, 'afterDelete') ? null : this._proto[_private.PVK].triggers?.afterDelete?.[this.className];
624
- let result;
625
- if (_.isFunction(beforeDelete)) {
626
- const object = this._objectMethods(_.first(await utilsJs.asyncIterableToArray(this._dispatcher(options).find({ ...this._queryOptions, limit: 1 }))));
627
- if (!object)
628
- return undefined;
629
- await beforeDelete(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
630
- result = this._objectMethods(await this._dispatcher(options).deleteOne({
631
- ...this._queryOptions,
632
- filter: { _id: { $eq: object.objectId } },
633
- }));
634
- }
635
- else {
636
- result = this._objectMethods(await this._dispatcher(options).deleteOne(this._queryOptions));
637
- }
638
- if (result && _.isFunction(afterDelete)) {
639
- try {
640
- await afterDelete(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
641
- }
642
- catch (e) {
643
- console.error(e);
644
- }
645
- }
646
- return result;
395
+ return this._objectMethods(await this._dispatcher(options).deleteOne(this._queryOptions));
647
396
  }
648
397
  async deleteMany(options) {
649
- const context = options?.context ?? {};
650
- const silent = _.castArray(options?.silent ?? []);
651
- const beforeDelete = _.includes(silent, 'beforeDelete') ? null : this._proto[_private.PVK].triggers?.beforeDelete?.[this.className];
652
- const afterDelete = _.includes(silent, 'afterDelete') ? null : this._proto[_private.PVK].triggers?.afterDelete?.[this.className];
653
- if (_.isFunction(beforeDelete) || _.isFunction(afterDelete)) {
654
- const objects = this._objectMethods(await utilsJs.asyncIterableToArray(this._dispatcher(options).find(this._queryOptions)));
655
- if (_.isEmpty(objects))
656
- return 0;
657
- if (_.isFunction(beforeDelete)) {
658
- await Promise.all(_.map(objects, object => beforeDelete(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
659
- }
660
- await this._dispatcher(options).deleteMany({
661
- ...this._queryOptions,
662
- filter: { _id: { $in: _.map(objects, x => x.objectId) } },
663
- });
664
- if (_.isFunction(afterDelete)) {
665
- await Promise.all(_.map(objects, async (object) => {
666
- try {
667
- await afterDelete(proxy(Object.setPrototypeOf({ object: object.clone(), context }, options?.session ?? this._proto)));
668
- }
669
- catch (e) {
670
- console.error(e);
671
- }
672
- }));
673
- }
674
- return objects.length;
675
- }
676
398
  return this._dispatcher(options).deleteMany(this._queryOptions);
677
399
  }
678
400
  }
@@ -873,6 +595,46 @@ const varifyPassword = async (alg, password, options) => {
873
595
  }
874
596
  };
875
597
 
598
+ //
599
+ // proxy.ts
600
+ //
601
+ // The MIT License
602
+ // Copyright (c) 2021 - 2024 O2ter Limited. All rights reserved.
603
+ //
604
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
605
+ // of this software and associated documentation files (the "Software"), to deal
606
+ // in the Software without restriction, including without limitation the rights
607
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
608
+ // copies of the Software, and to permit persons to whom the Software is
609
+ // furnished to do so, subject to the following conditions:
610
+ //
611
+ // The above copyright notice and this permission notice shall be included in
612
+ // all copies or substantial portions of the Software.
613
+ //
614
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
615
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
616
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
617
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
618
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
619
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
620
+ // THE SOFTWARE.
621
+ //
622
+ const proxy = (x) => {
623
+ const self = x;
624
+ const proxy = _.create(self);
625
+ for (const name of _.uniq(_.flatMap(utilsJs.prototypes(proxy), x => Object.getOwnPropertyNames(x)))) {
626
+ if (name === 'constructor')
627
+ continue;
628
+ if (_.isFunction(self[name])) {
629
+ proxy[name] = self[name].bind(self);
630
+ }
631
+ else {
632
+ Object.defineProperty(proxy, name, { get: () => self[name] });
633
+ }
634
+ }
635
+ return proxy;
636
+ };
637
+
876
638
  //
877
639
  // internal.ts
878
640
  //
@@ -993,7 +755,6 @@ const mergeSchema = (...schemas) => _.reduce(schemas, (acc, schema) => ({
993
755
  class ProtoInternal {
994
756
  options;
995
757
  functions = {};
996
- triggers = {};
997
758
  constructor(options) {
998
759
  validateSchemaName(options.schema);
999
760
  const schema = mergeSchema(defaultSchema, options.fileStorage.schema, options.schema);
@@ -1670,26 +1431,6 @@ class ProtoService extends index.ProtoType {
1670
1431
  define(name, callback, options) {
1671
1432
  this[_private.PVK].functions[name] = options ? { callback, ...options } : callback;
1672
1433
  }
1673
- beforeSave(name, callback) {
1674
- if (!this[_private.PVK].triggers.beforeSave)
1675
- this[_private.PVK].triggers.beforeSave = {};
1676
- this[_private.PVK].triggers.beforeSave[name] = callback;
1677
- }
1678
- afterSave(name, callback) {
1679
- if (!this[_private.PVK].triggers.afterSave)
1680
- this[_private.PVK].triggers.afterSave = {};
1681
- this[_private.PVK].triggers.afterSave[name] = callback;
1682
- }
1683
- beforeDelete(name, callback) {
1684
- if (!this[_private.PVK].triggers.beforeDelete)
1685
- this[_private.PVK].triggers.beforeDelete = {};
1686
- this[_private.PVK].triggers.beforeDelete[name] = callback;
1687
- }
1688
- afterDelete(name, callback) {
1689
- if (!this[_private.PVK].triggers.afterDelete)
1690
- this[_private.PVK].triggers.afterDelete = {};
1691
- this[_private.PVK].triggers.afterDelete[name] = callback;
1692
- }
1693
1434
  lockTable(className, update) {
1694
1435
  return this.storage.lockTable(className, update);
1695
1436
  }
@@ -1727,12 +1468,14 @@ class ProtoService extends index.ProtoType {
1727
1468
  });
1728
1469
  }
1729
1470
  async gc(classNames) {
1471
+ const time = new Date();
1730
1472
  for (const className of _.castArray(classNames ?? this.classes())) {
1731
1473
  if (className === 'File') {
1732
1474
  const found = this.storage.find({
1733
1475
  className: 'File',
1734
- filter: index$2.QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1476
+ filter: index$2.QuerySelector.decode({ _expired_at: { $lt: time } }),
1735
1477
  matches: {},
1478
+ countMatches: [],
1736
1479
  includes: ['_id', '_expired_at', 'token'],
1737
1480
  objectIdSize: 0
1738
1481
  });
@@ -1744,9 +1487,10 @@ class ProtoService extends index.ProtoType {
1744
1487
  }
1745
1488
  await this.storage.deleteMany({
1746
1489
  className,
1747
- filter: index$2.QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1490
+ filter: index$2.QuerySelector.decode({ _expired_at: { $lt: time } }),
1748
1491
  includes: ['_id', '_expired_at'],
1749
1492
  matches: {},
1493
+ countMatches: [],
1750
1494
  objectIdSize: 0
1751
1495
  });
1752
1496
  }
@@ -1886,12 +1630,12 @@ const verifyRelatedBy = (relatedBy) => {
1886
1630
  var classesRoute = (router, proto) => {
1887
1631
  const defaultHandler = async (req) => {
1888
1632
  const { name } = req.params;
1889
- const { operation, context, silent, random, attributes, update, setOnInsert, relatedBy, ...options } = index.deserialize(req.body);
1633
+ const { operation, random, attributes, update, setOnInsert, relatedBy, ...options } = index.deserialize(req.body);
1890
1634
  verifyRelatedBy(relatedBy);
1891
1635
  const payload = proto.connect(req);
1892
1636
  const query = relatedBy ? payload.Relation(payload.Object(relatedBy.className, relatedBy.objectId), relatedBy.key) : payload.Query(name);
1893
1637
  query[_private.PVK].options = options;
1894
- const opts = { master: payload.isMaster, context, silent };
1638
+ const opts = { master: payload.isMaster };
1895
1639
  switch (operation) {
1896
1640
  case 'explain':
1897
1641
  if (!payload.isMaster)