@nymphjs/nymph 1.0.0-beta.9 → 1.0.0-beta.90

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +432 -0
  2. package/README.md +10 -20
  3. package/dist/Entity.d.ts +277 -10
  4. package/dist/Entity.js +543 -148
  5. package/dist/Entity.js.map +1 -1
  6. package/dist/Entity.test.js +53 -13
  7. package/dist/Entity.test.js.map +1 -1
  8. package/dist/Entity.types.d.ts +249 -23
  9. package/dist/Entity.types.js +1 -2
  10. package/dist/Nymph.d.ts +431 -12
  11. package/dist/Nymph.js +692 -180
  12. package/dist/Nymph.js.map +1 -1
  13. package/dist/Nymph.types.d.ts +65 -7
  14. package/dist/Nymph.types.js +3 -5
  15. package/dist/Nymph.types.js.map +1 -1
  16. package/dist/conf/d.d.ts +43 -0
  17. package/dist/conf/d.js +1 -2
  18. package/dist/conf/defaults.d.ts +1 -1
  19. package/dist/conf/defaults.js +26 -3
  20. package/dist/conf/defaults.js.map +1 -1
  21. package/dist/conf/index.d.ts +2 -2
  22. package/dist/conf/index.js +2 -8
  23. package/dist/conf/index.js.map +1 -1
  24. package/dist/driver/NymphDriver.d.ts +137 -39
  25. package/dist/driver/NymphDriver.js +344 -194
  26. package/dist/driver/NymphDriver.js.map +1 -1
  27. package/dist/driver/index.d.ts +2 -2
  28. package/dist/driver/index.js +3 -23
  29. package/dist/driver/index.js.map +1 -1
  30. package/dist/driver/testArtifacts.d.ts +2 -2
  31. package/dist/driver/testArtifacts.js +557 -92
  32. package/dist/driver/testArtifacts.js.map +1 -1
  33. package/dist/errors/ClassNotAvailableError.js +1 -5
  34. package/dist/errors/ClassNotAvailableError.js.map +1 -1
  35. package/dist/errors/EntityConflictError.js +1 -5
  36. package/dist/errors/EntityConflictError.js.map +1 -1
  37. package/dist/errors/EntityInvalidDataError.d.ts +6 -0
  38. package/dist/errors/EntityInvalidDataError.js +8 -6
  39. package/dist/errors/EntityInvalidDataError.js.map +1 -1
  40. package/dist/errors/EntityIsSleepingReferenceError.js +1 -5
  41. package/dist/errors/EntityIsSleepingReferenceError.js.map +1 -1
  42. package/dist/errors/EntityUniqueConstraintError.d.ts +3 -0
  43. package/dist/errors/EntityUniqueConstraintError.js +7 -0
  44. package/dist/errors/EntityUniqueConstraintError.js.map +1 -0
  45. package/dist/errors/InvalidParametersError.js +1 -5
  46. package/dist/errors/InvalidParametersError.js.map +1 -1
  47. package/dist/errors/InvalidStateError.d.ts +3 -0
  48. package/dist/errors/InvalidStateError.js +7 -0
  49. package/dist/errors/InvalidStateError.js.map +1 -0
  50. package/dist/errors/NotConfiguredError.js +1 -5
  51. package/dist/errors/NotConfiguredError.js.map +1 -1
  52. package/dist/errors/QueryFailedError.js +2 -5
  53. package/dist/errors/QueryFailedError.js.map +1 -1
  54. package/dist/errors/UnableToConnectError.js +1 -5
  55. package/dist/errors/UnableToConnectError.js.map +1 -1
  56. package/dist/errors/index.d.ts +10 -8
  57. package/dist/errors/index.js +10 -24
  58. package/dist/errors/index.js.map +1 -1
  59. package/dist/index.d.ts +11 -11
  60. package/dist/index.js +14 -34
  61. package/dist/index.js.map +1 -1
  62. package/dist/nymphJoiProps.js +10 -15
  63. package/dist/nymphJoiProps.js.map +1 -1
  64. package/dist/testArtifacts.d.ts +18 -6
  65. package/dist/testArtifacts.js +32 -37
  66. package/dist/testArtifacts.js.map +1 -1
  67. package/dist/testMocks.d.ts +15 -12
  68. package/dist/testMocks.js +40 -29
  69. package/dist/testMocks.js.map +1 -1
  70. package/dist/utils.d.ts +4 -4
  71. package/dist/utils.js +40 -29
  72. package/dist/utils.js.map +1 -1
  73. package/jest.config.js +11 -2
  74. package/package.json +22 -19
  75. package/src/Entity.test.ts +52 -7
  76. package/src/Entity.ts +458 -147
  77. package/src/Entity.types.ts +64 -74
  78. package/src/Nymph.ts +754 -321
  79. package/src/Nymph.types.ts +131 -26
  80. package/src/conf/d.ts +25 -0
  81. package/src/conf/defaults.ts +34 -1
  82. package/src/conf/index.ts +2 -2
  83. package/src/driver/NymphDriver.ts +362 -359
  84. package/src/driver/index.ts +2 -2
  85. package/src/driver/testArtifacts.ts +609 -185
  86. package/src/errors/EntityUniqueConstraintError.ts +6 -0
  87. package/src/errors/InvalidStateError.ts +6 -0
  88. package/src/errors/index.ts +12 -8
  89. package/src/index.ts +11 -11
  90. package/src/nymphJoiProps.ts +2 -2
  91. package/src/testArtifacts.ts +19 -20
  92. package/src/testMocks.ts +60 -35
  93. package/src/type/locutus.d.ts +3 -0
  94. package/src/utils.ts +39 -18
  95. package/tsconfig.json +5 -3
  96. package/typedoc.json +4 -0
