@nymphjs/nymph 1.0.0-beta.7 → 1.0.0-beta.70
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/CHANGELOG.md +320 -0
- package/README.md +13 -21
- package/dist/Entity.d.ts +269 -7
- package/dist/Entity.js +491 -105
- package/dist/Entity.js.map +1 -1
- package/dist/Entity.test.js +39 -17
- package/dist/Entity.test.js.map +1 -1
- package/dist/Entity.types.d.ts +225 -19
- package/dist/Nymph.d.ts +423 -11
- package/dist/Nymph.js +685 -176
- package/dist/Nymph.js.map +1 -1
- package/dist/Nymph.types.d.ts +31 -7
- package/dist/Nymph.types.js +2 -1
- package/dist/Nymph.types.js.map +1 -1
- package/dist/conf/d.d.ts +26 -0
- package/dist/conf/defaults.js +22 -0
- package/dist/conf/defaults.js.map +1 -1
- package/dist/driver/NymphDriver.d.ts +129 -38
- package/dist/driver/NymphDriver.js +264 -131
- package/dist/driver/NymphDriver.js.map +1 -1
- package/dist/driver/testArtifacts.d.ts +1 -1
- package/dist/driver/testArtifacts.js +336 -42
- package/dist/driver/testArtifacts.js.map +1 -1
- package/dist/errors/EntityInvalidDataError.d.ts +6 -0
- package/dist/errors/EntityInvalidDataError.js +7 -1
- package/dist/errors/EntityInvalidDataError.js.map +1 -1
- package/dist/errors/EntityUniqueConstraintError.d.ts +3 -0
- package/dist/errors/EntityUniqueConstraintError.js +11 -0
- package/dist/errors/EntityUniqueConstraintError.js.map +1 -0
- package/dist/errors/InvalidStateError.d.ts +3 -0
- package/dist/errors/InvalidStateError.js +11 -0
- package/dist/errors/InvalidStateError.js.map +1 -0
- package/dist/errors/QueryFailedError.js +1 -0
- package/dist/errors/QueryFailedError.js.map +1 -1
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.js +2 -0
- package/dist/errors/index.js.map +1 -1
- package/dist/testArtifacts.d.ts +17 -5
- package/dist/testArtifacts.js +31 -27
- package/dist/testArtifacts.js.map +1 -1
- package/dist/testMocks.d.ts +15 -10
- package/dist/testMocks.js +37 -13
- package/dist/testMocks.js.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +21 -5
- package/dist/utils.js.map +1 -1
- package/package.json +16 -14
- package/src/Entity.test.ts +32 -11
- package/src/Entity.ts +423 -134
- package/src/Entity.types.ts +32 -66
- package/src/Nymph.ts +618 -195
- package/src/Nymph.types.ts +97 -26
- package/src/conf/d.ts +8 -0
- package/src/conf/defaults.ts +24 -0
- package/src/driver/NymphDriver.ts +281 -311
- package/src/driver/testArtifacts.ts +384 -173
- package/src/errors/EntityUniqueConstraintError.ts +6 -0
- package/src/errors/InvalidStateError.ts +6 -0
- package/src/errors/index.ts +4 -0
- package/src/nymphJoiProps.ts +2 -2
- package/src/testArtifacts.ts +18 -19
- package/src/testMocks.ts +64 -32
- package/src/utils.ts +22 -8
- package/tsconfig.json +3 -3
- package/typedoc.json +4 -0
package/dist/Nymph.js
CHANGED
|
@@ -6,116 +6,264 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const conf_1 = require("./conf");
|
|
7
7
|
const Entity_1 = __importDefault(require("./Entity"));
|
|
8
8
|
const errors_1 = require("./errors");
|
|
9
|
+
/**
|
|
10
|
+
* An object relational mapper for Node.js.
|
|
11
|
+
*
|
|
12
|
+
* Written by Hunter Perrin for SciActive.
|
|
13
|
+
*
|
|
14
|
+
* @author Hunter Perrin <hperrin@gmail.com>
|
|
15
|
+
* @copyright SciActive Inc
|
|
16
|
+
* @see http://nymph.io/
|
|
17
|
+
*/
|
|
9
18
|
class Nymph {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
19
|
+
/**
|
|
20
|
+
* The Nymph config.
|
|
21
|
+
*/
|
|
22
|
+
config;
|
|
23
|
+
/**
|
|
24
|
+
* The Nymph instance that this one was cloned from, or null if it's not a
|
|
25
|
+
* clone.
|
|
26
|
+
*/
|
|
27
|
+
parent;
|
|
28
|
+
/**
|
|
29
|
+
* The Nymph database driver.
|
|
30
|
+
*/
|
|
31
|
+
driver;
|
|
32
|
+
/**
|
|
33
|
+
* An optional Tilmeld user/group manager instance.
|
|
34
|
+
*/
|
|
35
|
+
tilmeld = undefined;
|
|
36
|
+
/**
|
|
37
|
+
* A simple map of names to Entity classes.
|
|
38
|
+
*/
|
|
39
|
+
entityClasses = {};
|
|
40
|
+
/**
|
|
41
|
+
* The entity class for this instance of Nymph.
|
|
42
|
+
*/
|
|
43
|
+
Entity;
|
|
44
|
+
connectCallbacks = [];
|
|
45
|
+
disconnectCallbacks = [];
|
|
46
|
+
queryCallbacks = [];
|
|
47
|
+
beforeGetEntityCallbacks = [];
|
|
48
|
+
beforeGetEntitiesCallbacks = [];
|
|
49
|
+
beforeSaveEntityCallbacks = [];
|
|
50
|
+
afterSaveEntityCallbacks = [];
|
|
51
|
+
failedSaveEntityCallbacks = [];
|
|
52
|
+
beforeDeleteEntityCallbacks = [];
|
|
53
|
+
afterDeleteEntityCallbacks = [];
|
|
54
|
+
failedDeleteEntityCallbacks = [];
|
|
55
|
+
beforeDeleteEntityByIDCallbacks = [];
|
|
56
|
+
afterDeleteEntityByIDCallbacks = [];
|
|
57
|
+
failedDeleteEntityByIDCallbacks = [];
|
|
58
|
+
beforeNewUIDCallbacks = [];
|
|
59
|
+
afterNewUIDCallbacks = [];
|
|
60
|
+
failedNewUIDCallbacks = [];
|
|
61
|
+
beforeSetUIDCallbacks = [];
|
|
62
|
+
afterSetUIDCallbacks = [];
|
|
63
|
+
failedSetUIDCallbacks = [];
|
|
64
|
+
beforeRenameUIDCallbacks = [];
|
|
65
|
+
afterRenameUIDCallbacks = [];
|
|
66
|
+
failedRenameUIDCallbacks = [];
|
|
67
|
+
beforeDeleteUIDCallbacks = [];
|
|
68
|
+
afterDeleteUIDCallbacks = [];
|
|
69
|
+
failedDeleteUIDCallbacks = [];
|
|
70
|
+
beforeStartTransactionCallbacks = [];
|
|
71
|
+
afterStartTransactionCallbacks = [];
|
|
72
|
+
beforeCommitTransactionCallbacks = [];
|
|
73
|
+
afterCommitTransactionCallbacks = [];
|
|
74
|
+
beforeRollbackTransactionCallbacks = [];
|
|
75
|
+
afterRollbackTransactionCallbacks = [];
|
|
76
|
+
/**
|
|
77
|
+
* Initialize Nymph.
|
|
78
|
+
*
|
|
79
|
+
* @param config The Nymph configuration.
|
|
80
|
+
* @param driver The Nymph database driver.
|
|
81
|
+
* @param tilmeld The Tilmeld user/group manager instance, if you want to use it.
|
|
82
|
+
* @param parent Used internally by Nymph. Don't set this.
|
|
83
|
+
*/
|
|
84
|
+
constructor(config, driver, tilmeld, parent) {
|
|
49
85
|
this.config = { ...conf_1.ConfigDefaults, ...config };
|
|
50
86
|
this.driver = driver;
|
|
87
|
+
this.parent = parent ?? null;
|
|
51
88
|
this.Entity = this.addEntityClass(Entity_1.default);
|
|
52
|
-
if (
|
|
89
|
+
if (parent) {
|
|
90
|
+
for (const name in parent.entityClasses) {
|
|
91
|
+
if (name === 'Entity' ||
|
|
92
|
+
parent.entityClasses[name].skipOnClone) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
this.addEntityClass(parent.entityClasses[name]);
|
|
96
|
+
}
|
|
97
|
+
const events = [
|
|
98
|
+
'connect',
|
|
99
|
+
'disconnect',
|
|
100
|
+
'query',
|
|
101
|
+
'beforeGetEntity',
|
|
102
|
+
'beforeGetEntities',
|
|
103
|
+
'beforeSaveEntity',
|
|
104
|
+
'afterSaveEntity',
|
|
105
|
+
'failedSaveEntity',
|
|
106
|
+
'beforeDeleteEntity',
|
|
107
|
+
'afterDeleteEntity',
|
|
108
|
+
'failedDeleteEntity',
|
|
109
|
+
'beforeDeleteEntityByID',
|
|
110
|
+
'afterDeleteEntityByID',
|
|
111
|
+
'failedDeleteEntityByID',
|
|
112
|
+
'beforeNewUID',
|
|
113
|
+
'afterNewUID',
|
|
114
|
+
'failedNewUID',
|
|
115
|
+
'beforeSetUID',
|
|
116
|
+
'afterSetUID',
|
|
117
|
+
'failedSetUID',
|
|
118
|
+
'beforeRenameUID',
|
|
119
|
+
'afterRenameUID',
|
|
120
|
+
'failedRenameUID',
|
|
121
|
+
'beforeDeleteUID',
|
|
122
|
+
'afterDeleteUID',
|
|
123
|
+
'failedDeleteUID',
|
|
124
|
+
'beforeStartTransaction',
|
|
125
|
+
'afterStartTransaction',
|
|
126
|
+
'beforeCommitTransaction',
|
|
127
|
+
'afterCommitTransaction',
|
|
128
|
+
'beforeRollbackTransaction',
|
|
129
|
+
'afterRollbackTransaction',
|
|
130
|
+
];
|
|
131
|
+
for (let event of events) {
|
|
132
|
+
const prop = event + 'Callbacks';
|
|
133
|
+
// @ts-ignore: The callback should be the right type here.
|
|
134
|
+
const callbacks = parent[prop];
|
|
135
|
+
for (let callback of callbacks) {
|
|
136
|
+
if (callback.skipOnClone) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
this.on(event, callback);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
this.config.debugLog('nymph', 'Nymph loaded.');
|
|
144
|
+
if (tilmeld != null) {
|
|
53
145
|
this.tilmeld = tilmeld;
|
|
54
146
|
}
|
|
55
147
|
this.driver.init(this);
|
|
148
|
+
this.config.debugLog('nymph', 'Nymph driver loaded.');
|
|
56
149
|
if (this.tilmeld) {
|
|
57
150
|
this.tilmeld.init(this);
|
|
151
|
+
this.config.debugLog('nymph', 'Tilmeld loaded.');
|
|
58
152
|
}
|
|
59
153
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
'
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
'afterDeleteEntityByID',
|
|
80
|
-
'beforeNewUID',
|
|
81
|
-
'afterNewUID',
|
|
82
|
-
'beforeSetUID',
|
|
83
|
-
'afterSetUID',
|
|
84
|
-
'beforeRenameUID',
|
|
85
|
-
'afterRenameUID',
|
|
86
|
-
'beforeDeleteUID',
|
|
87
|
-
'afterDeleteUID',
|
|
88
|
-
];
|
|
89
|
-
for (let event of events) {
|
|
90
|
-
const prop = event + 'Callbacks';
|
|
91
|
-
const callbacks = this[prop];
|
|
92
|
-
for (let callback of callbacks) {
|
|
93
|
-
if (callback.skipOnClone) {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
nymph.on(event, callback);
|
|
154
|
+
/**
|
|
155
|
+
* Add your class to this instance.
|
|
156
|
+
*
|
|
157
|
+
* This will create a class that extends your class within this instance of
|
|
158
|
+
* Nymph and return it. You can then use this class's constructor and methods,
|
|
159
|
+
* which will use this instance of Nymph.
|
|
160
|
+
*
|
|
161
|
+
* Because this creates a subclass, don't use the class
|
|
162
|
+
* returned from `getEntityClass` to check with `instanceof`.
|
|
163
|
+
*/
|
|
164
|
+
addEntityClass(EntityClass) {
|
|
165
|
+
if (EntityClass.class in this.entityClasses) {
|
|
166
|
+
this.config.debugLog('nymph', `Adding duplicate class "${EntityClass.class}". This may be a mistake.`);
|
|
167
|
+
}
|
|
168
|
+
// Check that it doesn't have the same etype as any other class.
|
|
169
|
+
for (const ExistingClass of Object.values(this.entityClasses)) {
|
|
170
|
+
if (EntityClass.ETYPE === ExistingClass.ETYPE &&
|
|
171
|
+
EntityClass.class !== EntityClass.class) {
|
|
172
|
+
this.config.debugLog('nymph', `Adding class "${EntityClass.class}" with same etype as existing class "${ExistingClass.class}". This may be a mistake.`);
|
|
97
173
|
}
|
|
98
174
|
}
|
|
99
|
-
|
|
175
|
+
const nymph = this;
|
|
176
|
+
this.entityClasses[EntityClass.class] = class extends EntityClass {
|
|
177
|
+
static nymph = nymph;
|
|
178
|
+
constructor(...args) {
|
|
179
|
+
super(...args);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
return this.entityClasses[EntityClass.class];
|
|
100
183
|
}
|
|
184
|
+
getEntityClass(className) {
|
|
185
|
+
let key = null;
|
|
186
|
+
if (typeof className === 'string') {
|
|
187
|
+
key = className;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
key = className.class;
|
|
191
|
+
}
|
|
192
|
+
if (key in this.entityClasses) {
|
|
193
|
+
return this.entityClasses[key];
|
|
194
|
+
}
|
|
195
|
+
this.config.debugError('nymph', `Tried to use missing class "${key}".`);
|
|
196
|
+
throw new errors_1.ClassNotAvailableError(`Tried to use missing class "${key}".`);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Get the class that uses the specified etype.
|
|
200
|
+
*
|
|
201
|
+
* Note that it is fine, though unusual, for two classes to use the same
|
|
202
|
+
* etype. However, this can lead to very hard to diagnose bugs, so is
|
|
203
|
+
* generally discouraged.
|
|
204
|
+
*/
|
|
205
|
+
getEntityClassByEtype(etype) {
|
|
206
|
+
for (const EntityClass of Object.values(this.entityClasses)) {
|
|
207
|
+
if (EntityClass.ETYPE === etype) {
|
|
208
|
+
return EntityClass;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
this.config.debugError('nymph', `Tried to use missing class by etype "${etype}".`);
|
|
212
|
+
throw new errors_1.ClassNotAvailableError(`Tried to use missing class by etype "${etype}".`);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Get a clone of this instance with cloned classes and event listeners.
|
|
216
|
+
*
|
|
217
|
+
* @returns A clone of this instance.
|
|
218
|
+
*/
|
|
219
|
+
clone() {
|
|
220
|
+
return new Nymph(this.config, this.driver.clone(), this.tilmeld?.clone(), this);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Connect to the database.
|
|
224
|
+
*
|
|
225
|
+
* @returns Whether the instance is connected to the database.
|
|
226
|
+
*/
|
|
101
227
|
async connect() {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
228
|
+
try {
|
|
229
|
+
const result = this.driver.connect();
|
|
230
|
+
for (let callback of this.connectCallbacks) {
|
|
231
|
+
if (callback) {
|
|
232
|
+
await callback(this, result);
|
|
233
|
+
}
|
|
106
234
|
}
|
|
235
|
+
this.config.debugLog('nymph', 'Driver connected.');
|
|
236
|
+
return await result;
|
|
237
|
+
}
|
|
238
|
+
catch (e) {
|
|
239
|
+
this.config.debugError('nymph', `Failed to connect: ${e}`);
|
|
240
|
+
throw e;
|
|
107
241
|
}
|
|
108
|
-
return await result;
|
|
109
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Disconnect from the database.
|
|
245
|
+
*
|
|
246
|
+
* @returns Whether the instance is connected to the database.
|
|
247
|
+
*/
|
|
110
248
|
async disconnect() {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
249
|
+
try {
|
|
250
|
+
const result = this.driver.disconnect();
|
|
251
|
+
for (let callback of this.disconnectCallbacks) {
|
|
252
|
+
if (callback) {
|
|
253
|
+
await callback(this, result);
|
|
254
|
+
}
|
|
115
255
|
}
|
|
256
|
+
this.config.debugLog('nymph', 'Driver disconnected.');
|
|
257
|
+
return await result;
|
|
258
|
+
}
|
|
259
|
+
catch (e) {
|
|
260
|
+
this.config.debugError('nymph', `Failed to disconnect: ${e}`);
|
|
261
|
+
throw e;
|
|
116
262
|
}
|
|
117
|
-
return await result;
|
|
118
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Run all the query callbacks on a query.
|
|
266
|
+
*/
|
|
119
267
|
runQueryCallbacks(options, selectors) {
|
|
120
268
|
for (let callback of this.queryCallbacks) {
|
|
121
269
|
if (callback) {
|
|
@@ -123,160 +271,519 @@ class Nymph {
|
|
|
123
271
|
}
|
|
124
272
|
}
|
|
125
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Start an atomic transaction and returns a new instance of Nymph.
|
|
276
|
+
*
|
|
277
|
+
* All proceeding changes using this new instance will wait to be written to
|
|
278
|
+
* the database's permanent storage until commit() is called. You can also
|
|
279
|
+
* undo all the changes since this function ran with rollback().
|
|
280
|
+
*
|
|
281
|
+
* Transactions will nest as long as every name is unique. Internally, Nymph
|
|
282
|
+
* uses names prefixed with "nymph-".
|
|
283
|
+
*
|
|
284
|
+
* @returns A new instance of Nymph that should be used for the transaction.
|
|
285
|
+
*/
|
|
126
286
|
async startTransaction(name) {
|
|
127
|
-
|
|
287
|
+
try {
|
|
288
|
+
for (let callback of this.beforeStartTransactionCallbacks) {
|
|
289
|
+
if (callback) {
|
|
290
|
+
await callback(this, name);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
this.config.debugLog('nymph', `Starting transaction "${name}".`);
|
|
294
|
+
const result = await this.driver.startTransaction(name);
|
|
295
|
+
for (let callback of this.afterStartTransactionCallbacks) {
|
|
296
|
+
if (callback) {
|
|
297
|
+
await callback(this, name, result);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return result;
|
|
301
|
+
}
|
|
302
|
+
catch (e) {
|
|
303
|
+
this.config.debugError('nymph', `Failed to start transaction: ${e}`);
|
|
304
|
+
throw e;
|
|
305
|
+
}
|
|
128
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* Commit the named transaction.
|
|
309
|
+
*
|
|
310
|
+
* After this is called, the transaction instance should be discarded.
|
|
311
|
+
*
|
|
312
|
+
* @returns True on success, false on failure.
|
|
313
|
+
*/
|
|
129
314
|
async commit(name) {
|
|
130
|
-
|
|
315
|
+
try {
|
|
316
|
+
for (let callback of this.beforeCommitTransactionCallbacks) {
|
|
317
|
+
if (callback) {
|
|
318
|
+
await callback(this, name);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
this.config.debugLog('nymph', `Committing transaction "${name}".`);
|
|
322
|
+
const result = await this.driver.commit(name);
|
|
323
|
+
for (let callback of this.afterCommitTransactionCallbacks) {
|
|
324
|
+
if (callback) {
|
|
325
|
+
await callback(this, name, result);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
catch (e) {
|
|
331
|
+
this.config.debugError('nymph', `Failed to commit transaction: ${e}`);
|
|
332
|
+
throw e;
|
|
333
|
+
}
|
|
131
334
|
}
|
|
335
|
+
/**
|
|
336
|
+
* Rollback the named transaction.
|
|
337
|
+
*
|
|
338
|
+
* After this is called, the transaction instance should be discarded.
|
|
339
|
+
*
|
|
340
|
+
* @returns True on success, false on failure.
|
|
341
|
+
*/
|
|
132
342
|
async rollback(name) {
|
|
133
|
-
|
|
343
|
+
try {
|
|
344
|
+
for (let callback of this.beforeRollbackTransactionCallbacks) {
|
|
345
|
+
if (callback) {
|
|
346
|
+
await callback(this, name);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
this.config.debugLog('nymph', `Rolling back transaction "${name}".`);
|
|
350
|
+
const result = await this.driver.rollback(name);
|
|
351
|
+
for (let callback of this.afterRollbackTransactionCallbacks) {
|
|
352
|
+
if (callback) {
|
|
353
|
+
await callback(this, name, result);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return result;
|
|
357
|
+
}
|
|
358
|
+
catch (e) {
|
|
359
|
+
this.config.debugError('nymph', `Failed to roll back transaction: ${e}`);
|
|
360
|
+
throw e;
|
|
361
|
+
}
|
|
134
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Check if there is any open transaction.
|
|
365
|
+
*
|
|
366
|
+
* @returns True if there is a transaction.
|
|
367
|
+
*/
|
|
135
368
|
async inTransaction() {
|
|
136
369
|
return await this.driver.inTransaction();
|
|
137
370
|
}
|
|
371
|
+
/**
|
|
372
|
+
* Increment or create a unique ID and return the new value.
|
|
373
|
+
*
|
|
374
|
+
* Unique IDs, or UIDs are similar to GUIDs, but numeric and sequential.
|
|
375
|
+
*
|
|
376
|
+
* A UID can be used to identify an object when the GUID doesn't suffice. On
|
|
377
|
+
* a system where a new entity is created many times per second, referring
|
|
378
|
+
* to something by its GUID may be unintuitive. However, the component
|
|
379
|
+
* designer is responsible for assigning UIDs to the component's entities.
|
|
380
|
+
* Beware that if a UID is incremented for an entity, and the entity cannot
|
|
381
|
+
* be saved, there is no safe, and therefore, no recommended way to
|
|
382
|
+
* decrement the UID back to its previous value.
|
|
383
|
+
*
|
|
384
|
+
* If newUID() is passed the name of a UID which does not exist yet, one
|
|
385
|
+
* will be created with that name, and assigned the value 1. If the UID
|
|
386
|
+
* already exists, its value will be incremented. The new value will be
|
|
387
|
+
* returned.
|
|
388
|
+
*
|
|
389
|
+
* @param name The UID's name.
|
|
390
|
+
* @returns The UID's new value, or null on failure.
|
|
391
|
+
*/
|
|
138
392
|
async newUID(name) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
393
|
+
try {
|
|
394
|
+
for (let callback of this.beforeNewUIDCallbacks) {
|
|
395
|
+
if (callback) {
|
|
396
|
+
await callback(this, name);
|
|
397
|
+
}
|
|
142
398
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (callback) {
|
|
147
|
-
await callback(this, result);
|
|
399
|
+
let result;
|
|
400
|
+
try {
|
|
401
|
+
result = this.driver.newUID(name);
|
|
148
402
|
}
|
|
403
|
+
catch (e) {
|
|
404
|
+
for (let callback of this.failedNewUIDCallbacks) {
|
|
405
|
+
if (callback) {
|
|
406
|
+
await callback(this, e);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
throw e;
|
|
410
|
+
}
|
|
411
|
+
for (let callback of this.afterNewUIDCallbacks) {
|
|
412
|
+
if (callback) {
|
|
413
|
+
await callback(this, result);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return await result;
|
|
417
|
+
}
|
|
418
|
+
catch (e) {
|
|
419
|
+
this.config.debugError('nymph', `Failed to create UID "${name}": ${e}`);
|
|
420
|
+
throw e;
|
|
149
421
|
}
|
|
150
|
-
return await result;
|
|
151
422
|
}
|
|
423
|
+
/**
|
|
424
|
+
* Get the current value of a unique ID.
|
|
425
|
+
* @param name The UID's name.
|
|
426
|
+
* @returns The UID's value, or null on failure and if it doesn't exist.
|
|
427
|
+
*/
|
|
152
428
|
async getUID(name) {
|
|
153
|
-
|
|
429
|
+
try {
|
|
430
|
+
return await this.driver.getUID(name);
|
|
431
|
+
}
|
|
432
|
+
catch (e) {
|
|
433
|
+
this.config.debugError('nymph', `Failed to get UID "${name}": ${e}`);
|
|
434
|
+
throw e;
|
|
435
|
+
}
|
|
154
436
|
}
|
|
437
|
+
/**
|
|
438
|
+
* Set the value of a UID.
|
|
439
|
+
*
|
|
440
|
+
* @param name The UID's name.
|
|
441
|
+
* @param value The value.
|
|
442
|
+
* @returns True on success, false on failure.
|
|
443
|
+
*/
|
|
155
444
|
async setUID(name, value) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
445
|
+
try {
|
|
446
|
+
for (let callback of this.beforeSetUIDCallbacks) {
|
|
447
|
+
if (callback) {
|
|
448
|
+
await callback(this, name, value);
|
|
449
|
+
}
|
|
159
450
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
451
|
+
let result;
|
|
452
|
+
try {
|
|
453
|
+
result = this.driver.setUID(name, value);
|
|
454
|
+
}
|
|
455
|
+
catch (e) {
|
|
456
|
+
for (let callback of this.failedSetUIDCallbacks) {
|
|
457
|
+
if (callback) {
|
|
458
|
+
await callback(this, e);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
throw e;
|
|
165
462
|
}
|
|
463
|
+
for (let callback of this.afterSetUIDCallbacks) {
|
|
464
|
+
if (callback) {
|
|
465
|
+
await callback(this, result);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return await result;
|
|
469
|
+
}
|
|
470
|
+
catch (e) {
|
|
471
|
+
this.config.debugError('nymph', `Failed to set UID "${name}": ${e}`);
|
|
472
|
+
throw e;
|
|
166
473
|
}
|
|
167
|
-
return await result;
|
|
168
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* Delete a unique ID.
|
|
477
|
+
*
|
|
478
|
+
* @param name The UID's name.
|
|
479
|
+
* @returns True on success, false on failure.
|
|
480
|
+
*/
|
|
169
481
|
async deleteUID(name) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
482
|
+
try {
|
|
483
|
+
for (let callback of this.beforeDeleteUIDCallbacks) {
|
|
484
|
+
if (callback) {
|
|
485
|
+
await callback(this, name);
|
|
486
|
+
}
|
|
173
487
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
488
|
+
let result;
|
|
489
|
+
try {
|
|
490
|
+
result = this.driver.deleteUID(name);
|
|
491
|
+
}
|
|
492
|
+
catch (e) {
|
|
493
|
+
for (let callback of this.failedDeleteUIDCallbacks) {
|
|
494
|
+
if (callback) {
|
|
495
|
+
await callback(this, e);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
throw e;
|
|
179
499
|
}
|
|
500
|
+
for (let callback of this.afterDeleteUIDCallbacks) {
|
|
501
|
+
if (callback) {
|
|
502
|
+
await callback(this, result);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return await result;
|
|
506
|
+
}
|
|
507
|
+
catch (e) {
|
|
508
|
+
this.config.debugError('nymph', `Failed to delete UID "${name}": ${e}`);
|
|
509
|
+
throw e;
|
|
180
510
|
}
|
|
181
|
-
return await result;
|
|
182
511
|
}
|
|
512
|
+
/**
|
|
513
|
+
* Rename a unique ID.
|
|
514
|
+
*
|
|
515
|
+
* @param oldName The old name.
|
|
516
|
+
* @param newName The new name.
|
|
517
|
+
* @returns True on success, false on failure.
|
|
518
|
+
*/
|
|
183
519
|
async renameUID(oldName, newName) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
520
|
+
try {
|
|
521
|
+
for (let callback of this.beforeRenameUIDCallbacks) {
|
|
522
|
+
if (callback) {
|
|
523
|
+
await callback(this, oldName, newName);
|
|
524
|
+
}
|
|
187
525
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
526
|
+
let result;
|
|
527
|
+
try {
|
|
528
|
+
result = this.driver.renameUID(oldName, newName);
|
|
529
|
+
}
|
|
530
|
+
catch (e) {
|
|
531
|
+
for (let callback of this.failedRenameUIDCallbacks) {
|
|
532
|
+
if (callback) {
|
|
533
|
+
await callback(this, e);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
throw e;
|
|
193
537
|
}
|
|
538
|
+
for (let callback of this.afterRenameUIDCallbacks) {
|
|
539
|
+
if (callback) {
|
|
540
|
+
await callback(this, result);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return await result;
|
|
544
|
+
}
|
|
545
|
+
catch (e) {
|
|
546
|
+
this.config.debugError('nymph', `Failed to rename UID "${oldName}" to "${newName}": ${e}`);
|
|
547
|
+
throw e;
|
|
194
548
|
}
|
|
195
|
-
return await result;
|
|
196
549
|
}
|
|
550
|
+
/**
|
|
551
|
+
* Save an entity to the database.
|
|
552
|
+
*
|
|
553
|
+
* If the entity has never been saved (has no GUID), a variable "cdate"
|
|
554
|
+
* is set on it with the current Unix timestamp.
|
|
555
|
+
*
|
|
556
|
+
* The variable "mdate" is set to the current Unix timestamp.
|
|
557
|
+
*
|
|
558
|
+
* @param entity The entity.
|
|
559
|
+
* @returns True on success, false on failure.
|
|
560
|
+
*/
|
|
197
561
|
async saveEntity(entity) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
562
|
+
try {
|
|
563
|
+
for (let callback of this.beforeSaveEntityCallbacks) {
|
|
564
|
+
if (callback) {
|
|
565
|
+
await callback(this, entity);
|
|
566
|
+
}
|
|
201
567
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
568
|
+
let result;
|
|
569
|
+
try {
|
|
570
|
+
result = this.driver.saveEntity(entity);
|
|
571
|
+
}
|
|
572
|
+
catch (e) {
|
|
573
|
+
for (let callback of this.failedSaveEntityCallbacks) {
|
|
574
|
+
if (callback) {
|
|
575
|
+
await callback(this, e);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
throw e;
|
|
579
|
+
}
|
|
580
|
+
for (let callback of this.afterSaveEntityCallbacks) {
|
|
581
|
+
if (callback) {
|
|
582
|
+
await callback(this, result);
|
|
583
|
+
}
|
|
207
584
|
}
|
|
585
|
+
return await result;
|
|
586
|
+
}
|
|
587
|
+
catch (e) {
|
|
588
|
+
this.config.debugError('nymph', `Failed to save entity: ${e}`);
|
|
589
|
+
throw e;
|
|
208
590
|
}
|
|
209
|
-
return await result;
|
|
210
591
|
}
|
|
211
592
|
async getEntities(options = {}, ...selectors) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
593
|
+
try {
|
|
594
|
+
for (let callback of this.beforeGetEntitiesCallbacks) {
|
|
595
|
+
if (callback) {
|
|
596
|
+
await callback(this, options, selectors);
|
|
597
|
+
}
|
|
215
598
|
}
|
|
599
|
+
return await this.driver.getEntities(options, ...selectors);
|
|
600
|
+
}
|
|
601
|
+
catch (e) {
|
|
602
|
+
this.config.debugError('nymph', `Failed to get entities: ${e}`);
|
|
603
|
+
throw e;
|
|
216
604
|
}
|
|
217
|
-
return await this.driver.getEntities(options, ...selectors);
|
|
218
605
|
}
|
|
219
606
|
async getEntity(options = {}, ...selectors) {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
for (let callback of this.beforeGetEntityCallbacks) {
|
|
225
|
-
if (callback) {
|
|
226
|
-
await callback(this, options, selectors);
|
|
607
|
+
try {
|
|
608
|
+
// Set up options and selectors.
|
|
609
|
+
if (typeof selectors[0] === 'string') {
|
|
610
|
+
selectors = [{ type: '&', guid: selectors[0] }];
|
|
227
611
|
}
|
|
612
|
+
options.limit = 1;
|
|
613
|
+
for (let callback of this.beforeGetEntityCallbacks) {
|
|
614
|
+
if (callback) {
|
|
615
|
+
await callback(this, options, selectors);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
const entities = await this.driver.getEntities(options, ...selectors);
|
|
619
|
+
if (options.return === 'count') {
|
|
620
|
+
return entities;
|
|
621
|
+
}
|
|
622
|
+
if (!entities || !entities.length) {
|
|
623
|
+
return null;
|
|
624
|
+
}
|
|
625
|
+
return entities[0];
|
|
228
626
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
if (!entities || !entities.length) {
|
|
234
|
-
return null;
|
|
627
|
+
catch (e) {
|
|
628
|
+
this.config.debugError('nymph', `Failed to get entity: ${e}`);
|
|
629
|
+
throw e;
|
|
235
630
|
}
|
|
236
|
-
return entities[0];
|
|
237
631
|
}
|
|
632
|
+
/**
|
|
633
|
+
* Delete an entity from the database.
|
|
634
|
+
*
|
|
635
|
+
* @param entity The entity.
|
|
636
|
+
* @returns True on success, false on failure.
|
|
637
|
+
*/
|
|
238
638
|
async deleteEntity(entity) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
639
|
+
try {
|
|
640
|
+
for (let callback of this.beforeDeleteEntityCallbacks) {
|
|
641
|
+
if (callback) {
|
|
642
|
+
await callback(this, entity);
|
|
643
|
+
}
|
|
242
644
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
645
|
+
let result;
|
|
646
|
+
try {
|
|
647
|
+
result = this.driver.deleteEntity(entity);
|
|
648
|
+
}
|
|
649
|
+
catch (e) {
|
|
650
|
+
for (let callback of this.failedDeleteEntityCallbacks) {
|
|
651
|
+
if (callback) {
|
|
652
|
+
await callback(this, e);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
throw e;
|
|
656
|
+
}
|
|
657
|
+
for (let callback of this.afterDeleteEntityCallbacks) {
|
|
658
|
+
if (callback) {
|
|
659
|
+
await callback(this, result);
|
|
660
|
+
}
|
|
248
661
|
}
|
|
662
|
+
return await result;
|
|
663
|
+
}
|
|
664
|
+
catch (e) {
|
|
665
|
+
this.config.debugError('nymph', `Failed to delete entity: ${e}`);
|
|
666
|
+
throw e;
|
|
249
667
|
}
|
|
250
|
-
return await result;
|
|
251
668
|
}
|
|
669
|
+
/**
|
|
670
|
+
* Delete an entity from the database by its GUID.
|
|
671
|
+
*
|
|
672
|
+
* @param guid The entity's GUID.
|
|
673
|
+
* @param className The entity's class name.
|
|
674
|
+
* @returns True on success, false on failure.
|
|
675
|
+
*/
|
|
252
676
|
async deleteEntityByID(guid, className) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
677
|
+
try {
|
|
678
|
+
for (let callback of this.beforeDeleteEntityByIDCallbacks) {
|
|
679
|
+
if (callback) {
|
|
680
|
+
await callback(this, guid, className);
|
|
681
|
+
}
|
|
256
682
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
683
|
+
let result;
|
|
684
|
+
try {
|
|
685
|
+
result = this.driver.deleteEntityByID(guid, className);
|
|
686
|
+
}
|
|
687
|
+
catch (e) {
|
|
688
|
+
for (let callback of this.failedDeleteEntityByIDCallbacks) {
|
|
689
|
+
if (callback) {
|
|
690
|
+
await callback(this, e);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
throw e;
|
|
262
694
|
}
|
|
695
|
+
for (let callback of this.afterDeleteEntityByIDCallbacks) {
|
|
696
|
+
if (callback) {
|
|
697
|
+
await callback(this, result);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
return await result;
|
|
701
|
+
}
|
|
702
|
+
catch (e) {
|
|
703
|
+
this.config.debugError('nymph', `Failed to delete entity by ID: ${e}`);
|
|
704
|
+
throw e;
|
|
263
705
|
}
|
|
264
|
-
return await result;
|
|
265
706
|
}
|
|
707
|
+
/**
|
|
708
|
+
* Export entities to a local file.
|
|
709
|
+
*
|
|
710
|
+
* This is the file format:
|
|
711
|
+
*
|
|
712
|
+
* ```
|
|
713
|
+
* #nex2
|
|
714
|
+
* # The above line must be the first thing in the file.
|
|
715
|
+
* # Comments begin with #
|
|
716
|
+
* # And can have white space before them.
|
|
717
|
+
* # This defines a UID.
|
|
718
|
+
* <name/of/uid>[5]
|
|
719
|
+
* <another uid>[8000]
|
|
720
|
+
* # For UIDs, the name is in angle brackets (<>) and the value follows
|
|
721
|
+
* # in square brackets ([]).
|
|
722
|
+
* # This starts a new entity.
|
|
723
|
+
* {1234abcd}<etype>[tag,list,with,commas]
|
|
724
|
+
* # For entities, the GUID is in curly brackets ({}), then the etype in
|
|
725
|
+
* # angle brackets, then the comma separated tag list follows in square
|
|
726
|
+
* # brackets ([]).
|
|
727
|
+
* # Properties are stored like this:
|
|
728
|
+
* # propname=JSON.stringify(value)
|
|
729
|
+
* abilities=["system/admin"]
|
|
730
|
+
* groups=[]
|
|
731
|
+
* inheritAbilities=false
|
|
732
|
+
* name="admin"
|
|
733
|
+
* # White space before/after "=" and at beginning/end of line is ignored.
|
|
734
|
+
* username = "admin"
|
|
735
|
+
* {2}<etype>[tag,list]
|
|
736
|
+
* another="This is another entity."
|
|
737
|
+
* newline="\n"
|
|
738
|
+
* ```
|
|
739
|
+
*
|
|
740
|
+
* @param filename The file to export to.
|
|
741
|
+
* @returns True on success, false on failure.
|
|
742
|
+
*/
|
|
266
743
|
async export(filename) {
|
|
267
|
-
|
|
744
|
+
try {
|
|
745
|
+
return await this.driver.export(filename);
|
|
746
|
+
}
|
|
747
|
+
catch (e) {
|
|
748
|
+
this.config.debugError('nymph', `Failed to export: ${e}`);
|
|
749
|
+
throw e;
|
|
750
|
+
}
|
|
268
751
|
}
|
|
752
|
+
/**
|
|
753
|
+
* Export entities to the console.
|
|
754
|
+
*
|
|
755
|
+
* @returns True on success, false on failure.
|
|
756
|
+
*/
|
|
269
757
|
async exportPrint() {
|
|
270
|
-
|
|
758
|
+
try {
|
|
759
|
+
return await this.driver.exportPrint();
|
|
760
|
+
}
|
|
761
|
+
catch (e) {
|
|
762
|
+
this.config.debugError('nymph', `Failed to export: ${e}`);
|
|
763
|
+
throw e;
|
|
764
|
+
}
|
|
271
765
|
}
|
|
766
|
+
/**
|
|
767
|
+
* Import entities from a file.
|
|
768
|
+
*
|
|
769
|
+
* @param filename The file to import from.
|
|
770
|
+
* @returns True on success, false on failure.
|
|
771
|
+
*/
|
|
272
772
|
async import(filename) {
|
|
273
|
-
|
|
773
|
+
try {
|
|
774
|
+
return await this.driver.import(filename);
|
|
775
|
+
}
|
|
776
|
+
catch (e) {
|
|
777
|
+
this.config.debugError('nymph', `Failed to import: ${e}`);
|
|
778
|
+
throw e;
|
|
779
|
+
}
|
|
274
780
|
}
|
|
275
781
|
on(event, callback) {
|
|
276
782
|
const prop = (event + 'Callbacks');
|
|
277
783
|
if (!(prop in this)) {
|
|
278
784
|
throw new Error('Invalid event type.');
|
|
279
785
|
}
|
|
786
|
+
// @ts-ignore: The callback should be the right type here.
|
|
280
787
|
this[prop].push(callback);
|
|
281
788
|
return () => this.off(event, callback);
|
|
282
789
|
}
|
|
@@ -285,8 +792,10 @@ class Nymph {
|
|
|
285
792
|
if (!(prop in this)) {
|
|
286
793
|
return false;
|
|
287
794
|
}
|
|
795
|
+
// @ts-ignore: The callback should always be the right type here.
|
|
288
796
|
const i = this[prop].indexOf(callback);
|
|
289
797
|
if (i > -1) {
|
|
798
|
+
// @ts-ignore: The callback should always be the right type here.
|
|
290
799
|
this[prop].splice(i, 1);
|
|
291
800
|
}
|
|
292
801
|
return true;
|