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/index.mjs CHANGED
@@ -1,18 +1,18 @@
1
1
  import _ from 'lodash';
2
2
  import { Server } from '@o2ter/server-js';
3
- import { Q as QueryValidator, r as resolveColumn, a as resolveDataType, g as generateId } from './internals/random-DPRG8oW6.mjs';
3
+ import { Q as QueryValidator, r as resolveColumn, a as resolveDataType, g as generateId } from './internals/random-DGiu1IIq.mjs';
4
4
  import { P as PVK } from './internals/private-BUpLAMZi.mjs';
5
- import { prototypes, asyncStream, asyncIterableToArray, isBinaryData, base64ToBuffer } from '@o2ter/utils-js';
6
- import { T as TQuery, M as MASTER_USER_HEADER_NAME, a as MASTER_PASS_HEADER_NAME, A as AUTH_COOKIE_KEY, b as TUser, P as ProtoType, s as serialize, d as deserialize, U as UPLOAD_TOKEN_HEADER_NAME } from './internals/index-R0gbIGc-.mjs';
7
- export { c as ProtoClient, e as classExtends, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser } from './internals/index-R0gbIGc-.mjs';
8
- import { i as isPointer, a as isRelation, d as decodeUpdateOp, T as TObject, b as isShape, c as defaultObjectKeyTypes, e as isPrimitive } from './internals/index--ifyu-GL.mjs';
5
+ import { asyncStream, prototypes, isBinaryData, base64ToBuffer } from '@o2ter/utils-js';
6
+ import { T as TQuery, M as MASTER_USER_HEADER_NAME, a as MASTER_PASS_HEADER_NAME, A as AUTH_COOKIE_KEY, b as TUser, P as ProtoType, s as serialize, d as deserialize, U as UPLOAD_TOKEN_HEADER_NAME } from './internals/index-tEsKpX2d.mjs';
7
+ export { c as ProtoClient, e as classExtends, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser } from './internals/index-tEsKpX2d.mjs';
8
+ import { i as isPointer, a as isRelation, T as TObject, b as isShape, d as defaultObjectKeyTypes, c as isPrimitive } from './internals/index-CTsc042s.mjs';
9
9
  import jwt from 'jsonwebtoken';
10
10
  import { Blob } from 'node:buffer';
11
11
  import { Readable } from 'node:stream';
12
12
  import { scrypt } from 'node:crypto';
13
13
  import { promisify } from 'util';
14
14
  import { randomBytes, randomUUID } from '@o2ter/crypto-js';
15
- import { Q as QuerySelector } from './internals/index-C3fbOqmn.mjs';
15
+ import { Q as QuerySelector } from './internals/index-DHQhYGsJ.mjs';
16
16
  import queryType from 'query-types';
17
17
  import busboy from 'busboy';
18
18
  export { Decimal } from 'decimal.js';
@@ -131,6 +131,7 @@ const dispatcher = (proto, options, disableSecurity) => {
131
131
  async insert(options, attrs) {
132
132
  QueryValidator.recursiveCheck(attrs);
133
133
  const _validator = await validator();
134
+ _validator.validateCountMatches(options.className, options.countMatches ?? []);
134
135
  const _includes = _validator.decodeIncludes(options.className, options.includes ?? ['*']);
135
136
  const _matches = _validator.decodeMatches(options.className, options.matches ?? {}, _includes);
136
137
  if (!_validator.validateCLPs(options.className, 'create'))
@@ -142,6 +143,7 @@ const dispatcher = (proto, options, disableSecurity) => {
142
143
  className: options.className,
143
144
  includes: _includes,
144
145
  matches: _matches,
146
+ countMatches: options.countMatches ?? [],
145
147
  objectIdSize: proto[PVK].options.objectIdSize
146
148
  }, _attrs), { lockTable: options.className, retry: true });
147
149
  }
