@nymphjs/nymph 1.0.0-beta.13 → 1.0.0-beta.130

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 (123) hide show
  1. package/CHANGELOG.md +644 -0
  2. package/README.md +34 -24
  3. package/dist/Entity.d.ts +304 -15
  4. package/dist/Entity.js +609 -154
  5. package/dist/Entity.js.map +1 -1
  6. package/dist/Entity.test.js +75 -29
  7. package/dist/Entity.test.js.map +1 -1
  8. package/dist/Entity.types.d.ts +275 -28
  9. package/dist/Entity.types.js +1 -2
  10. package/dist/Nymph.d.ts +575 -11
  11. package/dist/Nymph.js +825 -288
  12. package/dist/Nymph.js.map +1 -1
  13. package/dist/Nymph.types.d.ts +117 -7
  14. package/dist/Nymph.types.js +47 -5
  15. package/dist/Nymph.types.js.map +1 -1
  16. package/dist/conf/defaults.d.ts +1 -1
  17. package/dist/conf/defaults.js +26 -3
  18. package/dist/conf/defaults.js.map +1 -1
  19. package/dist/conf/index.d.ts +50 -2
  20. package/dist/conf/index.js +2 -8
  21. package/dist/conf/index.js.map +1 -1
  22. package/dist/decorators/index.d.ts +1 -0
  23. package/dist/decorators/index.js +2 -0
  24. package/dist/decorators/index.js.map +1 -0
  25. package/dist/decorators/transactional.d.ts +12 -0
  26. package/dist/decorators/transactional.js +45 -0
  27. package/dist/decorators/transactional.js.map +1 -0
  28. package/dist/driver/NymphDriver.d.ts +209 -41
  29. package/dist/driver/NymphDriver.js +737 -242
  30. package/dist/driver/NymphDriver.js.map +1 -1
  31. package/dist/driver/index.d.ts +1 -2
  32. package/dist/driver/index.js +2 -23
  33. package/dist/driver/index.js.map +1 -1
  34. package/dist/driver/testArtifacts.d.ts +4 -4
  35. package/dist/driver/testArtifacts.js +2016 -1086
  36. package/dist/driver/testArtifacts.js.map +1 -1
  37. package/dist/errors/ClassNotAvailableError.js +1 -5
  38. package/dist/errors/ClassNotAvailableError.js.map +1 -1
  39. package/dist/errors/EntityConflictError.js +1 -5
  40. package/dist/errors/EntityConflictError.js.map +1 -1
  41. package/dist/errors/EntityInvalidDataError.d.ts +6 -0
  42. package/dist/errors/EntityInvalidDataError.js +8 -6
  43. package/dist/errors/EntityInvalidDataError.js.map +1 -1
  44. package/dist/errors/EntityIsSleepingReferenceError.js +1 -5
  45. package/dist/errors/EntityIsSleepingReferenceError.js.map +1 -1
  46. package/dist/errors/EntityUniqueConstraintError.d.ts +3 -0
  47. package/dist/errors/EntityUniqueConstraintError.js +7 -0
  48. package/dist/errors/EntityUniqueConstraintError.js.map +1 -0
  49. package/dist/errors/HttpError.d.ts +5 -0
  50. package/dist/errors/HttpError.js +11 -0
  51. package/dist/errors/HttpError.js.map +1 -0
  52. package/dist/errors/InvalidParametersError.js +1 -5
  53. package/dist/errors/InvalidParametersError.js.map +1 -1
  54. package/dist/errors/InvalidStateError.d.ts +3 -0
  55. package/dist/errors/InvalidStateError.js +7 -0
  56. package/dist/errors/InvalidStateError.js.map +1 -0
  57. package/dist/errors/MethodFailedError.d.ts +3 -0
  58. package/dist/errors/MethodFailedError.js +7 -0
  59. package/dist/errors/MethodFailedError.js.map +1 -0
  60. package/dist/errors/NotConfiguredError.js +1 -5
  61. package/dist/errors/NotConfiguredError.js.map +1 -1
  62. package/dist/errors/QueryFailedError.d.ts +2 -1
  63. package/dist/errors/QueryFailedError.js +5 -6
  64. package/dist/errors/QueryFailedError.js.map +1 -1
  65. package/dist/errors/UnableToConnectError.js +1 -5
  66. package/dist/errors/UnableToConnectError.js.map +1 -1
  67. package/dist/errors/index.d.ts +12 -8
  68. package/dist/errors/index.js +12 -24
  69. package/dist/errors/index.js.map +1 -1
  70. package/dist/helpers/index.d.ts +1 -0
  71. package/dist/helpers/index.js +2 -0
  72. package/dist/helpers/index.js.map +1 -0
  73. package/dist/helpers/transaction.d.ts +15 -0
  74. package/dist/helpers/transaction.js +47 -0
  75. package/dist/helpers/transaction.js.map +1 -0
  76. package/dist/index.d.ts +13 -11
  77. package/dist/index.js +16 -34
  78. package/dist/index.js.map +1 -1
  79. package/dist/nymphJoiProps.d.ts +1 -1
  80. package/dist/nymphJoiProps.js +10 -15
  81. package/dist/nymphJoiProps.js.map +1 -1
  82. package/dist/testArtifacts.d.ts +21 -6
  83. package/dist/testArtifacts.js +124 -50
  84. package/dist/testArtifacts.js.map +1 -1
  85. package/dist/testMocks.d.ts +27 -12
  86. package/dist/testMocks.js +44 -33
  87. package/dist/testMocks.js.map +1 -1
  88. package/dist/utils.d.ts +5 -5
  89. package/dist/utils.js +106 -68
  90. package/dist/utils.js.map +1 -1
  91. package/jest.config.js +31 -5
  92. package/package.json +43 -24
  93. package/src/Entity.test.ts +73 -23
  94. package/src/Entity.ts +555 -169
  95. package/src/Entity.types.ts +102 -90
  96. package/src/Nymph.ts +930 -517
  97. package/src/Nymph.types.ts +145 -39
  98. package/src/conf/defaults.ts +34 -1
  99. package/src/conf/index.ts +50 -2
  100. package/src/decorators/index.ts +1 -0
  101. package/src/decorators/transactional.ts +63 -0
  102. package/src/driver/NymphDriver.ts +987 -417
  103. package/src/driver/index.ts +1 -3
  104. package/src/driver/testArtifacts.ts +2699 -1628
  105. package/src/errors/EntityUniqueConstraintError.ts +6 -0
  106. package/src/errors/HttpError.ts +12 -0
  107. package/src/errors/InvalidStateError.ts +6 -0
  108. package/src/errors/MethodFailedError.ts +6 -0
  109. package/src/errors/QueryFailedError.ts +3 -1
  110. package/src/errors/index.ts +16 -8
  111. package/src/helpers/index.ts +1 -0
  112. package/src/helpers/transaction.ts +65 -0
  113. package/src/index.ts +13 -11
  114. package/src/nymphJoiProps.ts +2 -2
  115. package/src/testArtifacts.ts +69 -20
  116. package/src/testMocks.ts +107 -41
  117. package/src/utils.ts +102 -67
  118. package/tsconfig.json +6 -3
  119. package/typedoc.json +4 -0
  120. package/dist/conf/d.d.ts +0 -6
  121. package/dist/conf/d.js +0 -3
  122. package/dist/conf/d.js.map +0 -1
  123. package/src/conf/d.ts +0 -24
