@nymphjs/nymph 1.0.0-beta.1 → 1.0.0-beta.100

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 +535 -0
  2. package/README.md +14 -22
  3. package/dist/Entity.d.ts +293 -10
  4. package/dist/Entity.js +571 -149
  5. package/dist/Entity.js.map +1 -1
  6. package/dist/Entity.test.js +67 -26
  7. package/dist/Entity.test.js.map +1 -1
  8. package/dist/Entity.types.d.ts +256 -23
  9. package/dist/Entity.types.js +1 -2
  10. package/dist/Nymph.d.ts +497 -13
  11. package/dist/Nymph.js +761 -188
  12. package/dist/Nymph.js.map +1 -1
  13. package/dist/Nymph.types.d.ts +95 -26
  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 +181 -43
  25. package/dist/driver/NymphDriver.js +532 -285
  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 +939 -219
  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 +19 -6
  65. package/dist/testArtifacts.js +32 -37
  66. package/dist/testArtifacts.js.map +1 -1
  67. package/dist/testMocks.d.ts +28 -11
  68. package/dist/testMocks.js +44 -28
  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 +24 -20
  75. package/src/Entity.test.ts +63 -13
  76. package/src/Entity.ts +498 -156
  77. package/src/Entity.types.ts +72 -74
  78. package/src/Nymph.ts +904 -336
  79. package/src/Nymph.types.ts +179 -44
  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 +650 -460
  84. package/src/driver/index.ts +2 -2
  85. package/src/driver/testArtifacts.ts +966 -196
  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 +20 -20
  92. package/src/testMocks.ts +85 -37
  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, type EntityObjectType } from './Entity.js';
4
+ import type { EntityConstructor, EntityInterface, SerializedEntityData } 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,513 @@ 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
- addEntityClass(entityClass: EntityConstructor): void;
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
+ */
32
96
  getEntityClass(className: string): EntityConstructor;
33
- constructor(config: Partial<Config>, driver: NymphDriver, tilmeld?: TilmeldInterface);
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>;
37
- runQueryCallbacks(options: Options, selectors: FormattedSelector[]): Promise<void>;
124
+ /**
125
+ * Run all the query callbacks on a query.
126
+ */
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", "object", "guid", or "count". Defaults
241
+ * to "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
+ * - search - An array with a name, then search query. True if the named
275
+ * property matches the query. Use single quotes to find sequential terms.
276
+ * Use double quotes to find exact terms. Use "or" as the or operator. Use
277
+ * "-" before a term as the negation operator. Stop words and punctuation
278
+ * are stripped. Case insensitive.
279
+ * - match - An array with a name, then regular expression. True if the
280
+ * named property matches. Uses POSIX RegExp. Case sensitive. Must *not* be
281
+ * surrounded by any delimiters.
282
+ * - imatch - An array with a name, then regular expression. True if the
283
+ * named property matches. Uses POSIX RegExp. Case insensitive. Must *not*
284
+ * be surrounded by any delimiters.
285
+ * - like - An array with a name, then pattern. True if the named property
286
+ * matches. Uses % for variable length wildcard and _ for single character
287
+ * wildcard. Case sensitive.
288
+ * - ilike - An array with a name, then pattern. True if the named property
289
+ * matches. Uses % for variable length wildcard and _ for single character
290
+ * wildcard. Case insensitive.
291
+ * - gt - An array with a name, then value. True if the named property is
292
+ * greater than the value.
293
+ * - gte - An array with a name, then value. True if the named property is
294
+ * greater than or equal to the value.
295
+ * - lt - An array with a name, then value. True if the named property is
296
+ * less than the value.
297
+ * - lte - An array with a name, then value. True if the named property is
298
+ * less than or equal to the value.
299
+ * - ref - An array with a name, then either an entity, or a GUID. True if
300
+ * the named property is the entity or contains the entity.
301
+ * - qref - An array with a name, then a full query (including options) in an
302
+ * array. True if the named property is an entity that matches the query or
303
+ * contains an entity that matches the query.
304
+ * - selector - A selector. (Keep in mind, you can also use an array of these,
305
+ * just like any other clause.)
306
+ *
307
+ * These clauses can all be negated, by prefixing them with an exclamation
308
+ * point, such as "!truthy" to mean falsy (or undefined).
309
+ *
310
+ * Any clause that accepts an array of name and value can also accept a third
311
+ * element. If value is null and the third element is a string, the third
312
+ * element will be used with Locutus' strtotime function to set value to a
313
+ * relative timestamp. For example, the following selector will look for all
314
+ * entities that were created in the last day:
315
+ *
316
+ * ```
317
+ * {
318
+ * type: '&',
319
+ * gte: ['cdate', null, '-1 day']
320
+ * }
321
+ * ```
322
+ *
323
+ * Locutus' implementation: https://locutus.io/php/datetime/strtotime/
324
+ * PHP's documentation: https://www.php.net/manual/en/function.strtotime.php
325
+ *
326
+ * This example will retrieve the last two entities where:
327
+ *
328
+ * - It has 'person' tag.
329
+ * - spouse is defined.
330
+ * - gender is male and lname is Smith.
331
+ * - warnings is not an integer 0.
332
+ * - It has 'level1' and 'level2' tags, or it has 'access1' and 'access2'
333
+ * tags.
334
+ * - It has either 'employee' or 'manager' tag.
335
+ * - name is either Clark, James, Chris, Christopher, Jake, or Jacob.
336
+ * - If age is 22 or more, then pay is not greater than 8.
337
+ *
338
+ * ```
339
+ * const entities = Nymph.getEntities(
340
+ * { class: Entity, reverse: true, limit: 2 },
341
+ * {
342
+ * type: '&', // all must be true
343
+ * tag: 'person',
344
+ * defined: 'spouse',
345
+ * equal: [
346
+ * ['gender', 'male'],
347
+ * ['lname', 'Smith']
348
+ * ],
349
+ * '!equal': ['warnings', 0]
350
+ * },
351
+ * {
352
+ * type: '|', // at least one of the selectors in this must match
353
+ * selector: [
354
+ * {
355
+ * type: '&',
356
+ * tag: ['level1', 'level2']
357
+ * },
358
+ * {
359
+ * type: '&',
360
+ * tag: ['access1', 'access2']
361
+ * }
362
+ * ]
363
+ * },
364
+ * {
365
+ * type: '|', // at least one must be true
366
+ * tag: ['employee', 'manager']
367
+ * },
368
+ * {
369
+ * type: '|',
370
+ * equal: [
371
+ * ['name', 'Clark'],
372
+ * ['name', 'James']
373
+ * ],
374
+ * match: [
375
+ * ['name', 'Chris(topher)?'],
376
+ * ['name', 'Ja(ke|cob)']
377
+ * ]
378
+ * },
379
+ * {
380
+ * type: '!|', // at least one must be false
381
+ * gte: ['age', 22],
382
+ * gt: ['pay', 8]
383
+ * }
384
+ * );
385
+ * ```
386
+ *
387
+ * @param options The options.
388
+ * @param selectors Unlimited optional selectors to search for. If none are given, all entities are retrieved for the given options.
389
+ * @returns An array of entities or guids, or a count.
390
+ * @todo Use an asterisk to specify any variable.
391
+ */
48
392
  getEntities<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
