@nymphjs/nymph 1.0.0-beta.11 → 1.0.0-beta.111

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