package/dist/Nymph.d.ts CHANGED
@@ -1,13 +1,42 @@
1
- import { Config } from './conf';
2
- import { NymphDriver } from './driver';
3
- import Entity from './Entity';
4
- import { EntityConstructor, EntityInterface } from './Entity.types';
5
- import type { Selector, Options, NymphConnectCallback, NymphDisconnectCallback, NymphBeforeGetEntityCallback, NymphBeforeGetEntitiesCallback, NymphBeforeSaveEntityCallback, NymphAfterSaveEntityCallback, NymphBeforeDeleteEntityCallback, NymphAfterDeleteEntityCallback, NymphBeforeDeleteEntityByIDCallback, NymphAfterDeleteEntityByIDCallback, NymphBeforeNewUIDCallback, NymphAfterNewUIDCallback, NymphBeforeSetUIDCallback, NymphAfterSetUIDCallback, NymphBeforeRenameUIDCallback, NymphAfterRenameUIDCallback, NymphBeforeDeleteUIDCallback, NymphAfterDeleteUIDCallback, NymphEventType, NymphQueryCallback, FormattedSelector, TilmeldInterface } from './Nymph.types';
1
+ import { Config } from './conf/index.js';
2
+ import type { NymphDriver } from './driver/index.js';
3
+ import Entity, { type EntityInstanceType } from './Entity.js';
4
+ import type { EntityConstructor, EntityInterface } from './Entity.types.js';
5
+ import type { Selector, Options, NymphConnectCallback, NymphDisconnectCallback, NymphBeforeGetEntityCallback, NymphBeforeGetEntitiesCallback, NymphBeforeSaveEntityCallback, NymphAfterSaveEntityCallback, NymphFailedSaveEntityCallback, NymphBeforeDeleteEntityCallback, NymphAfterDeleteEntityCallback, NymphFailedDeleteEntityCallback, NymphBeforeDeleteEntityByIDCallback, NymphAfterDeleteEntityByIDCallback, NymphFailedDeleteEntityByIDCallback, NymphBeforeNewUIDCallback, NymphAfterNewUIDCallback, NymphFailedNewUIDCallback, NymphBeforeSetUIDCallback, NymphAfterSetUIDCallback, NymphFailedSetUIDCallback, NymphBeforeRenameUIDCallback, NymphAfterRenameUIDCallback, NymphFailedRenameUIDCallback, NymphBeforeDeleteUIDCallback, NymphAfterDeleteUIDCallback, NymphFailedDeleteUIDCallback, NymphBeforeStartTransactionCallback, NymphAfterStartTransactionCallback, NymphBeforeCommitTransactionCallback, NymphAfterCommitTransactionCallback, NymphBeforeRollbackTransactionCallback, NymphAfterRollbackTransactionCallback, NymphEventType, NymphQueryCallback, FormattedSelector, TilmeldInterface } from './Nymph.types.js';
6
+ /**
7
+ * An object relational mapper for Node.js.
8
+ *
9
+ * Written by Hunter Perrin for SciActive.
10
+ *
11
+ * @author Hunter Perrin <hperrin@gmail.com>
12
+ * @copyright SciActive Inc
13
+ * @see http://nymph.io/
14
+ */
6
15
  export default class Nymph {
16
+ /**
17
+ * The Nymph config.
18
+ */
7
19
  config: Config;
20
+ /**
21
+ * The Nymph instance that this one was cloned from, or null if it's not a
22
+ * clone.
23
+ */
24
+ parent: Nymph | null;
25
+ /**
26
+ * The Nymph database driver.
27
+ */
8
28
  driver: NymphDriver;
29
+ /**
30
+ * An optional Tilmeld user/group manager instance.
31
+ */
9
32
  tilmeld?: TilmeldInterface;
33
+ /**
34
+ * A simple map of names to Entity classes.
35
+ */
10
36
  private entityClasses;
37
+ /**
38
+ * The entity class for this instance of Nymph.
39
+ */
11
40
  Entity: typeof Entity;
12
41
  private connectCallbacks;
13
42
  private disconnectCallbacks;
@@ -16,58 +45,448 @@ export default class Nymph {
16
45
  private beforeGetEntitiesCallbacks;
17
46
  private beforeSaveEntityCallbacks;
18
47
  private afterSaveEntityCallbacks;
48
+ private failedSaveEntityCallbacks;
19
49
  private beforeDeleteEntityCallbacks;
20
50
  private afterDeleteEntityCallbacks;
51
+ private failedDeleteEntityCallbacks;
21
52
  private beforeDeleteEntityByIDCallbacks;
22
53
  private afterDeleteEntityByIDCallbacks;
54
+ private failedDeleteEntityByIDCallbacks;
23
55
  private beforeNewUIDCallbacks;
24
56
  private afterNewUIDCallbacks;
57
+ private failedNewUIDCallbacks;
25
58
  private beforeSetUIDCallbacks;
26
59
  private afterSetUIDCallbacks;
60
+ private failedSetUIDCallbacks;
27
61
  private beforeRenameUIDCallbacks;
28
62
  private afterRenameUIDCallbacks;
63
+ private failedRenameUIDCallbacks;
29
64
  private beforeDeleteUIDCallbacks;
30
65
  private afterDeleteUIDCallbacks;
31
- constructor(config: Partial<Config>, driver: NymphDriver, tilmeld?: TilmeldInterface);
32
- addEntityClass<T extends EntityConstructor>(entityClass: T): T;
66
+ private failedDeleteUIDCallbacks;
67
+ private beforeStartTransactionCallbacks;
68
+ private afterStartTransactionCallbacks;
69
+ private beforeCommitTransactionCallbacks;
70
+ private afterCommitTransactionCallbacks;
71
+ private beforeRollbackTransactionCallbacks;
72
+ private afterRollbackTransactionCallbacks;
73
+ /**
74
+ * Initialize Nymph.
75
+ *
76
+ * @param config The Nymph configuration.
77
+ * @param driver The Nymph database driver.
78
+ * @param tilmeld The Tilmeld user/group manager instance, if you want to use it.
79
+ * @param parent Used internally by Nymph. Don't set this.
80
+ */
81
+ constructor(config: Partial<Config>, driver: NymphDriver, tilmeld?: TilmeldInterface, parent?: Nymph);
82
+ /**
83
+ * Add your class to this instance.
84
+ *
85
+ * This will create a class that extends your class within this instance of
86
+ * Nymph and return it. You can then use this class's constructor and methods,
87
+ * which will use this instance of Nymph.
88
+ *
89
+ * Because this creates a subclass, don't use the class
90
+ * returned from `getEntityClass` to check with `instanceof`.
91
+ */
92
+ addEntityClass<T extends EntityConstructor>(EntityClass: T): T;
93
+ /**
94
+ * Get the class that uses the specified class name.
95
+ */
33
96
  getEntityClass(className: string): EntityConstructor;
97
+ getEntityClass<T extends EntityConstructor>(className: T): T;
98
+ /**
99
+ * Get the class that uses the specified etype.
100
+ *
101
+ * Note that it is fine, though unusual, for two classes to use the same
102
+ * etype. However, this can lead to very hard to diagnose bugs, so is
103
+ * generally discouraged.
104
+ */
105
+ getEntityClassByEtype(etype: string): EntityConstructor;
106
+ /**
107
+ * Get a clone of this instance with cloned classes and event listeners.
108
+ *
109
+ * @returns A clone of this instance.
110
+ */
34
111
  clone(): Nymph;
112
+ /**
113
+ * Connect to the database.
114
+ *
115
+ * @returns Whether the instance is connected to the database.
116
+ */
35
117
  connect(): Promise<boolean>;
118
+ /**
119
+ * Disconnect from the database.
120
+ *
121
+ * @returns Whether the instance is connected to the database.
122
+ */
36
123
  disconnect(): Promise<boolean>;
124
+ /**
125
+ * Run all the query callbacks on a query.
126
+ */
37
127
  runQueryCallbacks(options: Options, selectors: FormattedSelector[]): void;
128
+ /**
129
+ * Start an atomic transaction and returns a new instance of Nymph.
130
+ *
131
+ * All proceeding changes using this new instance will wait to be written to
132
+ * the database's permanent storage until commit() is called. You can also
133
+ * undo all the changes since this function ran with rollback().
134
+ *
135
+ * Transactions will nest as long as every name is unique. Internally, Nymph
136
+ * uses names prefixed with "nymph-".
137
+ *
138
+ * @returns A new instance of Nymph that should be used for the transaction.
139
+ */
38
140
  startTransaction(name: string): Promise<Nymph>;
141
+ /**
142
+ * Commit the named transaction.
143
+ *
144
+ * After this is called, the transaction instance should be discarded.
145
+ *
146
+ * @returns True on success, false on failure.
147
+ */
39
148
  commit(name: string): Promise<boolean>;
149
+ /**
150
+ * Rollback the named transaction.
151
+ *
152
+ * After this is called, the transaction instance should be discarded.
153
+ *
154
+ * @returns True on success, false on failure.
155
+ */
40
156
  rollback(name: string): Promise<boolean>;
157
+ /**
158
+ * Check if there is any open transaction.
159
+ *
160
+ * @returns True if there is a transaction.
161
+ */
41
162
  inTransaction(): Promise<boolean>;
163
+ /**
164
+ * Increment or create a unique ID and return the new value.
165
+ *
166
+ * Unique IDs, or UIDs are similar to GUIDs, but numeric and sequential.
167
+ *
168
+ * A UID can be used to identify an object when the GUID doesn't suffice. On
169
+ * a system where a new entity is created many times per second, referring
170
+ * to something by its GUID may be unintuitive. However, the component
171
+ * designer is responsible for assigning UIDs to the component's entities.
172
+ * Beware that if a UID is incremented for an entity, and the entity cannot
173
+ * be saved, there is no safe, and therefore, no recommended way to
174
+ * decrement the UID back to its previous value.
175
+ *
176
+ * If newUID() is passed the name of a UID which does not exist yet, one
177
+ * will be created with that name, and assigned the value 1. If the UID
178
+ * already exists, its value will be incremented. The new value will be
179
+ * returned.
180
+ *
181
+ * @param name The UID's name.
182
+ * @returns The UID's new value, or null on failure.
183
+ */
42
184
  newUID(name: string): Promise<number | null>;
185
+ /**
186
+ * Get the current value of a unique ID.
187
+ * @param name The UID's name.
188
+ * @returns The UID's value, or null on failure and if it doesn't exist.
189
+ */
43
190
  getUID(name: string): Promise<number | null>;
191
+ /**
192
+ * Set the value of a UID.
193
+ *
194
+ * @param name The UID's name.
195
+ * @param value The value.
196
+ * @returns True on success, false on failure.
197
+ */
44
198
  setUID(name: string, value: number): Promise<boolean>;
199
+ /**
200
+ * Delete a unique ID.
201
+ *
202
+ * @param name The UID's name.
203
+ * @returns True on success, false on failure.
204
+ */
45
205
  deleteUID(name: string): Promise<boolean>;
206
+ /**
207
+ * Rename a unique ID.
208
+ *
209
+ * @param oldName The old name.
210
+ * @param newName The new name.
211
+ * @returns True on success, false on failure.
212
+ */
46
213
  renameUID(oldName: string, newName: string): Promise<boolean>;
214
+ /**
215
+ * Save an entity to the database.
216
+ *
217
+ * If the entity has never been saved (has no GUID), a variable "cdate"
218
+ * is set on it with the current Unix timestamp.
219
+ *
220
+ * The variable "mdate" is set to the current Unix timestamp.
221
+ *
222
+ * @param entity The entity.
223
+ * @returns True on success, false on failure.
224
+ */
47
225
  saveEntity(entity: EntityInterface): Promise<boolean>;
226
+ /**
227
+ * Get an array of entities.
228
+ *
229
+ * `options` is an object, which contains any of the following settings:
230
+ *
231
+ * - class - The class to create each entity with.
232
+ * - limit - The limit of entities to be returned.
233
+ * - offset - The offset from the oldest matching entity to start retrieving.
234
+ * - reverse - If true, entities will be retrieved from newest to oldest.
235
+ * Therefore, offset will be from the newest entity.
236
+ * - sort - How to sort the entities. Accepts "cdate", "mdate", or the name of
237
+ * a top-level property. The method of sorting properties other than cdate
238
+ * and mdate is driver dependent. The only hard rule is that numbers should
239
+ * be sorted numerically (2 before 10). Defaults to "cdate".
240
+ * - return - What to return. "entity", "guid", or "count". Defaults to
241
+ * "entity".
242
+ * - source - Will be 'client' if the query came from a REST call.
243
+ * - skipCache - If true, Nymph will skip the cache and retrieve the entity
244
+ * from the DB.
245
+ * - skipAc - If true, Tilmeld will not filter returned entities according to
246
+ * access controls. (If Tilmeld is installed.) (This is *always* set to
247
+ * false by the REST server.)
248
+ *
249
+ * If a class is specified, it must have a factory() static method that
250
+ * returns a new instance.
251
+ *
252
+ * Selectors are objects. Any amount of selectors can be provided. Empty
253
+ * selectors will be ignored. The `type` property of a selector is required
254
+ * and can be one of the following strings:
255
+ *
256
+ * - & - (and) All values in the selector must be true.
257
+ * - | - (or) At least one value in the selector must be true.
258
+ * - !& - (not and) All values in the selector must be false.
259
+ * - !| - (not or) At least one value in the selector must be false.
260
+ *
261
+ * The rest of the properties in the selectors are called selector clauses,
262
+ * which can be any of the following (in the form selector.name = value, or
263
+ * selector.name = [value1, value2,...]):
264
+ *
265
+ * - guid - A GUID. True if the entity's GUID is equal.
266
+ * - tag - A tag. True if the entity has the tag.
267
+ * - defined - A name. True if the named property exists.
268
+ * - truthy - A name. True if the named property is defined and truthy.
269
+ * - equal - An array with a name, then value. True if the named property is
270
+ * defined and equal (their JSON strings are identical).
271
+ * - contain - An array with a name, then value. True if the named property
272
+ * contains the value (its JSON string is found within the property's JSON
273
+ * string).
274
+ * - match - An array with a name, then regular expression. True if the
275
+ * named property matches. Uses POSIX RegExp. Case sensitive. Must *not* be
276
+ * surrounded by any delimiters.
277
+ * - imatch - An array with a name, then regular expression. True if the
278
+ * named property matches. Uses POSIX RegExp. Case insensitive. Must *not*
279
+ * be surrounded by any delimiters.
280
+ * - like - An array with a name, then pattern. True if the named property
281
+ * matches. Uses % for variable length wildcard and _ for single character
282
+ * wildcard. Case sensitive.
283
+ * - ilike - An array with a name, then pattern. True if the named property
284
+ * matches. Uses % for variable length wildcard and _ for single character
285
+ * wildcard. Case insensitive.
286
+ * - gt - An array with a name, then value. True if the named property is
287
+ * greater than the value.
288
+ * - gte - An array with a name, then value. True if the named property is
289
+ * greater than or equal to the value.
290
+ * - lt - An array with a name, then value. True if the named property is
291
+ * less than the value.
292
+ * - lte - An array with a name, then value. True if the named property is
293
+ * less than or equal to the value.
294
+ * - ref - An array with a name, then either an entity, or a GUID. True if
295
+ * the named property is the entity or contains the entity.
296
+ * - qref - An array with a name, then a full query (including options) in an
297
+ * array. True if the named property is an entity that matches the query or
298
+ * contains an entity that matches the query.
299
+ * - selector - A selector. (Keep in mind, you can also use an array of these,
300
+ * just like any other clause.)
301
+ *
302
+ * These clauses can all be negated, by prefixing them with an exclamation
303
+ * point, such as "!truthy" to mean falsy (or undefined).
304
+ *
305
+ * Any clause that accepts an array of name and value can also accept a third
306
+ * element. If value is null and the third element is a string, the third
307
+ * element will be used with Locutus' strtotime function to set value to a
308
+ * relative timestamp. For example, the following selector will look for all
309
+ * entities that were created in the last day:
310
+ *
311
+ * ```
312
+ * {
313
+ * type: '&',
314
+ * gte: ['cdate', null, '-1 day']
315
+ * }
316
+ * ```
317
+ *
318
+ * Locutus' implementation: https://locutus.io/php/datetime/strtotime/
319
+ * PHP's documentation: https://www.php.net/manual/en/function.strtotime.php
320
+ *
321
+ * This example will retrieve the last two entities where:
322
+ *
323
+ * - It has 'person' tag.
324
+ * - spouse is defined.
325
+ * - gender is male and lname is Smith.
326
+ * - warnings is not an integer 0.
327
+ * - It has 'level1' and 'level2' tags, or it has 'access1' and 'access2'
328
+ * tags.
329
+ * - It has either 'employee' or 'manager' tag.
330
+ * - name is either Clark, James, Chris, Christopher, Jake, or Jacob.
331
+ * - If age is 22 or more, then pay is not greater than 8.
332
+ *
333
+ * ```
334
+ * const entities = Nymph.getEntities(
335
+ * { class: Entity, reverse: true, limit: 2 },
336
+ * {
337
+ * type: '&', // all must be true
338
+ * tag: 'person',
339
+ * defined: 'spouse',
340
+ * equal: [
341
+ * ['gender', 'male'],
342
+ * ['lname', 'Smith']
343
+ * ],
344
+ * '!equal': ['warnings', 0]
345
+ * },
346
+ * {
347
+ * type: '|', // at least one of the selectors in this must match
348
+ * selector: [
349
+ * {
350
+ * type: '&',
351
+ * tag: ['level1', 'level2']
352
+ * },
353
+ * {
354
+ * type: '&',
355
+ * tag: ['access1', 'access2']
356
+ * }
357
+ * ]
358
+ * },
359
+ * {
360
+ * type: '|', // at least one must be true
361
+ * tag: ['employee', 'manager']
362
+ * },
363
+ * {
364
+ * type: '|',
365
+ * equal: [
366
+ * ['name', 'Clark'],
367
+ * ['name', 'James']
368
+ * ],
369
+ * match: [
370
+ * ['name', 'Chris(topher)?'],
371
+ * ['name', 'Ja(ke|cob)']
372
+ * ]
373
+ * },
374
+ * {
375
+ * type: '!|', // at least one must be false
376
+ * gte: ['age', 22],
377
+ * gt: ['pay', 8]
378
+ * }
379
+ * );
380
+ * ```
381
+ *
382
+ * @param options The options.
383
+ * @param selectors Unlimited optional selectors to search for. If none are given, all entities are retrieved for the given options.
384
+ * @returns An array of entities or guids, or a count.
385
+ * @todo Use an asterisk to specify any variable.
386
+ */
48
387
  getEntities<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
49
388
  return: 'count';
50
389
  }, ...selectors: Selector[]): Promise<number>;