49
393
  return: 'count';
50
394
  }, ...selectors: Selector[]): Promise<number>;
51
395
  getEntities<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
52
396
  return: 'guid';
53
397
  }, ...selectors: Selector[]): Promise<string[]>;
54
- getEntities<T extends EntityConstructor = EntityConstructor>(options?: Options<T>, ...selectors: Selector[]): Promise<ReturnType<T['factorySync']>[]>;
398
+ getEntities<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
399
+ return: 'object';
400
+ }, ...selectors: Selector[]): Promise<EntityObjectType<T>[]>;
401
+ getEntities<T extends EntityConstructor = EntityConstructor>(options?: Options<T>, ...selectors: Selector[]): Promise<EntityInstanceType<T>[]>;
402
+ /**
403
+ * Get the first entity to match all options/selectors.
404
+ *
405
+ * options and selectors are the same as in getEntities().
406
+ *
407
+ * This function is equivalent to setting options.limit to 1 for
408
+ * getEntities(), except that it will return null if no entity is found.
409
+ * getEntities() would return an empty array.
410
+ *
411
+ * @param options The options.
412
+ * @param selectors Unlimited optional selectors to search for, or a single GUID. If none are given, all entities are searched for the given options.
413
+ * @returns An entity or guid, or null on failure or nothing found, or a number.
414
+ */
55
415
  getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
56
416
  return: 'count';
57
417
  }, ...selectors: Selector[]): Promise<number>;
58
418
  getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
59
419
  return: 'guid';
60
420
  }, ...selectors: Selector[]): Promise<string | null>;
61
- getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, ...selectors: Selector[]): Promise<ReturnType<T['factorySync']> | null>;
421
+ getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
422
+ return: 'object';
423
+ }, ...selectors: Selector[]): Promise<EntityObjectType<T> | null>;
424
+ getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, ...selectors: Selector[]): Promise<EntityInstanceType<T> | null>;
62
425
  getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
63
426
  return: 'guid';
64
427
  }, guid: string): Promise<string | null>;