@@ -1,18 +1,19 @@
1
- import type Nymph from './Nymph';
2
- import type { Options } from './Nymph.types';
1
+ import type Nymph from './Nymph.js';
2
+ import type Entity from './Entity.js';
3
3
 
4
4
  export type ACProperties = {
5
- user: any;
6
- group: any;
7
- acUser: any;
8
- acGroup: any;
9
- acOther: any;
10
- acRead: any;
11
- acWrite: any;
12
- acFull: any;
5
+ user: string | null;
6
+ group: string | null;
7
+ acUser: number | null;
8
+ acGroup: number | null;
9
+ acOther: number | null;
10
+ acRead: (string | null)[] | null;
11
+ acWrite: (string | null)[] | null;
12
+ acFull: (string | null)[] | null;
13
13
  };
14
14
 
15
15
  export type EntityReference = ['nymph_entity_reference', string, string];
16
+ export type ClassReference = ['nymph_class_reference', string];
16
17
 
17
18
  export type EntityData = {
18
19
  [k: string]: any;
@@ -22,8 +23,16 @@ export type SerializedEntityData = {
22
23
  [k: string]: string;
23
24
  };
24
25
 
26
+ export type EntityObject = {
27
+ guid: string | null;
28
+ cdate: number | null;
29
+ mdate: number | null;
30
+ tags: string[];
31
+ [k: string]: any;
32
+ };
33
+
25
34
  export type EntityJson = {
26
- class: string;
35
+ class: EntityConstructor;
27
36
  guid: string | null;
28
37
  cdate: number | null;
29
38
  mdate: number | null;
@@ -32,7 +41,7 @@ export type EntityJson = {
32
41
  };
33
42
 
34
43
  export type EntityPatch = {
35
- class: string;
44
+ class: EntityConstructor;
36
45
  guid: string;
37
46
  mdate: number | null;
38
47
  set: EntityData;
@@ -61,9 +70,9 @@ export interface DataObjectInterface {
61
70
  /**
62
71
  * Delete the object from storage.
63
72
  *
64
- * @returns True on success, false on failure.
73
+ * An error is thrown on failure.
65
74
  */
66
- $delete(): Promise<boolean>;
75
+ $delete(): Promise<void>;
67
76
  /**
68
77
  * Perform a more strict comparison of this object to another.
69
78
  *
@@ -92,18 +101,16 @@ export interface DataObjectInterface {
92
101
  /**
93
102
  * Refresh the object from storage. (Bypasses Nymph's cache.)
94
103
  *
95
- * If the object has been deleted from storage, the database cannot be
96
- * reached, or a database error occurs, `refresh()` will return 0.
97
- *
98
- * @returns False if the data has not been saved, 0 if it can't be refreshed, true on success.
104
+ * @returns False if the data has not been saved, true on success. An error is
105
+ * thrown on failure.
99
106
  */
100
- $refresh(): Promise<boolean | 0>;
107
+ $refresh(): Promise<boolean>;
101
108
  /**
102
109
  * Save the object to storage.
103
110
  *
104
- * @returns True on success, false on failure.
111
+ * An error is thrown on failure.
105
112
  */
106
- $save(): Promise<boolean>;
113
+ $save(): Promise<void>;
107
114
  /**
108
115
  * The object's data.
109
116
  */
@@ -137,6 +144,17 @@ export interface EntityInterface extends DataObjectInterface {
137
144
  * Array of the entity's tags.
138
145
  */
139
146
  tags: string[];
147
+ /**
148
+ * Get a GUID for the entity.
149
+ *
150
+ * If the entity has already been saved, this will just return the GUID.
151
+ *
152
+ * If the entity has not yet been saved, this will return a new GUID that gets
153
+ * held by the entity. The `guid` property will remain null, but this method
154
+ * will then always return the same GUID. When the entity is eventually saved
155
+ * into the database, this GUID will be used.
156
+ */
157
+ $getGuaranteedGUID(): string;
140
158
  /**
141
159
  * Add one or more tags.
142
160
  *
@@ -153,7 +171,8 @@ export interface EntityInterface extends DataObjectInterface {
153
171
  /**
154
172
  * Get the client enabled methods.
155
173
  *
156
- * @returns The names of methods allowed to be called by the frontend with serverCall.
174
+ * @returns The names of methods allowed to be called by the frontend with
175
+ * serverCall.
157
176
  */
158
177
  $getClientEnabledMethods(): string[];
159
178
  /**
@@ -162,9 +181,11 @@ export interface EntityInterface extends DataObjectInterface {
162
181
  * This should only be used by Nymph to save the data into storage.
163
182
  *
164
183
  * @param includeSData Whether to include the serialized data as well.
184
+ * @param referenceOnlyExisting Whether to only turn existing entities into
185
+ * references.
165
186
  * @returns The entity's data object.
166
187
  */
167
- $getData(includeSData?: boolean): EntityData;
188
+ $getData(includeSData?: boolean, referenceOnlyExisting?: boolean): EntityData;
168
189
  /**
169
190
  * Used to retrieve the serialized data object.
170
191
  *
@@ -178,12 +199,30 @@ export interface EntityInterface extends DataObjectInterface {
178
199
  * @returns The entity's serialized data object.
179
200
  */
180
201
  $getSData(): SerializedEntityData;
202
+ /**
203
+ * Get an array of strings that **must** be unique across the current etype.
204
+ *
205
+ * When you try to save another entity with any of the same unique strings,
206
+ * Nymph will throw an error.
207
+ *
208
+ * The default implementation of this method returns an empty array, meaning
209
+ * there are no uniqueness constraints applied to its etype.
210
+ *
211
+ * @returns Resolves to an array of entity's unique constraint strings.
212
+ */
213
+ $getUniques(): Promise<string[]>;
181
214
  /**
182
215
  * Get the original values of the AC properties.
183
216
  *
184
217
  * @returns An object of AC properties.
185
218
  */
186
219
  $getOriginalAcValues(): ACProperties;
220
+ /**
221
+ * Get the current values of the AC properties.
222
+ *
223
+ * @returns An object of AC properties.
224
+ */
225
+ $getCurrentAcValues(): ACProperties;
187
226
  /**
188
227
  * Get an object that holds the same data as the entity.
189
228
  *
@@ -211,6 +250,9 @@ export interface EntityInterface extends DataObjectInterface {
211
250
  /**
212
251
  * Accept JSON data from the client.
213
252
  *
253
+ * The given data must already be dereferenced (class references turned into
254
+ * classes and entity references turned into entities).
255
+ *
214
256
  * This function uses the security protection lists:
215
257
  *
216
258
  * - $protectedTags
@@ -218,13 +260,18 @@ export interface EntityInterface extends DataObjectInterface {
218
260
  * - $allowlistTags
219
261
  * - $allowlistData
220
262
  *
221
- * @param input The input data. Please note, this will be modified (destroyed).
222
- * @param allowConflict Allow to accept data that is older than the current data.
263
+ * @param input The input data. Please note, this will be modified
264
+ * (destroyed).
265
+ * @param allowConflict Allow to accept data that is older than the current
266
+ * data.
223
267
  */
224
268
  $jsonAcceptData(input: EntityJson, allowConflict?: boolean): void;
225
269
  /**
226
270
  * Accept JSON patch from the client.
227
271
  *
272
+ * The given data must already be dereferenced (class references turned into
273
+ * classes and entity references turned into entities).
274
+ *
228
275
  * This function uses the security protection lists:
229
276
  *
230
277
  * - $protectedTags
@@ -232,22 +279,37 @@ export interface EntityInterface extends DataObjectInterface {
232
279
  * - $allowlistTags
233
280
  * - $allowlistData
234
281
  *
235
- * @param patch The patch data. Please note, this will be modified (destroyed).
236
- * @param allowConflict Allow to accept data that is older than the current data.
282
+ * @param patch The patch data. Please note, this will be modified
283
+ * (destroyed).
284
+ * @param allowConflict Allow to accept data that is older than the current
285
+ * data.
237
286
  */
238
287
  $jsonAcceptPatch(patch: EntityPatch, allowConflict?: boolean): void;
239
288
  /**
240
289
  * Used to set the data.
241
290
  *
242
- * This should only be used by Nymph to push the data from storage.
291
+ * This should only be used by Nymph to push the data from storage or the
292
+ * client.
293
+ *
294
+ * The given data must already be dereferenced (class references turned into
295
+ * classes and entity references turned into entities).
243
296
  *
244
297
  * `sdata` is used by Nymph to avoid unserializing data that hasn't been
245
298
  * requested yet.
246
299
  *
300
+ * If `source` is set to "server", the data is coming from the DB or the
301
+ * cache. If not, assume the data is coming from the client and can't be
302
+ * trusted.
303
+ *
247
304
  * @param data The data object.
248
305
  * @param sdata The serialized data object.
306
+ * @param source If this is set to "server", the data is coming from the DB.
249
307
  */
250
- $putData(data: EntityData, sdata?: SerializedEntityData): void;
308
+ $putData(
309
+ data: EntityData,
310
+ sdata?: SerializedEntityData,
311
+ source?: 'server',
312
+ ): void;
251
313
  /**
252
314
  * Remove one or more tags.
253
315
  *
@@ -257,12 +319,14 @@ export interface EntityInterface extends DataObjectInterface {
257
319
  /**
258
320
  * Return a Nymph Entity Reference for this entity.
259
321
  *
260
- * If the entity hasn't been saved yet (and has no GUID), it will be
261
- * returned instead.
322
+ * If the entity hasn't been saved yet (and has no GUID), it will use the
323
+ * guaranteed GUID from `$getGuaranteedGUID`, unless `existingOnly` is true,
324
+ * then it will return the entity.
262
325
  *
263
- * @returns A Nymph Entity Reference array as an unsaved entity.
326
+ * @param existingOnly Whether to only turn existing entities into references.
327
+ * @returns A Nymph Entity Reference array.
264
328
  */
265
- $toReference(): EntityReference | EntityInterface;
329
+ $toReference(existingOnly?: boolean): EntityReference | EntityInterface;
266
330
  /**
267
331
  * Set whether to use "skipAc" when accessing entity references.
268
332
  *
@@ -271,61 +335,9 @@ export interface EntityInterface extends DataObjectInterface {
271
335
  $useSkipAc(useSkipAc: boolean): void;
272
336
  }
273
337
 
274
- export type EntityConstructor = (new (...args: any[]) => EntityInterface) & {
275
- /**
276
- * The instance of Nymph to use for queries.
277
- */
278
- nymph: Nymph;
279
- /**
280
- * A unique name for this type of entity used to separate its data from other
281
- * types of entities in the database.
282
- */
283
- ETYPE: string;
284
- /**
285
- * The lookup name for this entity.
286
- *
287
- * This is used for reference arrays (and sleeping references) and client
288
- * requests.
289
- */
290
- class: string;
291
- /**
292
- * Properties that will not be searchable from the frontend. If the frontend
293
- * includes any of these properties in any of their clauses, they will be
294
- * filtered out before the search is executed.
295
- */
296
- searchRestrictedData: string[];
297
- /**
298
- * The names of static methods allowed to be called by the frontend with
299
- * serverCallStatic.
300
- */
301
- clientEnabledStaticMethods: string[];
302
- /**
303
- * Create a new entity instance.
304
- *
305
- * @param guid An optional GUID to retrieve.
306
- */
307
- factory(guid?: string): Promise<EntityInterface>;
308
- /**
309
- * Create a new entity instance.
310
- *
311
- * @param guid An optional GUID to retrieve.
312
- */
313
- factorySync(guid?: string): EntityInterface;
314
- /**
315
- * Create a new sleeping reference instance.
316
- *
317
- * Sleeping references won't retrieve their data from the database until it
318
- * is actually used.
319
- *
320
- * @param reference The Nymph Entity Reference to use to wake.
321
- * @returns The new instance.
322
- */
323
- factoryReference(reference: EntityReference): EntityInterface;
324
- /**
325
- * Alter the options for a query for this entity.
326
- *
327
- * @param options The current options.
328
- * @returns The altered options.
329
- */
330
- alterOptions?<T extends Options>(options: T): T;
338
+ export type EntityConstructor<
339
+ D extends EntityData = EntityData,
340
+ E extends Entity<D> = Entity<D>,
341
+ > = (new (...args: any[]) => E) & {
342
+ [k in keyof typeof Entity]: (typeof Entity)[k];
331
343
  };