51
390
  getEntities<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
52
391
  return: 'guid';
53
392
  }, ...selectors: Selector[]): Promise<string[]>;
54
- getEntities<T extends EntityConstructor = EntityConstructor>(options?: Options<T>, ...selectors: Selector[]): Promise<ReturnType<T['factorySync']>[]>;
393
+ getEntities<T extends EntityConstructor = EntityConstructor>(options?: Options<T>, ...selectors: Selector[]): Promise<EntityInstanceType<T>[]>;
394
+ /**
395
+ * Get the first entity to match all options/selectors.
396
+ *
397
+ * options and selectors are the same as in getEntities().
398
+ *
399
+ * This function is equivalent to setting options.limit to 1 for
400
+ * getEntities(), except that it will return null if no entity is found.
401
+ * getEntities() would return an empty array.
402
+ *
403
+ * @param options The options.
404
+ * @param selectors Unlimited optional selectors to search for, or a single GUID. If none are given, all entities are searched for the given options.
405
+ * @returns An entity or guid, or null on failure or nothing found, or a number.
406
+ */
55
407
  getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
56
408
  return: 'count';
57
409
  }, ...selectors: Selector[]): Promise<number>;
58
410
  getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
59
411
  return: 'guid';
60
412
  }, ...selectors: Selector[]): Promise<string | null>;
