proto.io 0.0.174 → 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.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 +1 -1
- package/dist/adapters/storage/progres.mjs +3 -3
- package/dist/client.d.ts +3 -3
- package/dist/client.js +1 -1
- package/dist/client.mjs +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +46 -311
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +51 -316
- package/dist/index.mjs.map +1 -1
- package/dist/internals/{index-uwXdnxqN.d.ts → index-B9CWU0y7.d.ts} +32 -2
- package/dist/internals/index-B9CWU0y7.d.ts.map +1 -0
- package/dist/internals/{index-R0gbIGc-.mjs → index-BPbXumUi.mjs} +18 -44
- package/dist/internals/index-BPbXumUi.mjs.map +1 -0
- package/dist/internals/{index-CGX3qcjQ.d.ts → index-BQqYdjaH.d.ts} +2 -2
- package/dist/internals/index-BQqYdjaH.d.ts.map +1 -0
- package/dist/internals/{index-CE5tdYK8.d.ts → index-BYZpj31o.d.ts} +17 -68
- package/dist/internals/index-BYZpj31o.d.ts.map +1 -0
- package/dist/internals/{index--ifyu-GL.mjs → index-CTsc042s.mjs} +2 -2
- package/dist/internals/{index--ifyu-GL.mjs.map → index-CTsc042s.mjs.map} +1 -1
- package/dist/internals/{index-S_gTMQBh.js → index-CvA0tbwx.js} +17 -43
- package/dist/internals/index-CvA0tbwx.js.map +1 -0
- package/dist/internals/{index-C3fbOqmn.mjs → index-DHQhYGsJ.mjs} +2 -2
- package/dist/internals/{index-C3fbOqmn.mjs.map → index-DHQhYGsJ.mjs.map} +1 -1
- package/dist/internals/{random-DPRG8oW6.mjs → random-BMQpRlGH.mjs} +3 -3
- package/dist/internals/{random-DPRG8oW6.mjs.map → random-BMQpRlGH.mjs.map} +1 -1
- package/package.json +1 -1
- package/dist/internals/index-CE5tdYK8.d.ts.map +0 -1
- package/dist/internals/index-CGX3qcjQ.d.ts.map +0 -1
- package/dist/internals/index-R0gbIGc-.mjs.map +0 -1
- package/dist/internals/index-S_gTMQBh.js.map +0 -1
- package/dist/internals/index-uwXdnxqN.d.ts.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-
|
|
3
|
+
import { Q as QueryValidator, r as resolveColumn, a as resolveDataType, g as generateId } from './internals/random-BMQpRlGH.mjs';
|
|
4
4
|
import { P as PVK } from './internals/private-BUpLAMZi.mjs';
|
|
5
|
-
import {
|
|
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-
|
|
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-
|
|
8
|
-
import { i as isPointer, a as isRelation,
|
|
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-BPbXumUi.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-BPbXumUi.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-
|
|
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';
|
|
@@ -245,46 +245,6 @@ const dispatcher = (proto, options, disableSecurity) => {
|
|
|
245
245
|
};
|
|
246
246
|
};
|
|
247
247
|
|
|
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
248
|
//
|
|
289
249
|
// options.ts
|
|
290
250
|
//
|
|
@@ -391,68 +351,16 @@ class _ProtoQuery extends TQuery {
|
|
|
391
351
|
});
|
|
392
352
|
}
|
|
393
353
|
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
354
|
const result = this._objectMethods(await this._dispatcher(options).insert({
|
|
405
355
|
className: this.className,
|
|
406
356
|
includes: this[PVK].options.includes,
|
|
407
357
|
matches: this[PVK].options.matches,
|
|
408
|
-
},
|
|
358
|
+
}, attrs));
|
|
409
359
|
if (!result)
|
|
410
360
|
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
361
|
return result;
|
|
420
362
|
}
|
|
421
363
|
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
364
|
return this._dispatcher(options).insertMany({
|
|
457
365
|
className: this.className,
|
|
458
366
|
includes: this[PVK].options.includes,
|
|
@@ -460,216 +368,24 @@ class _ProtoQuery extends TQuery {
|
|
|
460
368
|
}, values);
|
|
461
369
|
}
|
|
462
370
|
async updateOne(update, options) {
|
|
463
|
-
|
|
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;
|
|
371
|
+
return this._objectMethods(await this._dispatcher(options).updateOne(this._queryOptions, update));
|
|
485
372
|
}
|
|
486
373
|
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
374
|
return this._dispatcher(options).updateMany(this._queryOptions, update);
|
|
519
375
|
}
|
|
520
376
|
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
377
|
const result = this._objectMethods(await this._dispatcher(options).upsertOne(this._queryOptions, update, setOnInsert));
|
|
551
378
|
if (!result)
|
|
552
379
|
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
380
|
return result;
|
|
562
381
|
}
|
|
563
382
|
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
383
|
return this._dispatcher(options).upsertMany(this._queryOptions, update, setOnInsert);
|
|
615
384
|
}
|
|
616
385
|
async deleteOne(options) {
|
|
617
|
-
|
|
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;
|
|
386
|
+
return this._objectMethods(await this._dispatcher(options).deleteOne(this._queryOptions));
|
|
644
387
|
}
|
|
645
388
|
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
389
|
return this._dispatcher(options).deleteMany(this._queryOptions);
|
|
674
390
|
}
|
|
675
391
|
}
|
|
@@ -870,6 +586,46 @@ const varifyPassword = async (alg, password, options) => {
|
|
|
870
586
|
}
|
|
871
587
|
};
|
|
872
588
|
|
|
589
|
+
//
|
|
590
|
+
// proxy.ts
|
|
591
|
+
//
|
|
592
|
+
// The MIT License
|
|
593
|
+
// Copyright (c) 2021 - 2024 O2ter Limited. All rights reserved.
|
|
594
|
+
//
|
|
595
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
596
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
597
|
+
// in the Software without restriction, including without limitation the rights
|
|
598
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
599
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
600
|
+
// furnished to do so, subject to the following conditions:
|
|
601
|
+
//
|
|
602
|
+
// The above copyright notice and this permission notice shall be included in
|
|
603
|
+
// all copies or substantial portions of the Software.
|
|
604
|
+
//
|
|
605
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
606
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
607
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
608
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
609
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
610
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
611
|
+
// THE SOFTWARE.
|
|
612
|
+
//
|
|
613
|
+
const proxy = (x) => {
|
|
614
|
+
const self = x;
|
|
615
|
+
const proxy = _.create(self);
|
|
616
|
+
for (const name of _.uniq(_.flatMap(prototypes(proxy), x => Object.getOwnPropertyNames(x)))) {
|
|
617
|
+
if (name === 'constructor')
|
|
618
|
+
continue;
|
|
619
|
+
if (_.isFunction(self[name])) {
|
|
620
|
+
proxy[name] = self[name].bind(self);
|
|
621
|
+
}
|
|
622
|
+
else {
|
|
623
|
+
Object.defineProperty(proxy, name, { get: () => self[name] });
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
return proxy;
|
|
627
|
+
};
|
|
628
|
+
|
|
873
629
|
//
|
|
874
630
|
// internal.ts
|
|
875
631
|
//
|
|
@@ -990,7 +746,6 @@ const mergeSchema = (...schemas) => _.reduce(schemas, (acc, schema) => ({
|
|
|
990
746
|
class ProtoInternal {
|
|
991
747
|
options;
|
|
992
748
|
functions = {};
|
|
993
|
-
triggers = {};
|
|
994
749
|
constructor(options) {
|
|
995
750
|
validateSchemaName(options.schema);
|
|
996
751
|
const schema = mergeSchema(defaultSchema, options.fileStorage.schema, options.schema);
|
|
@@ -1667,26 +1422,6 @@ class ProtoService extends ProtoType {
|
|
|
1667
1422
|
define(name, callback, options) {
|
|
1668
1423
|
this[PVK].functions[name] = options ? { callback, ...options } : callback;
|
|
1669
1424
|
}
|
|
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
1425
|
lockTable(className, update) {
|
|
1691
1426
|
return this.storage.lockTable(className, update);
|
|
1692
1427
|
}
|
|
@@ -1883,12 +1618,12 @@ const verifyRelatedBy = (relatedBy) => {
|
|
|
1883
1618
|
var classesRoute = (router, proto) => {
|
|
1884
1619
|
const defaultHandler = async (req) => {
|
|
1885
1620
|
const { name } = req.params;
|
|
1886
|
-
const { operation,
|
|
1621
|
+
const { operation, random, attributes, update, setOnInsert, relatedBy, ...options } = deserialize(req.body);
|
|
1887
1622
|
verifyRelatedBy(relatedBy);
|
|
1888
1623
|
const payload = proto.connect(req);
|
|
1889
1624
|
const query = relatedBy ? payload.Relation(payload.Object(relatedBy.className, relatedBy.objectId), relatedBy.key) : payload.Query(name);
|
|
1890
1625
|
query[PVK].options = options;
|
|
1891
|
-
const opts = { master: payload.isMaster
|
|
1626
|
+
const opts = { master: payload.isMaster };
|
|
1892
1627
|
switch (operation) {
|
|
1893
1628
|
case 'explain':
|
|
1894
1629
|
if (!payload.isMaster)
|