@nymphjs/nymph 1.0.0-beta.8 → 1.0.0-beta.81

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 (65) hide show
  1. package/CHANGELOG.md +382 -0
  2. package/README.md +9 -19
  3. package/dist/Entity.d.ts +269 -7
  4. package/dist/Entity.js +491 -105
  5. package/dist/Entity.js.map +1 -1
  6. package/dist/Entity.test.js +25 -3
  7. package/dist/Entity.test.js.map +1 -1
  8. package/dist/Entity.types.d.ts +225 -19
  9. package/dist/Nymph.d.ts +430 -11
  10. package/dist/Nymph.js +686 -168
  11. package/dist/Nymph.js.map +1 -1
  12. package/dist/Nymph.types.d.ts +65 -7
  13. package/dist/Nymph.types.js +2 -1
  14. package/dist/Nymph.types.js.map +1 -1
  15. package/dist/conf/d.d.ts +30 -0
  16. package/dist/conf/defaults.js +27 -0
  17. package/dist/conf/defaults.js.map +1 -1
  18. package/dist/driver/NymphDriver.d.ts +136 -38
  19. package/dist/driver/NymphDriver.js +264 -131
  20. package/dist/driver/NymphDriver.js.map +1 -1
  21. package/dist/driver/testArtifacts.d.ts +1 -1
  22. package/dist/driver/testArtifacts.js +499 -51
  23. package/dist/driver/testArtifacts.js.map +1 -1
  24. package/dist/errors/EntityInvalidDataError.d.ts +6 -0
  25. package/dist/errors/EntityInvalidDataError.js +7 -1
  26. package/dist/errors/EntityInvalidDataError.js.map +1 -1
  27. package/dist/errors/EntityUniqueConstraintError.d.ts +3 -0
  28. package/dist/errors/EntityUniqueConstraintError.js +11 -0
  29. package/dist/errors/EntityUniqueConstraintError.js.map +1 -0
  30. package/dist/errors/InvalidStateError.d.ts +3 -0
  31. package/dist/errors/InvalidStateError.js +11 -0
  32. package/dist/errors/InvalidStateError.js.map +1 -0
  33. package/dist/errors/QueryFailedError.js +1 -0
  34. package/dist/errors/QueryFailedError.js.map +1 -1
  35. package/dist/errors/index.d.ts +2 -0
  36. package/dist/errors/index.js +2 -0
  37. package/dist/errors/index.js.map +1 -1
  38. package/dist/testArtifacts.d.ts +17 -5
  39. package/dist/testArtifacts.js +31 -27
  40. package/dist/testArtifacts.js.map +1 -1
  41. package/dist/testMocks.d.ts +13 -10
  42. package/dist/testMocks.js +32 -13
  43. package/dist/testMocks.js.map +1 -1
  44. package/dist/utils.d.ts +1 -1
  45. package/dist/utils.js +21 -5
  46. package/dist/utils.js.map +1 -1
  47. package/package.json +16 -14
  48. package/src/Entity.test.ts +22 -5
  49. package/src/Entity.ts +423 -134
  50. package/src/Entity.types.ts +32 -66
  51. package/src/Nymph.ts +609 -176
  52. package/src/Nymph.types.ts +131 -26
  53. package/src/conf/d.ts +12 -0
  54. package/src/conf/defaults.ts +31 -0
  55. package/src/driver/NymphDriver.ts +289 -311
  56. package/src/driver/testArtifacts.ts +572 -181
  57. package/src/errors/EntityUniqueConstraintError.ts +6 -0
  58. package/src/errors/InvalidStateError.ts +6 -0
  59. package/src/errors/index.ts +4 -0
  60. package/src/nymphJoiProps.ts +2 -2
  61. package/src/testArtifacts.ts +18 -19
  62. package/src/testMocks.ts +55 -30
  63. package/src/utils.ts +22 -8
  64. package/tsconfig.json +3 -3
  65. package/typedoc.json +4 -0
package/dist/Entity.d.ts CHANGED
@@ -1,34 +1,273 @@
1
1
  import type Nymph from './Nymph';
