proto.io 0.0.172 → 0.0.174
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 +9 -2
- package/dist/adapters/storage/progres.js +58 -5
- package/dist/adapters/storage/progres.js.map +1 -1
- package/dist/adapters/storage/progres.mjs +58 -5
- 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 +242 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +243 -13
- package/dist/index.mjs.map +1 -1
- package/dist/internals/{index-BYbMU-Ao.mjs → index--ifyu-GL.mjs} +139 -1
- package/dist/internals/index--ifyu-GL.mjs.map +1 -0
- package/dist/internals/{index-B1wqSio6.mjs → index-C3fbOqmn.mjs} +2 -2
- package/dist/internals/{index-B1wqSio6.mjs.map → index-C3fbOqmn.mjs.map} +1 -1
- package/dist/internals/index-CE5tdYK8.d.ts +1816 -0
- package/dist/internals/index-CE5tdYK8.d.ts.map +1 -0
- package/dist/internals/{index-K0jhERvZ.d.ts → index-CGX3qcjQ.d.ts} +2 -2
- package/dist/internals/index-CGX3qcjQ.d.ts.map +1 -0
- 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/{index-D0hHgn2P.mjs → index-R0gbIGc-.mjs} +289 -2
- package/dist/internals/index-R0gbIGc-.mjs.map +1 -0
- package/dist/internals/{index-BJP46VGq.js → index-S_gTMQBh.js} +289 -2
- package/dist/internals/index-S_gTMQBh.js.map +1 -0
- package/dist/internals/{index-DchUjNEf.d.ts → index-uwXdnxqN.d.ts} +2 -2
- package/dist/internals/index-uwXdnxqN.d.ts.map +1 -0
- package/dist/internals/{random-BCpwYpyw.mjs → random-DPRG8oW6.mjs} +3 -3
- package/dist/internals/{random-BCpwYpyw.mjs.map → random-DPRG8oW6.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-BJP46VGq.js.map +0 -1
- package/dist/internals/index-BYbMU-Ao.mjs.map +0 -1
- package/dist/internals/index-BhWRmBiq.d.ts +0 -726
- package/dist/internals/index-BhWRmBiq.d.ts.map +0 -1
- package/dist/internals/index-CVutVPmd.js.map +0 -1
- package/dist/internals/index-D0hHgn2P.mjs.map +0 -1
- package/dist/internals/index-DchUjNEf.d.ts.map +0 -1
- package/dist/internals/index-K0jhERvZ.d.ts.map +0 -1
package/dist/client.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { P as ProtoClient } from './internals/index-
|
|
2
|
-
export { c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-
|
|
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';
|
|
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-
|
|
4
|
+
export { D as DeserializeOptions, S as SerializeOptions, d as TSerializable, e as deserialize, s as serialize } from './internals/index-CE5tdYK8.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,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var index = require('./internals/index-
|
|
5
|
+
var index = require('./internals/index-S_gTMQBh.js');
|
|
6
6
|
var Decimal = require('decimal.js');
|
|
7
|
-
require('./internals/index-
|
|
7
|
+
require('./internals/index-DXuW8UiB.js');
|
|
8
8
|
require('lodash');
|
|
9
9
|
require('./internals/private-CSB1Ep4g.js');
|
|
10
10
|
require('@o2ter/utils-js');
|
package/dist/client.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { c as ProtoClient } from './internals/index-
|
|
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-
|
|
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';
|
|
3
3
|
export { Decimal } from 'decimal.js';
|
|
4
|
-
import './internals/index-
|
|
4
|
+
import './internals/index--ifyu-GL.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-
|
|
5
|
-
export { D as DeserializeOptions, S as SerializeOptions, c as TFileStorage, d as TSerializable, e as deserialize, s as serialize } from './internals/index-
|
|
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';
|
|
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-
|
|
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';
|
|
9
9
|
import '@o2ter/utils-js';
|
|
10
10
|
import 'jsonwebtoken';
|
|
11
11
|
import 'lodash';
|
|
@@ -13,57 +13,129 @@ import 'node:stream';
|
|
|
13
13
|
import 'socket.io-client';
|
|
14
14
|
import '@socket.io/component-emitter';
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Schema definition utility functions.
|
|
18
|
+
*/
|
|
16
19
|
declare const schema: ((x: Record<string, TSchema>) => Record<string, TSchema>) & {
|
|
20
|
+
/**
|
|
21
|
+
* Defines a boolean schema.
|
|
22
|
+
* @param defaultValue - The default value for the boolean.
|
|
23
|
+
* @returns The boolean schema.
|
|
24
|
+
*/
|
|
17
25
|
boolean: (defaultValue?: boolean) => {
|
|
18
26
|
readonly type: "boolean";
|
|
19
27
|
readonly default: boolean | undefined;
|
|
20
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* Defines a number schema.
|
|
31
|
+
* @param defaultValue - The default value for the number.
|
|
32
|
+
* @returns The number schema.
|
|
33
|
+
*/
|
|
21
34
|
number: (defaultValue?: number) => {
|
|
22
35
|
readonly type: "number";
|
|
23
36
|
readonly default: number | undefined;
|
|
24
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* Defines a decimal schema.
|
|
40
|
+
* @param defaultValue - The default value for the decimal.
|
|
41
|
+
* @returns The decimal schema.
|
|
42
|
+
*/
|
|
25
43
|
decimal: (defaultValue?: Decimal) => {
|
|
26
44
|
readonly type: "decimal";
|
|
27
45
|
readonly default: Decimal | undefined;
|
|
28
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* Defines a string schema.
|
|
49
|
+
* @param defaultValue - The default value for the string.
|
|
50
|
+
* @returns The string schema.
|
|
51
|
+
*/
|
|
29
52
|
string: (defaultValue?: string) => {
|
|
30
53
|
readonly type: "string";
|
|
31
54
|
readonly default: string | undefined;
|
|
32
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Defines a date schema.
|
|
58
|
+
* @param defaultValue - The default value for the date.
|
|
59
|
+
* @returns The date schema.
|
|
60
|
+
*/
|
|
33
61
|
date: (defaultValue?: Date) => {
|
|
34
62
|
readonly type: "date";
|
|
35
63
|
readonly default: Date | undefined;
|
|
36
64
|
};
|
|
65
|
+
/**
|
|
66
|
+
* Defines an object schema.
|
|
67
|
+
* @param defaultValue - The default value for the object.
|
|
68
|
+
* @returns The object schema.
|
|
69
|
+
*/
|
|
37
70
|
object: <T extends Record<string, _TValue>>(defaultValue?: T) => {
|
|
38
71
|
readonly type: "object";
|
|
39
72
|
readonly default: T | undefined;
|
|
40
73
|
};
|
|
74
|
+
/**
|
|
75
|
+
* Defines an array schema.
|
|
76
|
+
* @param defaultValue - The default value for the array.
|
|
77
|
+
* @returns The array schema.
|
|
78
|
+
*/
|
|
41
79
|
array: <T extends _TValue[]>(defaultValue?: T) => {
|
|
42
80
|
readonly type: "array";
|
|
43
81
|
readonly default: T | undefined;
|
|
44
82
|
};
|
|
83
|
+
/**
|
|
84
|
+
* Defines a vector schema.
|
|
85
|
+
* @param dimension - The dimension of the vector.
|
|
86
|
+
* @param defaultValue - The default value for the vector.
|
|
87
|
+
* @returns The vector schema.
|
|
88
|
+
*/
|
|
45
89
|
vector: (dimension: number, defaultValue?: number[]) => {
|
|
46
90
|
readonly type: "vector";
|
|
47
91
|
readonly dimension: number;
|
|
48
92
|
readonly default: number[] | undefined;
|
|
49
93
|
};
|
|
94
|
+
/**
|
|
95
|
+
* Defines a shape schema.
|
|
96
|
+
* @param shape - The shape definition.
|
|
97
|
+
* @returns The shape schema.
|
|
98
|
+
*/
|
|
50
99
|
shape: (shape: Record<string, TSchema.DataType>) => {
|
|
51
100
|
readonly type: "shape";
|
|
52
101
|
readonly shape: Record<string, TSchema.DataType>;
|
|
53
102
|
};
|
|
103
|
+
/**
|
|
104
|
+
* Defines a pointer schema.
|
|
105
|
+
* @param target - The target of the pointer.
|
|
106
|
+
* @returns The pointer schema.
|
|
107
|
+
*/
|
|
54
108
|
pointer: (target: string) => {
|
|
55
109
|
readonly type: "pointer";
|
|
56
110
|
readonly target: string;
|
|
57
111
|
};
|
|
112
|
+
/**
|
|
113
|
+
* Defines a relation schema.
|
|
114
|
+
* @param target - The target of the relation.
|
|
115
|
+
* @param foreignField - The foreign field of the relation.
|
|
116
|
+
* @returns The relation schema.
|
|
117
|
+
*/
|
|
58
118
|
relation: (target: string, foreignField?: string) => {
|
|
59
119
|
readonly type: "relation";
|
|
60
120
|
readonly target: string;
|
|
61
121
|
readonly foreignField: string | undefined;
|
|
62
122
|
};
|
|
63
123
|
};
|
|
124
|
+
/**
|
|
125
|
+
* Creates a ProtoRoute.
|
|
126
|
+
* @param options - The options for the ProtoRoute.
|
|
127
|
+
* @returns A promise that resolves to a Router.
|
|
128
|
+
*/
|
|
64
129
|
declare const ProtoRoute: <E>(options: {
|
|
65
130
|
proto: ProtoService<E> | (ProtoServiceOptions<E> & ProtoServiceKeyOptions);
|
|
66
131
|
}) => Promise<Router>;
|
|
132
|
+
/**
|
|
133
|
+
* Registers a ProtoSocket.
|
|
134
|
+
* @param proto - The ProtoService instance.
|
|
135
|
+
* @param server - The server instance.
|
|
136
|
+
* @param endpoint - The optional endpoint.
|
|
137
|
+
* @returns The socket.io instance.
|
|
138
|
+
*/
|
|
67
139
|
declare const registerProtoSocket: <E>(proto: ProtoService<E>, server: Server, endpoint?: string) => socket_io.Server<socket_io.DefaultEventsMap, socket_io.DefaultEventsMap, socket_io.DefaultEventsMap, any> | socket_io.Namespace<socket_io.DefaultEventsMap, socket_io.DefaultEventsMap, socket_io.DefaultEventsMap, any>;
|
|
68
140
|
|
|
69
141
|
export { ProtoRoute, ProtoService, ProtoRoute as default, registerProtoSocket, schema };
|
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-S_gTMQBh.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');
|
|
@@ -186,6 +186,13 @@ const dispatcher = (proto, options, disableSecurity) => {
|
|
|
186
186
|
throw Error('No permission');
|
|
187
187
|
return proto.storage.atomic((storage) => storage.updateOne(_validator.decodeQuery(normalize(query), 'update'), normalize(_validator.validateFields(query.className, update, 'update', random.QueryValidator.patterns.path))));
|
|
188
188
|
},
|
|
189
|
+
async updateMany(query, update) {
|
|
190
|
+
random.QueryValidator.recursiveCheck(query, update);
|
|
191
|
+
const _validator = await validator();
|
|
192
|
+
if (!_validator.validateCLPs(query.className, 'update'))
|
|
193
|
+
throw Error('No permission');
|
|
194
|
+
return proto.storage.atomic((storage) => storage.updateMany(_validator.decodeQuery(normalize(query), 'update'), normalize(_validator.validateFields(query.className, update, 'update', random.QueryValidator.patterns.path))));
|
|
195
|
+
},
|
|
189
196
|
async upsertOne(query, update, setOnInsert) {
|
|
190
197
|
random.QueryValidator.recursiveCheck(query, update, setOnInsert);
|
|
191
198
|
const _validator = await validator();
|
|
@@ -205,6 +212,25 @@ const dispatcher = (proto, options, disableSecurity) => {
|
|
|
205
212
|
}
|
|
206
213
|
}
|
|
207
214
|
},
|
|
215
|
+
async upsertMany(query, update, setOnInsert) {
|
|
216
|
+
random.QueryValidator.recursiveCheck(query, update, setOnInsert);
|
|
217
|
+
const _validator = await validator();
|
|
218
|
+
if (!_validator.validateCLPs(query.className, 'create', 'update'))
|
|
219
|
+
throw Error('No permission');
|
|
220
|
+
const _query = _validator.decodeQuery(normalize(query), 'update');
|
|
221
|
+
const _update = normalize(_validator.validateFields(query.className, update, 'update', random.QueryValidator.patterns.path));
|
|
222
|
+
const _setOnInsert = normalize(_validator.validateFields(query.className, setOnInsert, 'create', random.QueryValidator.patterns.name));
|
|
223
|
+
while (true) {
|
|
224
|
+
try {
|
|
225
|
+
return await proto.storage.atomic((storage) => storage.upsertMany(_query, _update, _setOnInsert), { lockTable: query.className, retry: true });
|
|
226
|
+
}
|
|
227
|
+
catch (e) {
|
|
228
|
+
if (proto.storage.isDuplicateIdError(e))
|
|
229
|
+
continue;
|
|
230
|
+
throw e;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
},
|
|
208
234
|
async deleteOne(query) {
|
|
209
235
|
random.QueryValidator.recursiveCheck(query);
|
|
210
236
|
const _validator = await validator();
|
|
@@ -386,7 +412,12 @@ class _ProtoQuery extends index.TQuery {
|
|
|
386
412
|
if (!result)
|
|
387
413
|
throw Error('Unable to insert document');
|
|
388
414
|
if (_.isFunction(afterSave)) {
|
|
389
|
-
|
|
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
|
+
}
|
|
390
421
|
}
|
|
391
422
|
return result;
|
|
392
423
|
}
|
|
@@ -414,7 +445,14 @@ class _ProtoQuery extends index.TQuery {
|
|
|
414
445
|
matches: this[_private.PVK].options.matches,
|
|
415
446
|
}, _.map(objects, x => _.fromPairs([...x._set_entries()])));
|
|
416
447
|
if (_.isFunction(afterSave)) {
|
|
417
|
-
await Promise.all(_.map(objects, object =>
|
|
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
|
+
}));
|
|
418
456
|
}
|
|
419
457
|
return objects.length;
|
|
420
458
|
}
|
|
@@ -439,10 +477,49 @@ class _ProtoQuery extends index.TQuery {
|
|
|
439
477
|
}
|
|
440
478
|
const result = this._objectMethods(await this._dispatcher(options).updateOne(this._queryOptions, update));
|
|
441
479
|
if (result && _.isFunction(afterSave)) {
|
|
442
|
-
|
|
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
|
+
}
|
|
443
486
|
}
|
|
444
487
|
return result;
|
|
445
488
|
}
|
|
489
|
+
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
|
+
return this._dispatcher(options).updateMany(this._queryOptions, update);
|
|
522
|
+
}
|
|
446
523
|
async upsertOne(update, setOnInsert, options) {
|
|
447
524
|
const context = options?.context ?? {};
|
|
448
525
|
const silent = _.castArray(options?.silent ?? []);
|
|
@@ -477,10 +554,68 @@ class _ProtoQuery extends index.TQuery {
|
|
|
477
554
|
if (!result)
|
|
478
555
|
throw Error('Unable to upsert document');
|
|
479
556
|
if (_.isFunction(afterSave)) {
|
|
480
|
-
|
|
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
|
+
}
|
|
481
563
|
}
|
|
482
564
|
return result;
|
|
483
565
|
}
|
|
566
|
+
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
|
+
return this._dispatcher(options).upsertMany(this._queryOptions, update, setOnInsert);
|
|
618
|
+
}
|
|
484
619
|
async deleteOne(options) {
|
|
485
620
|
const context = options?.context ?? {};
|
|
486
621
|
const silent = _.castArray(options?.silent ?? []);
|
|
@@ -501,7 +636,12 @@ class _ProtoQuery extends index.TQuery {
|
|
|
501
636
|
result = this._objectMethods(await this._dispatcher(options).deleteOne(this._queryOptions));
|
|
502
637
|
}
|
|
503
638
|
if (result && _.isFunction(afterDelete)) {
|
|
504
|
-
|
|
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
|
+
}
|
|
505
645
|
}
|
|
506
646
|
return result;
|
|
507
647
|
}
|
|
@@ -522,7 +662,14 @@ class _ProtoQuery extends index.TQuery {
|
|
|
522
662
|
filter: { _id: { $in: _.map(objects, x => x.objectId) } },
|
|
523
663
|
});
|
|
524
664
|
if (_.isFunction(afterDelete)) {
|
|
525
|
-
await Promise.all(_.map(objects, object =>
|
|
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
|
+
}));
|
|
526
673
|
}
|
|
527
674
|
return objects.length;
|
|
528
675
|
}
|
|
@@ -1445,8 +1592,8 @@ class ProtoService extends index.ProtoType {
|
|
|
1445
1592
|
}
|
|
1446
1593
|
async userRoles(user) {
|
|
1447
1594
|
const self = this;
|
|
1595
|
+
const { inheritKeys, resolver } = self[_private.PVK].options.roleResolver;
|
|
1448
1596
|
const defaultResolver = async () => {
|
|
1449
|
-
const inheritKeys = self[_private.PVK].options.roleResolver?.inheritKeys ?? [];
|
|
1450
1597
|
const schema = self.schema;
|
|
1451
1598
|
const userKeys = _.filter(inheritKeys, k => {
|
|
1452
1599
|
const type = random.resolveDataType(schema, 'Role', k);
|
|
@@ -1471,7 +1618,6 @@ class ProtoService extends index.ProtoType {
|
|
|
1471
1618
|
}
|
|
1472
1619
|
return roles;
|
|
1473
1620
|
};
|
|
1474
|
-
const resolver = self[_private.PVK].options.roleResolver?.resolver;
|
|
1475
1621
|
if (resolver)
|
|
1476
1622
|
return resolver(user, defaultResolver);
|
|
1477
1623
|
return defaultResolver();
|
|
@@ -1779,7 +1925,9 @@ var classesRoute = (router, proto) => {
|
|
|
1779
1925
|
case 'insert': return query.insert(attributes, opts);
|
|
1780
1926
|
case 'insertMany': return query.insertMany(attributes, opts);
|
|
1781
1927
|
case 'updateOne': return query.updateOne(update, opts);
|
|
1928
|
+
case 'updateMany': return query.updateMany(update, opts);
|
|
1782
1929
|
case 'upsertOne': return query.upsertOne(update, setOnInsert, opts);
|
|
1930
|
+
case 'upsertMany': return query.upsertMany(update, setOnInsert, opts);
|
|
1783
1931
|
case 'deleteOne': return query.deleteOne(opts);
|
|
1784
1932
|
case 'deleteMany': return query.deleteMany(opts);
|
|
1785
1933
|
default: throw Error('Invalid operation');
|
|
@@ -1883,6 +2031,16 @@ var classesRoute = (router, proto) => {
|
|
|
1883
2031
|
const payload = proto.connect(req);
|
|
1884
2032
|
await response(res, async () => payload.refs(payload.Object(name, id), { master: payload.isMaster }));
|
|
1885
2033
|
});
|
|
2034
|
+
router.patch('/classes/:name', serverJs.Server.text({ type: '*/*' }), async (req, res) => {
|
|
2035
|
+
res.setHeader('Cache-Control', ['no-cache', 'no-store']);
|
|
2036
|
+
const { name } = req.params;
|
|
2037
|
+
const classes = proto.classes();
|
|
2038
|
+
if (!_.includes(classes, name))
|
|
2039
|
+
return void res.sendStatus(404);
|
|
2040
|
+
const payload = proto.connect(req);
|
|
2041
|
+
const update = _.mapValues(index.deserialize(req.body), v => ({ $set: v }));
|
|
2042
|
+
await response(res, () => createQuery(payload, req, false).updateMany(update, { master: payload.isMaster }));
|
|
2043
|
+
});
|
|
1886
2044
|
router.patch('/classes/:name/:id', serverJs.Server.text({ type: '*/*' }), async (req, res) => {
|
|
1887
2045
|
res.setHeader('Cache-Control', ['no-cache', 'no-store']);
|
|
1888
2046
|
const { name, id } = req.params;
|
|
@@ -2274,19 +2432,84 @@ var configRoute = (router, proto) => {
|
|
|
2274
2432
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
2275
2433
|
// THE SOFTWARE.
|
|
2276
2434
|
//
|
|
2435
|
+
/**
|
|
2436
|
+
* Schema definition utility functions.
|
|
2437
|
+
*/
|
|
2277
2438
|
const schema = _.assign((x) => x, {
|
|
2439
|
+
/**
|
|
2440
|
+
* Defines a boolean schema.
|
|
2441
|
+
* @param defaultValue - The default value for the boolean.
|
|
2442
|
+
* @returns The boolean schema.
|
|
2443
|
+
*/
|
|
2278
2444
|
boolean: (defaultValue) => ({ type: 'boolean', default: defaultValue }),
|
|
2445
|
+
/**
|
|
2446
|
+
* Defines a number schema.
|
|
2447
|
+
* @param defaultValue - The default value for the number.
|
|
2448
|
+
* @returns The number schema.
|
|
2449
|
+
*/
|
|
2279
2450
|
number: (defaultValue) => ({ type: 'number', default: defaultValue }),
|
|
2451
|
+
/**
|
|
2452
|
+
* Defines a decimal schema.
|
|
2453
|
+
* @param defaultValue - The default value for the decimal.
|
|
2454
|
+
* @returns The decimal schema.
|
|
2455
|
+
*/
|
|
2280
2456
|
decimal: (defaultValue) => ({ type: 'decimal', default: defaultValue }),
|
|
2457
|
+
/**
|
|
2458
|
+
* Defines a string schema.
|
|
2459
|
+
* @param defaultValue - The default value for the string.
|
|
2460
|
+
* @returns The string schema.
|
|
2461
|
+
*/
|
|
2281
2462
|
string: (defaultValue) => ({ type: 'string', default: defaultValue }),
|
|
2463
|
+
/**
|
|
2464
|
+
* Defines a date schema.
|
|
2465
|
+
* @param defaultValue - The default value for the date.
|
|
2466
|
+
* @returns The date schema.
|
|
2467
|
+
*/
|
|
2282
2468
|
date: (defaultValue) => ({ type: 'date', default: defaultValue }),
|
|
2469
|
+
/**
|
|
2470
|
+
* Defines an object schema.
|
|
2471
|
+
* @param defaultValue - The default value for the object.
|
|
2472
|
+
* @returns The object schema.
|
|
2473
|
+
*/
|
|
2283
2474
|
object: (defaultValue) => ({ type: 'object', default: defaultValue }),
|
|
2475
|
+
/**
|
|
2476
|
+
* Defines an array schema.
|
|
2477
|
+
* @param defaultValue - The default value for the array.
|
|
2478
|
+
* @returns The array schema.
|
|
2479
|
+
*/
|
|
2284
2480
|
array: (defaultValue) => ({ type: 'array', default: defaultValue }),
|
|
2481
|
+
/**
|
|
2482
|
+
* Defines a vector schema.
|
|
2483
|
+
* @param dimension - The dimension of the vector.
|
|
2484
|
+
* @param defaultValue - The default value for the vector.
|
|
2485
|
+
* @returns The vector schema.
|
|
2486
|
+
*/
|
|
2285
2487
|
vector: (dimension, defaultValue) => ({ type: 'vector', dimension, default: defaultValue }),
|
|
2488
|
+
/**
|
|
2489
|
+
* Defines a shape schema.
|
|
2490
|
+
* @param shape - The shape definition.
|
|
2491
|
+
* @returns The shape schema.
|
|
2492
|
+
*/
|
|
2286
2493
|
shape: (shape) => ({ type: 'shape', shape }),
|
|
2494
|
+
/**
|
|
2495
|
+
* Defines a pointer schema.
|
|
2496
|
+
* @param target - The target of the pointer.
|
|
2497
|
+
* @returns The pointer schema.
|
|
2498
|
+
*/
|
|
2287
2499
|
pointer: (target) => ({ type: 'pointer', target }),
|
|
2500
|
+
/**
|
|
2501
|
+
* Defines a relation schema.
|
|
2502
|
+
* @param target - The target of the relation.
|
|
2503
|
+
* @param foreignField - The foreign field of the relation.
|
|
2504
|
+
* @returns The relation schema.
|
|
2505
|
+
*/
|
|
2288
2506
|
relation: (target, foreignField) => ({ type: 'relation', target, foreignField }),
|
|
2289
2507
|
});
|
|
2508
|
+
/**
|
|
2509
|
+
* Creates a ProtoRoute.
|
|
2510
|
+
* @param options - The options for the ProtoRoute.
|
|
2511
|
+
* @returns A promise that resolves to a Router.
|
|
2512
|
+
*/
|
|
2290
2513
|
const ProtoRoute = async (options) => {
|
|
2291
2514
|
const proto = options.proto instanceof ProtoService ? options.proto : new ProtoService(options.proto);
|
|
2292
2515
|
await proto[_private.PVK].prepare();
|
|
@@ -2309,6 +2532,13 @@ const ProtoRoute = async (options) => {
|
|
|
2309
2532
|
configRoute(router, proto);
|
|
2310
2533
|
return router;
|
|
2311
2534
|
};
|
|
2535
|
+
/**
|
|
2536
|
+
* Registers a ProtoSocket.
|
|
2537
|
+
* @param proto - The ProtoService instance.
|
|
2538
|
+
* @param server - The server instance.
|
|
2539
|
+
* @param endpoint - The optional endpoint.
|
|
2540
|
+
* @returns The socket.io instance.
|
|
2541
|
+
*/
|
|
2312
2542
|
const registerProtoSocket = (proto, server, endpoint) => {
|
|
2313
2543
|
const io = endpoint ? server.socket().of(endpoint) : server.socket();
|
|
2314
2544
|
io.on('connection', async (socket) => {
|