@@ -155,6 +157,7 @@ const dispatcher = (proto, options, disableSecurity) => {
155
157
  async insertMany(options, values) {
156
158
  QueryValidator.recursiveCheck(values);
157
159
  const _validator = await validator();
160
+ _validator.validateCountMatches(options.className, options.countMatches ?? []);
158
161
  const _includes = _validator.decodeIncludes(options.className, options.includes ?? ['*']);
159
162
  const _matches = _validator.decodeMatches(options.className, options.matches ?? {}, _includes);
160
163
  if (!_validator.validateCLPs(options.className, 'create'))
@@ -166,6 +169,7 @@ const dispatcher = (proto, options, disableSecurity) => {
166
169
  className: options.className,
167
170
  includes: _includes,
168
171
  matches: _matches,
172
+ countMatches: options.countMatches ?? [],
169
173
  objectIdSize: proto[PVK].options.objectIdSize
170
174
  }, _attrs), { lockTable: options.className, retry: true });
171
175
  }
@@ -245,46 +249,6 @@ const dispatcher = (proto, options, disableSecurity) => {
245
249
  };
246
250
  };
247
251
 
248
- //
249
- // proxy.ts
250
- //
251
- // The MIT License
252
- // Copyright (c) 2021 - 2024 O2ter Limited. All rights reserved.
253
- //
254
- // Permission is hereby granted, free of charge, to any person obtaining a copy
255
- // of this software and associated documentation files (the "Software"), to deal
256
- // in the Software without restriction, including without limitation the rights
257
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
258
- // copies of the Software, and to permit persons to whom the Software is
259
- // furnished to do so, subject to the following conditions:
260
- //
261
- // The above copyright notice and this permission notice shall be included in
262
- // all copies or substantial portions of the Software.
263
- //
264
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
265
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
266
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
267
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
268
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
269
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
270
- // THE SOFTWARE.
271
- //
272
- const proxy = (x) => {
273
- const self = x;
274
- const proxy = _.create(self);
275
- for (const name of _.uniq(_.flatMap(prototypes(proxy), x => Object.getOwnPropertyNames(x)))) {
276
- if (name === 'constructor')
277
- continue;
278
- if (_.isFunction(self[name])) {
279
- proxy[name] = self[name].bind(self);
280
- }
281
- else {
282
- Object.defineProperty(proxy, name, { get: () => self[name] });
283
- }
284
- }
285
- return proxy;
286
- };
287
-
288
252
  //
289
253
  // options.ts
290
254
  //
@@ -391,285 +355,43 @@ class _ProtoQuery extends TQuery {
391
355
  });
392
356
  }
393
357
  async insert(attrs, options) {
394
- const context = options?.context ?? {};
395
- const silent = _.castArray(options?.silent ?? []);
396
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[PVK].triggers?.beforeSave?.[this.className];
397
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[PVK].triggers?.afterSave?.[this.className];
398
- const object = this._proto.Object(this.className);
399
- for (const [key, value] of _.toPairs(attrs)) {
400
- object[PVK].mutated[key] = { $set: value };
401
- }
402
- if (_.isFunction(beforeSave))
403
- await beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
404
358
  const result = this._objectMethods(await this._dispatcher(options).insert({
405
359
  className: this.className,
406
360
  includes: this[PVK].options.includes,
407
361
  matches: this[PVK].options.matches,
408
- }, _.fromPairs([...object._set_entries()])));
362
+ countMatches: this[PVK].options.countMatches,
363
+ }, attrs));
409
364
  if (!result)
410
365
  throw Error('Unable to insert document');
411
- if (_.isFunction(afterSave)) {
412
- try {
413
- await afterSave(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
414
- }
415
- catch (e) {
416
- console.error(e);
417
- }
418
- }
419
366
  return result;
420
367
  }
