proto.io 0.0.173 → 0.0.175
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.
- package/dist/adapters/file/database.d.ts +2 -2
- package/dist/adapters/file/database.js +2 -2
- package/dist/adapters/file/database.mjs +2 -2
- package/dist/adapters/file/filesystem.d.ts +2 -2
- package/dist/adapters/file/google-cloud-storage.d.ts +2 -2
- package/dist/adapters/storage/progres.d.ts +2 -2
- package/dist/adapters/storage/progres.js +3 -3
- package/dist/adapters/storage/progres.js.map +1 -1
- package/dist/adapters/storage/progres.mjs +3 -3
- package/dist/adapters/storage/progres.mjs.map +1 -1
- package/dist/client.d.ts +3 -3
- package/dist/client.js +2 -2
- package/dist/client.mjs +3 -3
- package/dist/index.d.ts +75 -3
- package/dist/index.js +121 -261
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +123 -263
- package/dist/index.mjs.map +1 -1
- package/dist/internals/{index-Cpv1DoEI.d.ts → index-B9CWU0y7.d.ts} +32 -2
- package/dist/internals/index-B9CWU0y7.d.ts.map +1 -0
- package/dist/internals/{index-BZNPlw1L.mjs → index-BPbXumUi.mjs} +286 -44
- package/dist/internals/index-BPbXumUi.mjs.map +1 -0
- package/dist/internals/{index-BJnQhKf3.d.ts → index-BQqYdjaH.d.ts} +2 -2
- package/dist/internals/index-BQqYdjaH.d.ts.map +1 -0
- package/dist/internals/{index-lX-M76Tn.d.ts → index-BYZpj31o.d.ts} +1062 -33
- package/dist/internals/index-BYZpj31o.d.ts.map +1 -0
- package/dist/internals/{index-BYbMU-Ao.mjs → index-CTsc042s.mjs} +140 -2
- package/dist/internals/index-CTsc042s.mjs.map +1 -0
- package/dist/internals/{index-CIecB6mS.js → index-CvA0tbwx.js} +286 -44
- package/dist/internals/index-CvA0tbwx.js.map +1 -0
- package/dist/internals/{index-B1wqSio6.mjs → index-DHQhYGsJ.mjs} +2 -2
- package/dist/internals/{index-B1wqSio6.mjs.map → index-DHQhYGsJ.mjs.map} +1 -1
- package/dist/internals/{index-CVutVPmd.js → index-DXuW8UiB.js} +139 -1
- package/dist/internals/index-DXuW8UiB.js.map +1 -0
- package/dist/internals/{index-CzfsyXvb.js → index-Dc3V_Bzw.js} +2 -2
- package/dist/internals/{index-CzfsyXvb.js.map → index-Dc3V_Bzw.js.map} +1 -1
- package/dist/internals/{random-BCpwYpyw.mjs → random-BMQpRlGH.mjs} +3 -3
- package/dist/internals/{random-BCpwYpyw.mjs.map → random-BMQpRlGH.mjs.map} +1 -1
- package/dist/internals/{random-Dytum6Nh.js → random-DVOUDDGg.js} +3 -3
- package/dist/internals/{random-Dytum6Nh.js.map → random-DVOUDDGg.js.map} +1 -1
- package/package.json +1 -1
- package/dist/internals/index-BJnQhKf3.d.ts.map +0 -1
- package/dist/internals/index-BYbMU-Ao.mjs.map +0 -1
- package/dist/internals/index-BZNPlw1L.mjs.map +0 -1
- package/dist/internals/index-CIecB6mS.js.map +0 -1
- package/dist/internals/index-CVutVPmd.js.map +0 -1
- package/dist/internals/index-Cpv1DoEI.d.ts.map +0 -1
- package/dist/internals/index-lX-M76Tn.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -4,18 +4,18 @@ 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-
|
|
7
|
+
var random = require('./internals/random-DVOUDDGg.js');
|
|
8
8
|
var _private = require('./internals/private-CSB1Ep4g.js');
|
|
9
9
|
var utilsJs = require('@o2ter/utils-js');
|
|
10
|
-
var index = require('./internals/index-
|
|
11
|
-
var index$1 = require('./internals/index-
|
|
10
|
+
var index = require('./internals/index-CvA0tbwx.js');
|
|
11
|
+
var index$1 = require('./internals/index-DXuW8UiB.js');
|
|
12
12
|
var jwt = require('jsonwebtoken');
|
|
13
13
|
var node_buffer = require('node:buffer');
|
|
14
14
|
var node_stream = require('node:stream');
|
|
15
15
|
var node_crypto = require('node:crypto');
|
|
16
16
|
var util = require('util');
|
|
17
17
|
var cryptoJs = require('@o2ter/crypto-js');
|
|
18
|
-
var index$2 = require('./internals/index-
|
|
18
|
+
var index$2 = require('./internals/index-Dc3V_Bzw.js');
|
|
19
19
|
var queryType = require('query-types');
|
|
20
20
|
var busboy = require('busboy');
|
|
21
21
|
var Decimal = require('decimal.js');
|
|
@@ -248,46 +248,6 @@ const dispatcher = (proto, options, disableSecurity) => {
|
|
|
248
248
|
};
|
|
249
249
|
};
|
|
250
250
|
|
|
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
251
|
//
|
|
292
252
|
// options.ts
|
|
293
253
|
//
|
|
@@ -394,56 +354,16 @@ class _ProtoQuery extends index.TQuery {
|
|
|
394
354
|
});
|
|
395
355
|
}
|
|
396
356
|
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
357
|
const result = this._objectMethods(await this._dispatcher(options).insert({
|
|
408
358
|
className: this.className,
|
|
409
359
|
includes: this[_private.PVK].options.includes,
|
|
410
360
|
matches: this[_private.PVK].options.matches,
|
|
411
|
-
},
|
|
361
|
+
}, attrs));
|
|
412
362
|
if (!result)
|
|
413
363
|
throw Error('Unable to insert document');
|
|
414
|
-
if (_.isFunction(afterSave)) {
|
|
415
|
-
await afterSave(proxy(Object.setPrototypeOf({ object: result, context }, options?.session ?? this._proto)));
|
|
416
|
-
}
|
|
417
364
|
return result;
|
|
418
365
|
}
|
|
419
366
|
async insertMany(values, options) {
|
|
420
|
-
const context = options?.context ?? {};
|
|
421
|
-
const silent = _.castArray(options?.silent ?? []);
|
|
422
|
-
const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
|
|
423
|
-
const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
|
|
424
|
-
if (_.isFunction(beforeSave) || _.isFunction(afterSave)) {
|
|
425
|
-
const objects = _.map(values, attr => {
|
|
426
|
-
const object = this._proto.Object(this.className);
|
|
427
|
-
for (const [key, value] of _.toPairs(attr)) {
|
|
428
|
-
object[_private.PVK].mutated[key] = { $set: value };
|
|
429
|
-
}
|
|
430
|
-
return object;
|
|
431
|
-
});
|
|
432
|
-
if (_.isEmpty(objects))
|
|
433
|
-
return 0;
|
|
434
|
-
if (_.isFunction(beforeSave)) {
|
|
435
|
-
await Promise.all(_.map(objects, object => beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
|
|
436
|
-
}
|
|
437
|
-
await this._dispatcher(options).insertMany({
|
|
438
|
-
className: this.className,
|
|
439
|
-
includes: this[_private.PVK].options.includes,
|
|
440
|
-
matches: this[_private.PVK].options.matches,
|
|
441
|
-
}, _.map(objects, x => _.fromPairs([...x._set_entries()])));
|
|
442
|
-
if (_.isFunction(afterSave)) {
|
|
443
|
-
await Promise.all(_.map(objects, object => afterSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
|
|
444
|
-
}
|
|
445
|
-
return objects.length;
|
|
446
|
-
}
|
|
447
367
|
return this._dispatcher(options).insertMany({
|
|
448
368
|
className: this.className,
|
|
449
369
|
includes: this[_private.PVK].options.includes,
|
|
@@ -451,175 +371,24 @@ class _ProtoQuery extends index.TQuery {
|
|
|
451
371
|
}, values);
|
|
452
372
|
}
|
|
453
373
|
async updateOne(update, options) {
|
|
454
|
-
|
|
455
|
-
const silent = _.castArray(options?.silent ?? []);
|
|
456
|
-
const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
|
|
457
|
-
const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
|
|
458
|
-
if (_.isFunction(beforeSave)) {
|
|
459
|
-
const object = this._objectMethods(_.first(await utilsJs.asyncIterableToArray(this._dispatcher(options).find({ ...this._queryOptions, limit: 1 }))));
|
|
460
|
-
if (!object)
|
|
461
|
-
return undefined;
|
|
462
|
-
object[_private.PVK].mutated = update;
|
|
463
|
-
await beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
|
|
464
|
-
update = object[_private.PVK].mutated;
|
|
465
|
-
}
|
|
466
|
-
const result = this._objectMethods(await this._dispatcher(options).updateOne(this._queryOptions, update));
|
|
467
|
-
if (result && _.isFunction(afterSave)) {
|
|
468
|
-
await afterSave(proxy(Object.setPrototypeOf({ object: result, context }, options?.session ?? this._proto)));
|
|
469
|
-
}
|
|
470
|
-
return result;
|
|
374
|
+
return this._objectMethods(await this._dispatcher(options).updateOne(this._queryOptions, update));
|
|
471
375
|
}
|
|
472
376
|
async updateMany(update, options) {
|
|
473
|
-
const context = options?.context ?? {};
|
|
474
|
-
const silent = _.castArray(options?.silent ?? []);
|
|
475
|
-
const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
|
|
476
|
-
const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
|
|
477
|
-
if (_.isFunction(beforeSave) || _.isFunction(afterSave)) {
|
|
478
|
-
const objects = this._objectMethods(await utilsJs.asyncIterableToArray(this._dispatcher(options).find(this._queryOptions)));
|
|
479
|
-
if (_.isEmpty(objects))
|
|
480
|
-
return 0;
|
|
481
|
-
if (_.isFunction(beforeSave)) {
|
|
482
|
-
await Promise.all(_.map(objects, object => beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
|
|
483
|
-
}
|
|
484
|
-
if (!_.isFunction(afterSave)) {
|
|
485
|
-
return this._dispatcher(options).updateMany({
|
|
486
|
-
...this._queryOptions,
|
|
487
|
-
filter: { _id: { $in: _.map(objects, x => x.objectId) } },
|
|
488
|
-
}, update);
|
|
489
|
-
}
|
|
490
|
-
const updated = await Promise.all(_.map(objects, x => this._dispatcher(options).updateOne({
|
|
491
|
-
...this._queryOptions,
|
|
492
|
-
filter: { _id: { $eq: x.objectId } },
|
|
493
|
-
}, update)));
|
|
494
|
-
await Promise.all(_.map(updated, object => afterSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
|
|
495
|
-
return updated.length;
|
|
496
|
-
}
|
|
497
377
|
return this._dispatcher(options).updateMany(this._queryOptions, update);
|
|
498
378
|
}
|
|
499
379
|
async upsertOne(update, setOnInsert, options) {
|
|
500
|
-
const context = options?.context ?? {};
|
|
501
|
-
const silent = _.castArray(options?.silent ?? []);
|
|
502
|
-
const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
|
|
503
|
-
const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
|
|
504
|
-
if (_.isFunction(beforeSave)) {
|
|
505
|
-
let object = this._objectMethods(_.first(await utilsJs.asyncIterableToArray(this._dispatcher(options).find({ ...this._queryOptions, limit: 1 }))));
|
|
506
|
-
if (object) {
|
|
507
|
-
object[_private.PVK].mutated = update;
|
|
508
|
-
}
|
|
509
|
-
else {
|
|
510
|
-
object = this._proto.Object(this.className);
|
|
511
|
-
for (const [key, value] of _.toPairs(setOnInsert)) {
|
|
512
|
-
object[_private.PVK].mutated[key] = { $set: value };
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
await beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
|
|
516
|
-
if (object.objectId) {
|
|
517
|
-
update = object[_private.PVK].mutated;
|
|
518
|
-
}
|
|
519
|
-
else {
|
|
520
|
-
setOnInsert = {};
|
|
521
|
-
for (const [key, update] of _.toPairs(object[_private.PVK].mutated)) {
|
|
522
|
-
const [op, value] = index$1.decodeUpdateOp(update);
|
|
523
|
-
if (op === '$set') {
|
|
524
|
-
setOnInsert[key] = value;
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
380
|
const result = this._objectMethods(await this._dispatcher(options).upsertOne(this._queryOptions, update, setOnInsert));
|
|
530
381
|
if (!result)
|
|
531
382
|
throw Error('Unable to upsert document');
|
|
532
|
-
if (_.isFunction(afterSave)) {
|
|
533
|
-
await afterSave(proxy(Object.setPrototypeOf({ object: result, context }, options?.session ?? this._proto)));
|
|
534
|
-
}
|
|
535
383
|
return result;
|
|
536
384
|
}
|
|
537
385
|
async upsertMany(update, setOnInsert, options) {
|
|
538
|
-
const context = options?.context ?? {};
|
|
539
|
-
const silent = _.castArray(options?.silent ?? []);
|
|
540
|
-
const beforeSave = _.includes(silent, 'beforeSave') ? null : this._proto[_private.PVK].triggers?.beforeSave?.[this.className];
|
|
541
|
-
const afterSave = _.includes(silent, 'afterSave') ? null : this._proto[_private.PVK].triggers?.afterSave?.[this.className];
|
|
542
|
-
if (_.isFunction(beforeSave) || _.isFunction(afterSave)) {
|
|
543
|
-
const objects = this._objectMethods(await utilsJs.asyncIterableToArray(this._dispatcher(options).find(this._queryOptions)));
|
|
544
|
-
if (!_.isEmpty(objects) && _.isFunction(beforeSave)) {
|
|
545
|
-
await Promise.all(_.map(objects, object => beforeSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
|
|
546
|
-
}
|
|
547
|
-
if (_.isEmpty(objects)) {
|
|
548
|
-
const result = await this._dispatcher(options).insert({
|
|
549
|
-
className: this.className,
|
|
550
|
-
includes: this[_private.PVK].options.includes,
|
|
551
|
-
matches: this[_private.PVK].options.matches,
|
|
552
|
-
}, setOnInsert);
|
|
553
|
-
if (!result)
|
|
554
|
-
throw Error('Unable to insert document');
|
|
555
|
-
if (_.isFunction(afterSave)) {
|
|
556
|
-
await afterSave(proxy(Object.setPrototypeOf({ object: result, context }, options?.session ?? this._proto)));
|
|
557
|
-
}
|
|
558
|
-
return { updated: 0, inserted: 1 };
|
|
559
|
-
}
|
|
560
|
-
if (!_.isFunction(afterSave)) {
|
|
561
|
-
return {
|
|
562
|
-
inserted: 0,
|
|
563
|
-
updated: await this._dispatcher(options).updateMany({
|
|
564
|
-
...this._queryOptions,
|
|
565
|
-
filter: { _id: { $in: _.map(objects, x => x.objectId) } },
|
|
566
|
-
}, update),
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
const updated = await Promise.all(_.map(objects, x => this._dispatcher(options).updateOne({
|
|
570
|
-
...this._queryOptions,
|
|
571
|
-
filter: { _id: { $eq: x.objectId } },
|
|
572
|
-
}, update)));
|
|
573
|
-
await Promise.all(_.map(updated, object => afterSave(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
|
|
574
|
-
return { updated: updated.length, inserted: 0 };
|
|
575
|
-
}
|
|
576
386
|
return this._dispatcher(options).upsertMany(this._queryOptions, update, setOnInsert);
|
|
577
387
|
}
|
|
578
388
|
async deleteOne(options) {
|
|
579
|
-
|
|
580
|
-
const silent = _.castArray(options?.silent ?? []);
|
|
581
|
-
const beforeDelete = _.includes(silent, 'beforeDelete') ? null : this._proto[_private.PVK].triggers?.beforeDelete?.[this.className];
|
|
582
|
-
const afterDelete = _.includes(silent, 'afterDelete') ? null : this._proto[_private.PVK].triggers?.afterDelete?.[this.className];
|
|
583
|
-
let result;
|
|
584
|
-
if (_.isFunction(beforeDelete)) {
|
|
585
|
-
const object = this._objectMethods(_.first(await utilsJs.asyncIterableToArray(this._dispatcher(options).find({ ...this._queryOptions, limit: 1 }))));
|
|
586
|
-
if (!object)
|
|
587
|
-
return undefined;
|
|
588
|
-
await beforeDelete(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)));
|
|
589
|
-
result = this._objectMethods(await this._dispatcher(options).deleteOne({
|
|
590
|
-
...this._queryOptions,
|
|
591
|
-
filter: { _id: { $eq: object.objectId } },
|
|
592
|
-
}));
|
|
593
|
-
}
|
|
594
|
-
else {
|
|
595
|
-
result = this._objectMethods(await this._dispatcher(options).deleteOne(this._queryOptions));
|
|
596
|
-
}
|
|
597
|
-
if (result && _.isFunction(afterDelete)) {
|
|
598
|
-
await afterDelete(proxy(Object.setPrototypeOf({ object: result, context }, options?.session ?? this._proto)));
|
|
599
|
-
}
|
|
600
|
-
return result;
|
|
389
|
+
return this._objectMethods(await this._dispatcher(options).deleteOne(this._queryOptions));
|
|
601
390
|
}
|
|
602
391
|
async deleteMany(options) {
|
|
603
|
-
const context = options?.context ?? {};
|
|
604
|
-
const silent = _.castArray(options?.silent ?? []);
|
|
605
|
-
const beforeDelete = _.includes(silent, 'beforeDelete') ? null : this._proto[_private.PVK].triggers?.beforeDelete?.[this.className];
|
|
606
|
-
const afterDelete = _.includes(silent, 'afterDelete') ? null : this._proto[_private.PVK].triggers?.afterDelete?.[this.className];
|
|
607
|
-
if (_.isFunction(beforeDelete) || _.isFunction(afterDelete)) {
|
|
608
|
-
const objects = this._objectMethods(await utilsJs.asyncIterableToArray(this._dispatcher(options).find(this._queryOptions)));
|
|
609
|
-
if (_.isEmpty(objects))
|
|
610
|
-
return 0;
|
|
611
|
-
if (_.isFunction(beforeDelete)) {
|
|
612
|
-
await Promise.all(_.map(objects, object => beforeDelete(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
|
|
613
|
-
}
|
|
614
|
-
await this._dispatcher(options).deleteMany({
|
|
615
|
-
...this._queryOptions,
|
|
616
|
-
filter: { _id: { $in: _.map(objects, x => x.objectId) } },
|
|
617
|
-
});
|
|
618
|
-
if (_.isFunction(afterDelete)) {
|
|
619
|
-
await Promise.all(_.map(objects, object => afterDelete(proxy(Object.setPrototypeOf({ object, context }, options?.session ?? this._proto)))));
|
|
620
|
-
}
|
|
621
|
-
return objects.length;
|
|
622
|
-
}
|
|
623
392
|
return this._dispatcher(options).deleteMany(this._queryOptions);
|
|
624
393
|
}
|
|
625
394
|
}
|
|
@@ -820,6 +589,46 @@ const varifyPassword = async (alg, password, options) => {
|
|
|
820
589
|
}
|
|
821
590
|
};
|
|
822
591
|
|
|
592
|
+
//
|
|
593
|
+
// proxy.ts
|
|
594
|
+
//
|
|
595
|
+
// The MIT License
|
|
596
|
+
// Copyright (c) 2021 - 2024 O2ter Limited. All rights reserved.
|
|
597
|
+
//
|
|
598
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
599
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
600
|
+
// in the Software without restriction, including without limitation the rights
|
|
601
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
602
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
603
|
+
// furnished to do so, subject to the following conditions:
|
|
604
|
+
//
|
|
605
|
+
// The above copyright notice and this permission notice shall be included in
|
|
606
|
+
// all copies or substantial portions of the Software.
|
|
607
|
+
//
|
|
608
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
609
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
610
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
611
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
612
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
613
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
614
|
+
// THE SOFTWARE.
|
|
615
|
+
//
|
|
616
|
+
const proxy = (x) => {
|
|
617
|
+
const self = x;
|
|
618
|
+
const proxy = _.create(self);
|
|
619
|
+
for (const name of _.uniq(_.flatMap(utilsJs.prototypes(proxy), x => Object.getOwnPropertyNames(x)))) {
|
|
620
|
+
if (name === 'constructor')
|
|
621
|
+
continue;
|
|
622
|
+
if (_.isFunction(self[name])) {
|
|
623
|
+
proxy[name] = self[name].bind(self);
|
|
624
|
+
}
|
|
625
|
+
else {
|
|
626
|
+
Object.defineProperty(proxy, name, { get: () => self[name] });
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return proxy;
|
|
630
|
+
};
|
|
631
|
+
|
|
823
632
|
//
|
|
824
633
|
// internal.ts
|
|
825
634
|
//
|
|
@@ -940,7 +749,6 @@ const mergeSchema = (...schemas) => _.reduce(schemas, (acc, schema) => ({
|
|
|
940
749
|
class ProtoInternal {
|
|
941
750
|
options;
|
|
942
751
|
functions = {};
|
|
943
|
-
triggers = {};
|
|
944
752
|
constructor(options) {
|
|
945
753
|
validateSchemaName(options.schema);
|
|
946
754
|
const schema = mergeSchema(defaultSchema, options.fileStorage.schema, options.schema);
|
|
@@ -1617,26 +1425,6 @@ class ProtoService extends index.ProtoType {
|
|
|
1617
1425
|
define(name, callback, options) {
|
|
1618
1426
|
this[_private.PVK].functions[name] = options ? { callback, ...options } : callback;
|
|
1619
1427
|
}
|
|
1620
|
-
beforeSave(name, callback) {
|
|
1621
|
-
if (!this[_private.PVK].triggers.beforeSave)
|
|
1622
|
-
this[_private.PVK].triggers.beforeSave = {};
|
|
1623
|
-
this[_private.PVK].triggers.beforeSave[name] = callback;
|
|
1624
|
-
}
|
|
1625
|
-
afterSave(name, callback) {
|
|
1626
|
-
if (!this[_private.PVK].triggers.afterSave)
|
|
1627
|
-
this[_private.PVK].triggers.afterSave = {};
|
|
1628
|
-
this[_private.PVK].triggers.afterSave[name] = callback;
|
|
1629
|
-
}
|
|
1630
|
-
beforeDelete(name, callback) {
|
|
1631
|
-
if (!this[_private.PVK].triggers.beforeDelete)
|
|
1632
|
-
this[_private.PVK].triggers.beforeDelete = {};
|
|
1633
|
-
this[_private.PVK].triggers.beforeDelete[name] = callback;
|
|
1634
|
-
}
|
|
1635
|
-
afterDelete(name, callback) {
|
|
1636
|
-
if (!this[_private.PVK].triggers.afterDelete)
|
|
1637
|
-
this[_private.PVK].triggers.afterDelete = {};
|
|
1638
|
-
this[_private.PVK].triggers.afterDelete[name] = callback;
|
|
1639
|
-
}
|
|
1640
1428
|
lockTable(className, update) {
|
|
1641
1429
|
return this.storage.lockTable(className, update);
|
|
1642
1430
|
}
|
|
@@ -1833,12 +1621,12 @@ const verifyRelatedBy = (relatedBy) => {
|
|
|
1833
1621
|
var classesRoute = (router, proto) => {
|
|
1834
1622
|
const defaultHandler = async (req) => {
|
|
1835
1623
|
const { name } = req.params;
|
|
1836
|
-
const { operation,
|
|
1624
|
+
const { operation, random, attributes, update, setOnInsert, relatedBy, ...options } = index.deserialize(req.body);
|
|
1837
1625
|
verifyRelatedBy(relatedBy);
|
|
1838
1626
|
const payload = proto.connect(req);
|
|
1839
1627
|
const query = relatedBy ? payload.Relation(payload.Object(relatedBy.className, relatedBy.objectId), relatedBy.key) : payload.Query(name);
|
|
1840
1628
|
query[_private.PVK].options = options;
|
|
1841
|
-
const opts = { master: payload.isMaster
|
|
1629
|
+
const opts = { master: payload.isMaster };
|
|
1842
1630
|
switch (operation) {
|
|
1843
1631
|
case 'explain':
|
|
1844
1632
|
if (!payload.isMaster)
|
|
@@ -2379,19 +2167,84 @@ var configRoute = (router, proto) => {
|
|
|
2379
2167
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
2380
2168
|
// THE SOFTWARE.
|
|
2381
2169
|
//
|
|
2170
|
+
/**
|
|
2171
|
+
* Schema definition utility functions.
|
|
2172
|
+
*/
|
|
2382
2173
|
const schema = _.assign((x) => x, {
|
|
2174
|
+
/**
|
|
2175
|
+
* Defines a boolean schema.
|
|
2176
|
+
* @param defaultValue - The default value for the boolean.
|
|
2177
|
+
* @returns The boolean schema.
|
|
2178
|
+
*/
|
|
2383
2179
|
boolean: (defaultValue) => ({ type: 'boolean', default: defaultValue }),
|
|
2180
|
+
/**
|
|
2181
|
+
* Defines a number schema.
|
|
2182
|
+
* @param defaultValue - The default value for the number.
|
|
2183
|
+
* @returns The number schema.
|
|
2184
|
+
*/
|
|
2384
2185
|
number: (defaultValue) => ({ type: 'number', default: defaultValue }),
|
|
2186
|
+
/**
|
|
2187
|
+
* Defines a decimal schema.
|
|
2188
|
+
* @param defaultValue - The default value for the decimal.
|
|
2189
|
+
* @returns The decimal schema.
|
|
2190
|
+
*/
|
|
2385
2191
|
decimal: (defaultValue) => ({ type: 'decimal', default: defaultValue }),
|
|
2192
|
+
/**
|
|
2193
|
+
* Defines a string schema.
|
|
2194
|
+
* @param defaultValue - The default value for the string.
|
|
2195
|
+
* @returns The string schema.
|
|
2196
|
+
*/
|
|
2386
2197
|
string: (defaultValue) => ({ type: 'string', default: defaultValue }),
|
|
2198
|
+
/**
|
|
2199
|
+
* Defines a date schema.
|
|
2200
|
+
* @param defaultValue - The default value for the date.
|
|
2201
|
+
* @returns The date schema.
|
|
2202
|
+
*/
|
|
2387
2203
|
date: (defaultValue) => ({ type: 'date', default: defaultValue }),
|
|
2204
|
+
/**
|
|
2205
|
+
* Defines an object schema.
|
|
2206
|
+
* @param defaultValue - The default value for the object.
|
|
2207
|
+
* @returns The object schema.
|
|
2208
|
+
*/
|
|
2388
2209
|
object: (defaultValue) => ({ type: 'object', default: defaultValue }),
|
|
2210
|
+
/**
|
|
2211
|
+
* Defines an array schema.
|
|
2212
|
+
* @param defaultValue - The default value for the array.
|
|
2213
|
+
* @returns The array schema.
|
|
2214
|
+
*/
|
|
2389
2215
|
array: (defaultValue) => ({ type: 'array', default: defaultValue }),
|
|
2216
|
+
/**
|
|
2217
|
+
* Defines a vector schema.
|
|
2218
|
+
* @param dimension - The dimension of the vector.
|
|
2219
|
+
* @param defaultValue - The default value for the vector.
|
|
2220
|
+
* @returns The vector schema.
|
|
2221
|
+
*/
|
|
2390
2222
|
vector: (dimension, defaultValue) => ({ type: 'vector', dimension, default: defaultValue }),
|
|
2223
|
+
/**
|
|
2224
|
+
* Defines a shape schema.
|
|
2225
|
+
* @param shape - The shape definition.
|
|
2226
|
+
* @returns The shape schema.
|
|
2227
|
+
*/
|
|
2391
2228
|
shape: (shape) => ({ type: 'shape', shape }),
|
|
2229
|
+
/**
|
|
2230
|
+
* Defines a pointer schema.
|
|
2231
|
+
* @param target - The target of the pointer.
|
|
2232
|
+
* @returns The pointer schema.
|
|
2233
|
+
*/
|
|
2392
2234
|
pointer: (target) => ({ type: 'pointer', target }),
|
|
2235
|
+
/**
|
|
2236
|
+
* Defines a relation schema.
|
|
2237
|
+
* @param target - The target of the relation.
|
|
2238
|
+
* @param foreignField - The foreign field of the relation.
|
|
2239
|
+
* @returns The relation schema.
|
|
2240
|
+
*/
|
|
2393
2241
|
relation: (target, foreignField) => ({ type: 'relation', target, foreignField }),
|
|
2394
2242
|
});
|
|
2243
|
+
/**
|
|
2244
|
+
* Creates a ProtoRoute.
|
|
2245
|
+
* @param options - The options for the ProtoRoute.
|
|
2246
|
+
* @returns A promise that resolves to a Router.
|
|
2247
|
+
*/
|
|
2395
2248
|
const ProtoRoute = async (options) => {
|
|
2396
2249
|
const proto = options.proto instanceof ProtoService ? options.proto : new ProtoService(options.proto);
|
|
2397
2250
|
await proto[_private.PVK].prepare();
|
|
@@ -2414,6 +2267,13 @@ const ProtoRoute = async (options) => {
|
|
|
2414
2267
|
configRoute(router, proto);
|
|
2415
2268
|
return router;
|
|
2416
2269
|
};
|
|
2270
|
+
/**
|
|
2271
|
+
* Registers a ProtoSocket.
|
|
2272
|
+
* @param proto - The ProtoService instance.
|
|
2273
|
+
* @param server - The server instance.
|
|
2274
|
+
* @param endpoint - The optional endpoint.
|
|
2275
|
+
* @returns The socket.io instance.
|
|
2276
|
+
*/
|
|
2417
2277
|
const registerProtoSocket = (proto, server, endpoint) => {
|
|
2418
2278
|
const io = endpoint ? server.socket().of(endpoint) : server.socket();
|
|
2419
2279
|
io.on('connection', async (socket) => {
|