61
- getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, ...selectors: Selector[]): Promise<ReturnType<T['factorySync']> | null>;
413
+ getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, ...selectors: Selector[]): Promise<EntityInstanceType<T> | null>;
62
414
  getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
63
415
  return: 'guid';
64
416
  }, guid: string): Promise<string | null>;
65
- getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, guid: string): Promise<ReturnType<T['factorySync']> | null>;
417
+ getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, guid: string): Promise<EntityInstanceType<T> | null>;
418
+ /**
419
+ * Delete an entity from the database.
420
+ *
421
+ * @param entity The entity.
422
+ * @returns True on success, false on failure.
423
+ */
66
424
  deleteEntity(entity: EntityInterface): Promise<boolean>;
425
+ /**
426
+ * Delete an entity from the database by its GUID.
427
+ *
428
+ * @param guid The entity's GUID.
429
+ * @param className The entity's class name.
430
+ * @returns True on success, false on failure.
431
+ */
67
432
  deleteEntityByID(guid: string, className?: string): Promise<boolean>;
433
+ /**
434
+ * Export entities to a local file.
435
+ *
436
+ * This is the file format:
437
+ *
438
+ * ```
439
+ * #nex2
440
+ * # The above line must be the first thing in the file.
441
+ * # Comments begin with #
442
+ * # And can have white space before them.
443
+ * # This defines a UID.
444
+ * <name/of/uid>[5]
445
+ * <another uid>[8000]
446
+ * # For UIDs, the name is in angle brackets (<>) and the value follows
447
+ * # in square brackets ([]).
448
+ * # This starts a new entity.
449
+ * {1234abcd}<etype>[tag,list,with,commas]
450
+ * # For entities, the GUID is in curly brackets ({}), then the etype in
451
+ * # angle brackets, then the comma separated tag list follows in square
452
+ * # brackets ([]).
453
+ * # Properties are stored like this:
454
+ * # propname=JSON.stringify(value)
455
+ * abilities=["system/admin"]
456
+ * groups=[]
457
+ * inheritAbilities=false
458
+ * name="admin"
459
+ * # White space before/after "=" and at beginning/end of line is ignored.
460
+ * username = "admin"
461
+ * {2}<etype>[tag,list]
462
+ * another="This is another entity."
463
+ * newline="\n"
464
+ * ```
465
+ *
466
+ * @param filename The file to export to.
467
+ * @returns True on success, false on failure.
468
+ */
68
469
  export(filename: string): Promise<boolean>;