421
368
  async insertMany(values, options) {
422
- const context = options?.context ?? {};
423
- const silent = _.castArray(options?.silent ?? []);
424
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[PVK].triggers?.beforeSave?.[this.className];
425
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[PVK].triggers?.afterSave?.[this.className];
426
- if (_.isFunction(beforeSave) || _.isFunction(afterSave)) {
427
- const objects = _.map(values, attr => {
428
- const object = this._proto.Object(this.className);
429
- for (const [key, value] of _.toPairs(attr)) {
430
- object[PVK].mutated[key] = { $set: value };
431
- }
432
- return object;
433
- });
434
- if (_.isEmpty(objects))
435
- return 0;
436
- if (_.isFunction(beforeSave)) {
437
- await Promise.all(_.map(objects, object => beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
438
- }
439
- await this._dispatcher(options).insertMany({
440
- className: this.className,
441
- includes: this[PVK].options.includes,
442
- matches: this[PVK].options.matches,
443
- }, _.map(objects, x => _.fromPairs([...x._set_entries()])));
444
- if (_.isFunction(afterSave)) {
445
- await Promise.all(_.map(objects, async (object) => {
446
- try {
447
- await afterSave(proxy(Object.setPrototypeOf({ object: object.clone(), context }, options?.session ?? this._proto)));
448
- }
449
- catch (e) {
450
- console.error(e);
451
- }
452
- }));
453
- }
454
- return objects.length;
455
- }
456
369
  return this._dispatcher(options).insertMany({
457
370
  className: this.className,
458
371
  includes: this[PVK].options.includes,
459
372
  matches: this[PVK].options.matches,
373
+ countMatches: this[PVK].options.countMatches,
460
374
  }, values);
461
375
  }
462
376
  async updateOne(update, options) {
463
- const context = options?.context ?? {};
464
- const silent = _.castArray(options?.silent ?? []);
465
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[PVK].triggers?.beforeSave?.[this.className];
466
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[PVK].triggers?.afterSave?.[this.className];
467
- if (_.isFunction(beforeSave)) {
468
- const object = this._objectMethods(_.first(await asyncIterableToArray(this._dispatcher(options).find({ ...this._queryOptions, limit: 1 }))));
469
- if (!object)
470
- return undefined;
471
- object[PVK].mutated = update;
472
- await beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
473
- update = object[PVK].mutated;
474
- }
475
- const result = this._objectMethods(await this._dispatcher(options).updateOne(this._queryOptions, update));
476
- if (result && _.isFunction(afterSave)) {
477
- try {
478
- await afterSave(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
479
- }
480
- catch (e) {
481
- console.error(e);
482
- }
483
- }
484
- return result;
377
+ return this._objectMethods(await this._dispatcher(options).updateOne(this._queryOptions, update));
485
378
  }
486
379
  async updateMany(update, options) {
487
- const context = options?.context ?? {};
488
- const silent = _.castArray(options?.silent ?? []);
489
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[PVK].triggers?.beforeSave?.[this.className];
490
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[PVK].triggers?.afterSave?.[this.className];
491
- if (_.isFunction(beforeSave) || _.isFunction(afterSave)) {
492
- const objects = this._objectMethods(await asyncIterableToArray(this._dispatcher(options).find(this._queryOptions)));
493
- if (_.isEmpty(objects))
494
- return 0;
495
- if (_.isFunction(beforeSave)) {
496
- await Promise.all(_.map(objects, object => beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
497
- }
498
- if (!_.isFunction(afterSave)) {
499
- return this._dispatcher(options).updateMany({
500
- ...this._queryOptions,
501
- filter: { _id: { $in: _.map(objects, x => x.objectId) } },
502
- }, update);
503
- }
504
- const updated = _.compact(await Promise.all(_.map(objects, x => this._dispatcher(options).updateOne({
505
- ...this._queryOptions,
506
- filter: { _id: { $eq: x.objectId } },
507
- }, update))));
508
- await Promise.all(_.map(updated, async (object) => {
509
- try {
510
- await afterSave(proxy(Object.setPrototypeOf({ object: object.clone(), context }, options?.session ?? this._proto)));
511
- }
512
- catch (e) {
513
- console.error(e);
514
- }
515
- }));
516
- return updated.length;
517
- }
518
380
  return this._dispatcher(options).updateMany(this._queryOptions, update);
519
381
  }
520
382
  async upsertOne(update, setOnInsert, options) {
521
- const context = options?.context ?? {};
522
- const silent = _.castArray(options?.silent ?? []);
523
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[PVK].triggers?.beforeSave?.[this.className];
524
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[PVK].triggers?.afterSave?.[this.className];
525
- if (_.isFunction(beforeSave)) {
526
- let object = this._objectMethods(_.first(await asyncIterableToArray(this._dispatcher(options).find({ ...this._queryOptions, limit: 1 }))));
527
- if (object) {
528
- object[PVK].mutated = update;
529
- }
530
- else {
531
- object = this._proto.Object(this.className);
532
- for (const [key, value] of _.toPairs(setOnInsert)) {
533
- object[PVK].mutated[key] = { $set: value };
534
- }
535
- }
536
- await beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
537
- if (object.objectId) {
538
- update = object[PVK].mutated;
539
- }
540
- else {
541
- setOnInsert = {};
542
- for (const [key, update] of _.toPairs(object[PVK].mutated)) {
543
- const [op, value] = decodeUpdateOp(update);
544
- if (op === '$set') {
545
- setOnInsert[key] = value;
546
- }
547
- }
548
- }
549
- }
550
383
  const result = this._objectMethods(await this._dispatcher(options).upsertOne(this._queryOptions, update, setOnInsert));
551
384
  if (!result)
552
385
  throw Error('Unable to upsert document');
553
- if (_.isFunction(afterSave)) {
554
- try {
555
- await afterSave(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
556
- }
557
- catch (e) {
558
- console.error(e);
559
- }
560
- }
561
386
  return result;
562
387
  }
563
388
  async upsertMany(update, setOnInsert, options) {
564
- const context = options?.context ?? {};
565
- const silent = _.castArray(options?.silent ?? []);
566
- const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[PVK].triggers?.beforeSave?.[this.className];
567
- const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[PVK].triggers?.afterSave?.[this.className];
568
- if (_.isFunction(beforeSave) || _.isFunction(afterSave)) {
569
- const objects = this._objectMethods(await asyncIterableToArray(this._dispatcher(options).find(this._queryOptions)));
570
- if (!_.isEmpty(objects) && _.isFunction(beforeSave)) {
571
- await Promise.all(_.map(objects, object => beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
572
- }
573
- if (_.isEmpty(objects)) {
574
- const result = await this._dispatcher(options).insert({
575
- className: this.className,
576
- includes: this[PVK].options.includes,
577
- matches: this[PVK].options.matches,
578
- }, setOnInsert);
579
- if (!result)
580
- throw Error('Unable to insert document');
581
- if (_.isFunction(afterSave)) {
582
- try {
583
- await afterSave(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
584
- }
585
- catch (e) {
586
- console.error(e);
587
- }
588
- }
589
- return { updated: 0, inserted: 1 };
590
- }
591
- if (!_.isFunction(afterSave)) {
592
- return {
593
- inserted: 0,
594
- updated: await this._dispatcher(options).updateMany({
595
- ...this._queryOptions,
596
- filter: { _id: { $in: _.map(objects, x => x.objectId) } },
597
- }, update),
598
- };
599
- }
600
- const updated = _.compact(await Promise.all(_.map(objects, x => this._dispatcher(options).updateOne({
601
- ...this._queryOptions,
602
- filter: { _id: { $eq: x.objectId } },
603
- }, update))));
604
- await Promise.all(_.map(updated, async (object) => {
605
- try {
606
- await afterSave(proxy(Object.setPrototypeOf({ object: object.clone(), context }, options?.session ?? this._proto)));
607
- }
608
- catch (e) {
609
- console.error(e);
610
- }
611
- }));
612
- return { updated: updated.length, inserted: 0 };
613
- }
614
389
  return this._dispatcher(options).upsertMany(this._queryOptions, update, setOnInsert);
615
390
  }
616
391
  async deleteOne(options) {
617
- const context = options?.context ?? {};
618
- const silent = _.castArray(options?.silent ?? []);
619
- const beforeDelete = _.includes(silent, 'beforeDelete') ? null : this._proto[PVK].triggers?.beforeDelete?.[this.className];
620
- const afterDelete = _.includes(silent, 'afterDelete') ? null : this._proto[PVK].triggers?.afterDelete?.[this.className];
621
- let result;
622
- if (_.isFunction(beforeDelete)) {
623
- const object = this._objectMethods(_.first(await asyncIterableToArray(this._dispatcher(options).find({ ...this._queryOptions, limit: 1 }))));
624
- if (!object)
625
- return undefined;
626
- await beforeDelete(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
627
- result = this._objectMethods(await this._dispatcher(options).deleteOne({
628
- ...this._queryOptions,
629
- filter: { _id: { $eq: object.objectId } },
630
- }));
631
- }
632
- else {
633
- result = this._objectMethods(await this._dispatcher(options).deleteOne(this._queryOptions));
634
- }
635
- if (result && _.isFunction(afterDelete)) {
636
- try {
637
- await afterDelete(proxy(Object.setPrototypeOf({ object: result.clone(), context }, options?.session ?? this._proto)));
638
- }
639
- catch (e) {
640
- console.error(e);
641
- }
642
- }
643
- return result;
392
+ return this._objectMethods(await this._dispatcher(options).deleteOne(this._queryOptions));
644
393
  }
645
394
  async deleteMany(options) {
646
- const context = options?.context ?? {};
647
- const silent = _.castArray(options?.silent ?? []);
648
- const beforeDelete = _.includes(silent, 'beforeDelete') ? null : this._proto[PVK].triggers?.beforeDelete?.[this.className];
649
- const afterDelete = _.includes(silent, 'afterDelete') ? null : this._proto[PVK].triggers?.afterDelete?.[this.className];
650
- if (_.isFunction(beforeDelete) || _.isFunction(afterDelete)) {
651
- const objects = this._objectMethods(await asyncIterableToArray(this._dispatcher(options).find(this._queryOptions)));
652
- if (_.isEmpty(objects))
653
- return 0;
654
- if (_.isFunction(beforeDelete)) {
655
- await Promise.all(_.map(objects, object => beforeDelete(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
656
- }
657
- await this._dispatcher(options).deleteMany({
658
- ...this._queryOptions,
659
- filter: { _id: { $in: _.map(objects, x => x.objectId) } },
660
- });
661
- if (_.isFunction(afterDelete)) {
662
- await Promise.all(_.map(objects, async (object) => {
663
- try {
664
- await afterDelete(proxy(Object.setPrototypeOf({ object: object.clone(), context }, options?.session ?? this._proto)));
665
- }
666
- catch (e) {
667
- console.error(e);
668
- }
669
- }));
670
- }
671
- return objects.length;
672
- }
673
395
  return this._dispatcher(options).deleteMany(this._queryOptions);
674
396
  }
675
397
  }
@@ -870,6 +592,46 @@ const varifyPassword = async (alg, password, options) => {
870
592
  }
871
593
  };
872
594
 
595
+ //
596
+ // proxy.ts
597
+ //
598
+ // The MIT License
599
+ // Copyright (c) 2021 - 2024 O2ter Limited. All rights reserved.
600
+ //
601
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
602
+ // of this software and associated documentation files (the "Software"), to deal
603
+ // in the Software without restriction, including without limitation the rights
604
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
605
+ // copies of the Software, and to permit persons to whom the Software is
606
+ // furnished to do so, subject to the following conditions:
607
+ //
608
+ // The above copyright notice and this permission notice shall be included in
609
+ // all copies or substantial portions of the Software.
610
+ //
611
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
612
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
613
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
614
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
615
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
616
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
617
+ // THE SOFTWARE.
618
+ //
619
+ const proxy = (x) => {
620
+ const self = x;
621
+ const proxy = _.create(self);
622
+ for (const name of _.uniq(_.flatMap(prototypes(proxy), x => Object.getOwnPropertyNames(x)))) {
623
+ if (name === 'constructor')
624
+ continue;
625
+ if (_.isFunction(self[name])) {
626
+ proxy[name] = self[name].bind(self);
627
+ }
628
+ else {
629
+ Object.defineProperty(proxy, name, { get: () => self[name] });
630
+ }
631
+ }
632
+ return proxy;
633
+ };
634
+
873
635
  //
874
636
  // internal.ts
875
637
  //
@@ -990,7 +752,6 @@ const mergeSchema = (...schemas) => _.reduce(schemas, (acc, schema) => ({
990
752
  class ProtoInternal {
991
753
  options;
992
754
  functions = {};
993
- triggers = {};
994
755
  constructor(options) {
995
756
  validateSchemaName(options.schema);
996
757
  const schema = mergeSchema(defaultSchema, options.fileStorage.schema, options.schema);
@@ -1667,26 +1428,6 @@ class ProtoService extends ProtoType {
1667
1428
  define(name, callback, options) {
1668
1429
  this[PVK].functions[name] = options ? { callback, ...options } : callback;
1669
1430
  }
1670
- beforeSave(name, callback) {
1671
- if (!this[PVK].triggers.beforeSave)
1672
- this[PVK].triggers.beforeSave = {};
1673
- this[PVK].triggers.beforeSave[name] = callback;
1674
- }
1675
- afterSave(name, callback) {
1676
- if (!this[PVK].triggers.afterSave)
1677
- this[PVK].triggers.afterSave = {};
1678
- this[PVK].triggers.afterSave[name] = callback;
1679
- }
1680
- beforeDelete(name, callback) {
1681
- if (!this[PVK].triggers.beforeDelete)
1682
- this[PVK].triggers.beforeDelete = {};
1683
- this[PVK].triggers.beforeDelete[name] = callback;
1684
- }
1685
- afterDelete(name, callback) {
1686
- if (!this[PVK].triggers.afterDelete)
1687
- this[PVK].triggers.afterDelete = {};
1688
- this[PVK].triggers.afterDelete[name] = callback;
1689
- }
1690
1431
  lockTable(className, update) {
1691
1432
  return this.storage.lockTable(className, update);
1692
1433
  }
@@ -1724,12 +1465,14 @@ class ProtoService extends ProtoType {
1724
1465
  });
1725
1466
  }
1726
1467
  async gc(classNames) {
1468
+ const time = new Date();
1727
1469
  for (const className of _.castArray(classNames ?? this.classes())) {
1728
1470
  if (className === 'File') {
1729
1471
  const found = this.storage.find({
1730
1472
  className: 'File',
1731
- filter: QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1473
+ filter: QuerySelector.decode({ _expired_at: { $lt: time } }),
1732
1474
  matches: {},
1475
+ countMatches: [],
1733
1476
  includes: ['_id', '_expired_at', 'token'],
1734
1477
  objectIdSize: 0
1735
1478
  });
@@ -1741,9 +1484,10 @@ class ProtoService extends ProtoType {
1741
1484
  }
1742
1485
  await this.storage.deleteMany({
1743
1486
  className,
1744
- filter: QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1487
+ filter: QuerySelector.decode({ _expired_at: { $lt: time } }),
1745
1488
  includes: ['_id', '_expired_at'],
1746
1489
  matches: {},
1490
+ countMatches: [],
1747
1491
  objectIdSize: 0
1748
1492
  });
1749
1493
  }
@@ -1883,12 +1627,12 @@ const verifyRelatedBy = (relatedBy) => {
1883
1627
  var classesRoute = (router, proto) => {
1884
1628
  const defaultHandler = async (req) => {
1885
1629
  const { name } = req.params;
1886
- const { operation, context, silent, random, attributes, update, setOnInsert, relatedBy, ...options } = deserialize(req.body);
1630
+ const { operation, random, attributes, update, setOnInsert, relatedBy, ...options } = deserialize(req.body);
1887
1631
  verifyRelatedBy(relatedBy);
1888
1632
  const payload = proto.connect(req);
1889
1633
  const query = relatedBy ? payload.Relation(payload.Object(relatedBy.className, relatedBy.objectId), relatedBy.key) : payload.Query(name);
1890
1634
  query[PVK].options = options;
1891
- const opts = { master: payload.isMaster, context, silent };
1635
+ const opts = { master: payload.isMaster };
1892
1636
  switch (operation) {
1893
1637
  case 'explain':
1894
1638
  if (!payload.isMaster)