65
- getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, guid: string): Promise<ReturnType<T['factorySync']> | null>;
428
+ getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
429
+ return: 'object';
430
+ }, guid: string): Promise<EntityObjectType<T> | null>;
431
+ getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, guid: string): Promise<EntityInstanceType<T> | null>;
432
+ /**
433
+ * Delete an entity from the database.
434
+ *
435
+ * @param entity The entity.
436
+ * @returns True on success, false on failure.
437
+ */
66
438
  deleteEntity(entity: EntityInterface): Promise<boolean>;
439
+ /**
440
+ * Delete an entity from the database by its GUID.
441
+ *
442
+ * @param guid The entity's GUID.
443
+ * @param className The entity's class name.
444
+ * @returns True on success, false on failure.
445
+ */
67
446
  deleteEntityByID(guid: string, className?: string): Promise<boolean>;
447
+ /**
448
+ * Export entities to a local file.
449
+ *
450
+ * This is the file format:
451
+ *
452
+ * ```
453
+ * #nex2
454
+ * # The above line must be the first thing in the file.
455
+ * # Comments begin with #
456
+ * # And can have white space before them.
457
+ * # This defines a UID.
458
+ * <name/of/uid>[5]
459
+ * <another uid>[8000]
460
+ * # For UIDs, the name is in angle brackets (<>) and the value follows
461
+ * # in square brackets ([]).
462
+ * # This starts a new entity.
463
+ * {1234abcd}<etype>[tag,list,with,commas]
464
+ * # For entities, the GUID is in curly brackets ({}), then the etype in
465
+ * # angle brackets, then the comma separated tag list follows in square
466
+ * # brackets ([]).
467
+ * # Properties are stored like this:
468
+ * # propname=JSON.stringify(value)
469
+ * abilities=["system/admin"]
470
+ * groups=[]
471
+ * inheritAbilities=false
472
+ * name="admin"
473
+ * # White space before/after "=" and at beginning/end of line is ignored.
474
+ * username = "admin"
475
+ * {2}<etype>[tag,list]
476
+ * another="This is another entity."
477
+ * newline="\n"
478
+ * ```
479
+ *
480
+ * @param filename The file to export to.
481
+ * @returns True on success, false on failure.
482
+ */
68
483
  export(filename: string): Promise<boolean>;
484
+ /**
485
+ * Export entities to the console.
486
+ *
487
+ * @returns True on success, false on failure.
488
+ */
69
489
  exportPrint(): Promise<boolean>;
490
+ /**
491
+ * Import entities from a file.
492
+ *
493
+ * @param filename The file to import from.
494
+ * @returns True on success, false on failure.
495
+ */
70
496
  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;
497
+ /**
498
+ * Import a single entity.
499
+ *
500
+ * @param entity The entity data to import.
501
+ */
502
+ importEntity(entity: {
503
+ guid: string;
504
+ cdate: number;
505
+ mdate: number;
506
+ tags: string[];
507
+ sdata: SerializedEntityData;
508
+ etype: string;
509
+ }): Promise<void>;
510
+ /**
511
+ * Import a single entity's tokens.
512
+ *
513
+ * This does not update any of the actual data, only the tokens used for full
514
+ * text search.
515
+ *
516
+ * Be careful with this function, since you can overwrite existing tokens with
517
+ * data that is out of sync. The DB _should_ prevent you from inserting tokens
518
+ * for an entity that doesn't exist, but it _won't_ prevent you from inserting
519
+ * tokens for properties that don't exist on an entity.
520
+ *
521
+ * @param entity The entity data to import.
522
+ */
523
+ importEntityTokens(entity: {
524
+ guid: string;
525
+ cdate: number;
526
+ mdate: number;
527
+ tags: string[];
528
+ sdata: SerializedEntityData;
529
+ etype: string;
530
+ }): Promise<void>;
531
+ /**
532
+ * Detect whether the database needs to be migrated.
533
+ *
534
+ * If not false (so, one of the string values), the database should be
535
+ * exported with an old version of Nymph, then imported into a fresh database
536
+ * with this version.
537
+ *
538
+ * 'json' means the data tables are missing the JSON field, and the DB
539
+ * **must** be exported and imported to fix it.
540
+ *
541
+ * 'tokens' means the FTS token tables are missing, and you can get away with
542
+ * a live migration with `liveMigration('tokenTables')` and doing
543
+ * `importEntityTokens(entity)` for each entity.
544
+ */
545
+ needsMigration(): Promise<false | "json" | "tokens">;
546
+ /**
547
+ * Perform a live migration on the DB.
548
+ *
549
+ * A 'tokenTables' migration will simply add the missing token tables. It will
550
+ * **not** fill them. You must use `importEntityTokens` on each entity after
551
+ * running this to fill the token tables and enable full text search matching
552
+ * on existing entities.
553
+ */
554
+ liveMigration(migrationType: 'tokenTables'): Promise<void>;
555
+ 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;
556
+ 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
557
  }