470
+ /**
471
+ * Export entities to the console.
472
+ *
473
+ * @returns True on success, false on failure.
474
+ */
69
475
  exportPrint(): Promise<boolean>;
476
+ /**
477
+ * Import entities from a file.
478
+ *
479
+ * @param filename The file to import from.
480
+ * @returns True on success, false on failure.
481
+ */
70
482
  import(filename: string): Promise<boolean>;
71
- on<T extends NymphEventType>(event: T, callback: T extends 'connect' ? NymphConnectCallback : T extends 'disconnect' ? NymphDisconnectCallback : T extends 'query' ? NymphQueryCallback : T extends 'beforeGetEntity' ? NymphBeforeGetEntityCallback : T extends 'beforeGetEntities' ? NymphBeforeGetEntitiesCallback : T extends 'beforeSaveEntity' ? NymphBeforeSaveEntityCallback : T extends 'afterSaveEntity' ? NymphAfterSaveEntityCallback : T extends 'beforeDeleteEntity' ? NymphBeforeDeleteEntityCallback : T extends 'afterDeleteEntity' ? NymphAfterDeleteEntityCallback : T extends 'beforeDeleteEntityByID' ? NymphBeforeDeleteEntityByIDCallback : T extends 'afterDeleteEntityByID' ? NymphAfterDeleteEntityByIDCallback : T extends 'beforeNewUID' ? NymphBeforeNewUIDCallback : T extends 'afterNewUID' ? NymphAfterNewUIDCallback : T extends 'beforeSetUID' ? NymphBeforeSetUIDCallback : T extends 'afterSetUID' ? NymphAfterSetUIDCallback : T extends 'beforeRenameUID' ? NymphBeforeRenameUIDCallback : T extends 'afterRenameUID' ? NymphAfterRenameUIDCallback : T extends 'beforeDeleteUID' ? NymphBeforeDeleteUIDCallback : T extends 'afterDeleteUID' ? NymphAfterDeleteUIDCallback : never): () => boolean;
72
- off<T extends NymphEventType>(event: T, callback: T extends 'connect' ? NymphConnectCallback : T extends 'disconnect' ? NymphDisconnectCallback : T extends 'query' ? NymphQueryCallback : T extends 'beforeGetEntity' ? NymphBeforeGetEntityCallback : T extends 'beforeGetEntities' ? NymphBeforeGetEntitiesCallback : T extends 'beforeSaveEntity' ? NymphBeforeSaveEntityCallback : T extends 'afterSaveEntity' ? NymphAfterSaveEntityCallback : T extends 'beforeDeleteEntity' ? NymphBeforeDeleteEntityCallback : T extends 'afterDeleteEntity' ? NymphAfterDeleteEntityCallback : T extends 'beforeDeleteEntityByID' ? NymphBeforeDeleteEntityByIDCallback : T extends 'afterDeleteEntityByID' ? NymphAfterDeleteEntityByIDCallback : T extends 'beforeNewUID' ? NymphBeforeNewUIDCallback : T extends 'afterNewUID' ? NymphAfterNewUIDCallback : T extends 'beforeSetUID' ? NymphBeforeSetUIDCallback : T extends 'afterSetUID' ? NymphAfterSetUIDCallback : T extends 'beforeRenameUID' ? NymphBeforeRenameUIDCallback : T extends 'afterRenameUID' ? NymphAfterRenameUIDCallback : T extends 'beforeDeleteUID' ? NymphBeforeDeleteUIDCallback : T extends 'afterDeleteUID' ? NymphAfterDeleteUIDCallback : never): boolean;
483
+ /**
484
+ * Detect whether the database needs to be migrated.
485
+ *
486
+ * If true, the database should be exported with an old version of Nymph, then
487
+ * imported into a fresh database with this version.
488
+ */
489
+ needsMigration(): Promise<boolean>;
490
+ on<T extends NymphEventType>(event: T, callback: T extends 'connect' ? NymphConnectCallback : T extends 'disconnect' ? NymphDisconnectCallback : T extends 'query' ? NymphQueryCallback : T extends 'beforeGetEntity' ? NymphBeforeGetEntityCallback : T extends 'beforeGetEntities' ? NymphBeforeGetEntitiesCallback : T extends 'beforeSaveEntity' ? NymphBeforeSaveEntityCallback : T extends 'afterSaveEntity' ? NymphAfterSaveEntityCallback : T extends 'failedSaveEntity' ? NymphFailedSaveEntityCallback : T extends 'beforeDeleteEntity' ? NymphBeforeDeleteEntityCallback : T extends 'afterDeleteEntity' ? NymphAfterDeleteEntityCallback : T extends 'failedDeleteEntity' ? NymphFailedDeleteEntityCallback : T extends 'beforeDeleteEntityByID' ? NymphBeforeDeleteEntityByIDCallback : T extends 'afterDeleteEntityByID' ? NymphAfterDeleteEntityByIDCallback : T extends 'failedDeleteEntityByID' ? NymphFailedDeleteEntityByIDCallback : T extends 'beforeNewUID' ? NymphBeforeNewUIDCallback : T extends 'afterNewUID' ? NymphAfterNewUIDCallback : T extends 'failedNewUID' ? NymphFailedNewUIDCallback : T extends 'beforeSetUID' ? NymphBeforeSetUIDCallback : T extends 'afterSetUID' ? NymphAfterSetUIDCallback : T extends 'failedSetUID' ? NymphFailedSetUIDCallback : T extends 'beforeRenameUID' ? NymphBeforeRenameUIDCallback : T extends 'afterRenameUID' ? NymphAfterRenameUIDCallback : T extends 'failedRenameUID' ? NymphFailedRenameUIDCallback : T extends 'beforeDeleteUID' ? NymphBeforeDeleteUIDCallback : T extends 'afterDeleteUID' ? NymphAfterDeleteUIDCallback : T extends 'failedDeleteUID' ? NymphFailedDeleteUIDCallback : T extends 'beforeStartTransaction' ? NymphBeforeStartTransactionCallback : T extends 'afterStartTransaction' ? NymphAfterStartTransactionCallback : T extends 'beforeCommitTransaction' ? NymphBeforeCommitTransactionCallback : T extends 'afterCommitTransaction' ? NymphAfterCommitTransactionCallback : T extends 'beforeRollbackTransaction' ? NymphBeforeRollbackTransactionCallback : T extends 'afterRollbackTransaction' ? NymphAfterRollbackTransactionCallback : never): () => boolean;
491
+ off<T extends NymphEventType>(event: T, callback: T extends 'connect' ? NymphConnectCallback : T extends 'disconnect' ? NymphDisconnectCallback : T extends 'query' ? NymphQueryCallback : T extends 'beforeGetEntity' ? NymphBeforeGetEntityCallback : T extends 'beforeGetEntities' ? NymphBeforeGetEntitiesCallback : T extends 'beforeSaveEntity' ? NymphBeforeSaveEntityCallback : T extends 'afterSaveEntity' ? NymphAfterSaveEntityCallback : T extends 'failedSaveEntity' ? NymphFailedSaveEntityCallback : T extends 'beforeDeleteEntity' ? NymphBeforeDeleteEntityCallback : T extends 'afterDeleteEntity' ? NymphAfterDeleteEntityCallback : T extends 'failedDeleteEntity' ? NymphFailedDeleteEntityCallback : T extends 'beforeDeleteEntityByID' ? NymphBeforeDeleteEntityByIDCallback : T extends 'afterDeleteEntityByID' ? NymphAfterDeleteEntityByIDCallback : T extends 'failedDeleteEntityByID' ? NymphFailedDeleteEntityByIDCallback : T extends 'beforeNewUID' ? NymphBeforeNewUIDCallback : T extends 'afterNewUID' ? NymphAfterNewUIDCallback : T extends 'failedNewUID' ? NymphFailedNewUIDCallback : T extends 'beforeSetUID' ? NymphBeforeSetUIDCallback : T extends 'afterSetUID' ? NymphAfterSetUIDCallback : T extends 'failedSetUID' ? NymphFailedSetUIDCallback : T extends 'beforeRenameUID' ? NymphBeforeRenameUIDCallback : T extends 'afterRenameUID' ? NymphAfterRenameUIDCallback : T extends 'failedRenameUID' ? NymphFailedRenameUIDCallback : T extends 'beforeDeleteUID' ? NymphBeforeDeleteUIDCallback : T extends 'afterDeleteUID' ? NymphAfterDeleteUIDCallback : T extends 'failedDeleteUID' ? NymphFailedDeleteUIDCallback : T extends 'beforeStartTransaction' ? NymphBeforeStartTransactionCallback : T extends 'afterStartTransaction' ? NymphAfterStartTransactionCallback : T extends 'beforeCommitTransaction' ? NymphBeforeCommitTransactionCallback : T extends 'afterCommitTransaction' ? NymphAfterCommitTransactionCallback : T extends 'beforeRollbackTransaction' ? NymphBeforeRollbackTransactionCallback : T extends 'afterRollbackTransaction' ? NymphAfterRollbackTransactionCallback : never): boolean;
73
492
  }