2
- import { ACProperties, EntityData, EntityInterface, EntityJson, EntityPatch, EntityReference, SerializedEntityData } from './Entity.types';
2
+ import type { Options } from './Nymph.types';
3
+ import type { ACProperties, EntityConstructor, EntityData, EntityInterface, EntityJson, EntityPatch, EntityReference, SerializedEntityData } from './Entity.types';
4
+ export type EntityDataType<T> = T extends Entity<infer DataType> ? DataType : never;
5
+ export type EntityInstanceType<T extends EntityConstructor> = T extends new () => infer E ? E & EntityDataType<E> : never;
6
+ /**
7
+ * Database abstraction object.
8
+ *
9
+ * Provides a way to access, manipulate, and store data in Nymph.
10
+ *
11
+ * The GUID is not set until the entity is saved. GUIDs must be unique forever,
12
+ * even after deletion. It's the job of the Nymph DB driver to make sure no two
13
+ * entities ever have the same GUID. This is generally done by using a large
14
+ * randomly generated ID.
15
+ *
16
+ * Each entity class has an etype that determines which table(s) in the database
17
+ * it belongs to. If two entity classes have the same etype, their data will be
18
+ * stored in the same table(s). This isn't a good idea, however, because
19
+ * references to an entity store a class name, not an etype.
20
+ *
21
+ * Tags are used to classify entities. Where an etype is used to separate data
22
+ * by tables, tags can be used to separate entities within a table. You can
23
+ * define specific tags to be protected, meaning they cannot be added/removed
24
+ * from the client. It can be useful to allow user defined tags, such as for
25
+ * blog posts.
26
+ *
27
+ * Simply calling $delete() will not unset the entity. It will still take up
28
+ * memory. Likewise, simply calling unset will not delete the entity from the
29
+ * DB.
30
+ *
31
+ * Some notes about $equals() and $is(), the replacements for "==":
32
+ *
33
+ * The == operator will likely not give you the result you want, since two
34
+ * instances of the same entity will fail that check, even though they represent
35
+ * the same data in the database.
36
+ *
37
+ * $equals() performs a more strict comparison of the entity to another. Use
38
+ * $equals() instead of the == operator when you want to check both the entities
39
+ * they represent, and the data inside them. In order to return true for
40
+ * $equals(), the entity and object must meet the following criteria:
41
+ *
42
+ * - They must be entities.
43
+ * - They must have equal GUIDs, or both must have no GUID.
44
+ * - Their data and tags must be equal.
45
+ *
46
+ * $is() performs a less strict comparison of the entity to another. Use $is()
47
+ * instead of the == operator when the entity's data may have been changed, but
48
+ * you only care if they represent the same entity. In order to return true, the
49
+ * entity and object must meet the following criteria:
50
+ *
51
+ * - They must be entities.
52
+ * - They must have equal GUIDs, or both must have no GUID.
53
+ * - If they have no GUIDs, their data and tags must be equal.
54
+ *
55
+ * Some notes about saving entities in other entity's properties:
56
+ *
57
+ * Entities use references in the DB to store an entity in their properties. The
58
+ * reference is stored as an array with the values:
59
+ *
60
+ * - 0 => The string 'nymph_entity_reference'
61
+ * - 1 => The referenced entity's GUID.
62
+ * - 2 => The referenced entity's class name.
63
+ *
64
+ * Since the referenced entity's class name (meaning the `class` static
65
+ * property, not the name of the class itself) is stored in the reference on the
66
+ * parent entity, if you change the class name in an update, you need to
67
+ * reassign all referenced entities of that class and resave.
68
+ *
69
+ * When an entity is loaded, it does not request its referenced entities from
70
+ * Nymph. Instead, it creates instances without data called sleeping references.
71
+ * When you first access an entity's data, if it is a sleeping reference, it
72
+ * will fill its data from the DB. You can call clearCache() to turn all the
73
+ * entities back into sleeping references.
74
+ */
3
75
  export default class Entity<T extends EntityData = EntityData> implements EntityInterface {
76
+ /**
77
+ * The instance of Nymph to use for queries.
78
+ */
4
79
  static nymph: Nymph;
80
+ /**
81
+ * A unique name for this type of entity used to separate its data from other
82
+ * types of entities in the database.
83
+ */
5
84
  static ETYPE: string;
85
+ /**
86
+ * The lookup name for this entity.
87
+ *
88
+ * This is used for reference arrays (and sleeping references) and client
89
+ * requests.
90
+ */
6
91
  static class: string;
7
92
  $nymph: Nymph;
8
93
  guid: string | null;
9
94
  cdate: number | null;
10
95
  mdate: number | null;
11
96
  tags: string[];
97
+ /**
98
+ * The data proxy handler.
99
+ */
12
100
  protected $dataHandler: Object;
101
+ /**
102
+ * The actual data store.
103
+ */
13
104
  protected $dataStore: T;
105
+ /**
106
+ * The actual sdata store.
107
+ */
14
108
  protected $sdata: SerializedEntityData;
109
+ /**
110
+ * The data proxy object.
111
+ */
15
112
  protected $data: T;
113
+ /**
114
+ * Whether this instance is a sleeping reference.
115
+ */
16
116
  protected $isASleepingReference: boolean;
117
+ /**
118
+ * The reference to use to wake.
119
+ */
17
120
  protected $sleepingReference: EntityReference | null;
121
+ /**
122
+ * A promise that resolved when the entity's data is wake.
123
+ */
124
+ protected $wakePromise: Promise<Entity<T>> | null;
125
+ /**
126
+ * Properties that will not be serialized into JSON with toJSON(). This
127
+ * can be considered a denylist, because these properties will not be set
128
+ * with incoming JSON.
129
+ *
130
+ * Clients CAN still determine what is in these properties, unless they are
131
+ * also listed in searchRestrictedData.
132
+ */
18
133
  protected $privateData: string[];
134
+ /**
135
+ * Whether this entity should publish changes to PubSub servers.
136
+ */
137
+ static pubSubEnabled: boolean;
138
+ /**
139
+ * Whether this entity should be accessible on the frontend through the REST
140
+ * server.
141
+ *
142
+ * If this is false, any request from the client that attempts to use this
143
+ * entity will fail.
144
+ */
145
+ static restEnabled: boolean;
146
+ /**
147
+ * Properties that will not be searchable from the frontend. If the frontend
148
+ * includes any of these properties in any of their clauses, they will be
149
+ * filtered out before the search is executed.
150
+ */
19
151
  static searchRestrictedData: string[];
152
+ /**
153
+ * Properties that can only be modified by server side code. They will still
154
+ * be visible on the frontend, unlike $privateData, but any changes to them
155
+ * that come from the frontend will be ignored.
156
+ *
157
+ * In addition to what's listed here, all of the access control properties
158
+ * will be included when Tilmeld is being used. These are:
159
+ *
160
+ * - acUser
161
+ * - acGroup
162
+ * - acOther
163
+ * - acRead
164
+ * - acWrite
165
+ * - acFull
166
+ * - user
167
+ * - group
168
+ *
169
+ * You should modify these through client enabled methods or the $save method
170
+ * instead, for safety.
171
+ */
20
172
  protected $protectedData: string[];
173
+ /**
174
+ * If this is defined, then it lists the only properties that will be
175
+ * accepted from incoming JSON. Any other properties will be ignored.
176
+ *
177
+ * If you use an allowlist, you don't need to use protectedData, since you
178
+ * can simply leave those entries out of allowlistData.
179
+ */
21
180
  protected $allowlistData?: string[];
181
+ /**
182
+ * Tags that can only be added/removed by server side code. They will still be
183
+ * visible on the frontend, but any changes to them that come from the
184
+ * frontend will be ignored.
185
+ */
22
186
  protected $protectedTags: string[];
187
+ /**
188
+ * If this is defined, then it lists the only tags that will be accepted from
189
+ * incoming JSON. Any other tags will be ignored.
190
+ */
23
191
  protected $allowlistTags?: string[];
192
+ /**
193
+ * The names of methods allowed to be called by the frontend with serverCall.
194
+ */
24
195
  protected $clientEnabledMethods: string[];
196
+ /**
197
+ * The names of static methods allowed to be called by the frontend with
198
+ * serverCallStatic.
199
+ */
25
200
  static clientEnabledStaticMethods: string[];
201
+ /**
202
+ * Whether to use "skipAc" when accessing entity references.
203
+ */
26
204
  private $skipAc;
205
+ /**
206
+ * The AC properties' values when the entity was loaded.
207
+ */
27
208
  private $originalAcValues;
28
- constructor(guid?: string);
29
- static factory(guid?: string): Promise<EntityInterface | Entity<EntityData>>;
30
- static factorySync(guid?: string): Entity<EntityData>;
31
- static factoryReference(reference: EntityReference): Entity<EntityData>;
209
+ /**
210
+ * Alter the options for a query for this entity.
211
+ *
212
+ * @param options The current options.
213
+ * @returns The altered options.
214
+ */
215
+ static alterOptions?<T extends Options>(options: T): T;
216
+ /**
217
+ * Initialize an entity.
218
+ */
219
+ constructor(..._rest: any[]);
220
+ /**
221
+ * Create or retrieve a new entity instance.
222
+ *
223
+ * Note that this will always return an entity, even if the GUID is not found.
224
+ *
225
+ * @param guid An optional GUID to retrieve.
226
+ */
227
+ static factory<E extends Entity>(this: {
228
+ new (): E;
229
+ }, guid?: string): Promise<E & EntityDataType<E>>;
230
+ /**
231
+ * Create a new entity instance.
232
+ */
233
+ static factorySync<E extends Entity>(this: {
234
+ new (): E;
235
+ }): E & EntityDataType<E>;
236
+ /**
237
+ * Create a new sleeping reference instance.
238
+ *
239
+ * Sleeping references won't retrieve their data from the database until they
240
+ * are readied with `$wake()` or a parent's `$wakeAll()`.
241
+ *
242
+ * @param reference The Nymph Entity Reference to use to wake.
243
+ * @returns The new instance.
244
+ */
245
+ static factoryReference<E extends Entity>(this: {
246
+ new (): E;
247
+ }, reference: EntityReference): E & EntityDataType<E>;
248
+ /**
249
+ * Get an array of strings that **must** be unique across the current etype.
250
+ *
251
+ * When you try to save another entity with any of the same unique strings,
252
+ * Nymph will throw an error.
253
+ *
254
+ * The default implementation of this static method instantiates the entity,
255
+ * assigns all of the given data, then calls `$getUniques` and returns its
256
+ * output. This can have a performance impact if a lot of extra processing
257
+ * happens during any of these steps. You can override this method to
258
+ * calculate the unique strings faster, but you must return the same strings
259
+ * that would be returned by `$getUniques`.
260
+ *
261
+ * @returns Resolves to an array of entity's unique constraint strings.
262
+ */
263
+ static getUniques({ guid, cdate, mdate, tags, data, sdata, }: {
264
+ guid?: string;
265
+ cdate?: number;
266
+ mdate?: number;
267
+ tags: string[];
268
+ data: EntityData;
269
+ sdata?: SerializedEntityData;
270
+ }): Promise<string[]>;
32
271
  toJSON(): EntityReference | EntityJson | null;
33
272
  $addTag(...tags: string[]): void;
34
273
  $arraySearch(array: any[], strict?: boolean): number;
@@ -38,7 +277,14 @@ export default class Entity<T extends EntityData = EntityData> implements Entity
38
277
  $equals(object: any): boolean;
39
278
  $getData(includeSData?: boolean): any;
40
279
  $getSData(): SerializedEntityData;
280
+ $getUniques(): Promise<string[]>;
41
281
  $getOriginalAcValues(): ACProperties;
282
+ $getCurrentAcValues(): ACProperties;
283
+ $getAcUid(): any;
284
+ $getAcGid(): any;
285
+ $getAcReadIds(): (string | null)[] | null;
286
+ $getAcWriteIds(): (string | null)[] | null;
287
+ $getAcFullIds(): (string | null)[] | null;
42
288
  $getValidatable(): any;
43
289
  $getTags(): string[];
44
290
  $hasTag(...tags: string[]): boolean;
@@ -47,11 +293,27 @@ export default class Entity<T extends EntityData = EntityData> implements Entity
47
293
  $jsonAcceptData(input: EntityJson, allowConflict?: boolean): void;
48
294
  $jsonAcceptPatch(patch: EntityPatch, allowConflict?: boolean): void;
49
295
  $putData(data: EntityData, sdata?: SerializedEntityData): void;
296
+ /**
297
+ * Set up a sleeping reference.
298
+ * @param array $reference The reference to use to wake.
299
+ */
50
300
  protected $referenceSleep(reference: EntityReference): void;
51
- protected $referenceWake(): boolean;
301
+ /**
302
+ * Check if this is a sleeping reference and throw an error if so.
303
+ */
304
+ protected $check(): void;
305
+ /**
306
+ * Check if this is a sleeping reference.
307
+ */
308
+ $asleep(): boolean;
309
+ /**
310
+ * Wake from a sleeping reference.
311
+ */
312
+ $wake(): Promise<Entity<T>>;
313
+ $wakeAll(level?: number): Promise<Entity<T>>;
52
314
  $refresh(): Promise<boolean | 0>;
53
315
  $removeTag(...tags: string[]): void;
54
316
  $save(): Promise<boolean>;
55
- $toReference(): EntityReference | this;
317
+ $toReference(): this | EntityReference;
56
318
  $useSkipAc(skipAc: boolean): void;
57
319
  }