@nymphjs/nymph 1.0.0-beta.10 → 1.0.0-beta.101

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 (97) hide show
  1. package/CHANGELOG.md +487 -0
  2. package/README.md +10 -20
  3. package/dist/Entity.d.ts +293 -10
  4. package/dist/Entity.js +563 -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 +496 -12
  11. package/dist/Nymph.js +752 -179
  12. package/dist/Nymph.js.map +1 -1
  13. package/dist/Nymph.types.d.ts +77 -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 +174 -40
  25. package/dist/driver/NymphDriver.js +469 -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 +814 -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.d.ts +1 -1
  63. package/dist/nymphJoiProps.js +10 -15
  64. package/dist/nymphJoiProps.js.map +1 -1
  65. package/dist/testArtifacts.d.ts +19 -6
  66. package/dist/testArtifacts.js +32 -37
  67. package/dist/testArtifacts.js.map +1 -1
  68. package/dist/testMocks.d.ts +27 -12
  69. package/dist/testMocks.js +40 -29
  70. package/dist/testMocks.js.map +1 -1
  71. package/dist/utils.d.ts +4 -4
  72. package/dist/utils.js +40 -29
  73. package/dist/utils.js.map +1 -1
  74. package/jest.config.js +11 -2
  75. package/package.json +23 -19
  76. package/src/Entity.test.ts +50 -7
  77. package/src/Entity.ts +483 -147
  78. package/src/Entity.types.ts +72 -74
  79. package/src/Nymph.ts +868 -322
  80. package/src/Nymph.types.ts +146 -27
  81. package/src/conf/d.ts +25 -0
  82. package/src/conf/defaults.ts +34 -1
  83. package/src/conf/index.ts +2 -2
  84. package/src/driver/NymphDriver.ts +562 -414
  85. package/src/driver/index.ts +2 -2
  86. package/src/driver/testArtifacts.ts +954 -185
  87. package/src/errors/EntityUniqueConstraintError.ts +6 -0
  88. package/src/errors/InvalidStateError.ts +6 -0
  89. package/src/errors/index.ts +12 -8
  90. package/src/index.ts +11 -11
  91. package/src/nymphJoiProps.ts +2 -2
  92. package/src/testArtifacts.ts +20 -20
  93. package/src/testMocks.ts +76 -35
  94. package/src/type/locutus.d.ts +3 -0
  95. package/src/utils.ts +39 -18
  96. package/tsconfig.json +5 -3
  97. package/typedoc.json +4 -0
package/dist/Entity.js CHANGED
@@ -1,32 +1,228 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const lodash_1 = require("lodash");
4
- const errors_1 = require("./errors");
5
- const utils_1 = require("./utils");
6
- class Entity {
7
- constructor(guid) {
8
- this.guid = null;
9
- this.cdate = null;
10
- this.mdate = null;
11
- this.tags = [];
12
- this.$isASleepingReference = false;
13
- this.$sleepingReference = null;
14
- this.$privateData = [];
15
- this.$protectedData = [];
16
- this.$protectedTags = [];
17
- this.$clientEnabledMethods = [];
18
- this.$skipAc = false;
19
- this.$originalAcValues = null;
1
+ import { difference, intersection, isEqual } from 'lodash-es';
2
+ import { guid } from '@nymphjs/guid';
3
+ import { EntityConflictError, EntityIsSleepingReferenceError, InvalidParametersError, InvalidStateError, } from './errors/index.js';
4
+ import { entitiesToReferences, referencesToEntities, uniqueStrings, } from './utils.js';
5
+ /**
6
+ * Database abstraction object.
7
+ *
8
+ * Provides a way to access, manipulate, and store data in Nymph.
9
+ *
10
+ * The GUID is not set until the entity is saved. GUIDs must be unique forever,
11
+ * even after deletion. It's the job of the Nymph DB driver to make sure no two
12
+ * entities ever have the same GUID. This is generally done by using a large
13
+ * randomly generated ID.
14
+ *
15
+ * Each entity class has an etype that determines which table(s) in the database
16
+ * it belongs to. If two entity classes have the same etype, their data will be
17
+ * stored in the same table(s). This isn't a good idea, however, because
18
+ * references to an entity store a class name, not an etype.
19
+ *
20
+ * Tags are used to classify entities. Where an etype is used to separate data
21
+ * by tables, tags can be used to separate entities within a table. You can
22
+ * define specific tags to be protected, meaning they cannot be added/removed
23
+ * from the client. It can be useful to allow user defined tags, such as for
24
+ * blog posts.
25
+ *
26
+ * Simply calling $delete() will not unset the entity. It will still take up
27
+ * memory. Likewise, simply calling unset will not delete the entity from the
28
+ * DB.
29
+ *
30
+ * Some notes about $equals() and $is(), the replacements for "==":
31
+ *
32
+ * The == operator will likely not give you the result you want, since two
33
+ * instances of the same entity will fail that check, even though they represent
34
+ * the same data in the database.
35
+ *
36
+ * $equals() performs a more strict comparison of the entity to another. Use
37
+ * $equals() instead of the == operator when you want to check both the entities
38
+ * they represent, and the data inside them. In order to return true for
39
+ * $equals(), the entity and object must meet the following criteria:
40
+ *
41
+ * - They must be entities.
42
+ * - They must have equal GUIDs, or both must have no GUID.
43
+ * - Their data and tags must be equal.
44
+ *
45
+ * $is() performs a less strict comparison of the entity to another. Use $is()
46
+ * instead of the == operator when the entity's data may have been changed, but
47
+ * you only care if they represent the same entity. In order to return true, the
48
+ * entity and object must meet the following criteria:
49
+ *
50
+ * - They must be entities.
51
+ * - They must have equal GUIDs, or both must have no GUID.
52
+ * - If they have no GUIDs, their data and tags must be equal.
53
+ *
54
+ * Some notes about saving entities in other entity's properties:
55
+ *
56
+ * Entities use references in the DB to store an entity in their properties. The
57
+ * reference is stored as an array with the values:
58
+ *
59
+ * - 0 => The string 'nymph_entity_reference'
60
+ * - 1 => The referenced entity's GUID.
61
+ * - 2 => The referenced entity's class name.
62
+ *
63
+ * Since the referenced entity's class name (meaning the `class` static
64
+ * property, not the name of the class itself) is stored in the reference on the
65
+ * parent entity, if you change the class name in an update, you need to
66
+ * reassign all referenced entities of that class and resave.
67
+ *
68
+ * When an entity is loaded, it does not request its referenced entities from
69
+ * Nymph. Instead, it creates instances without data called sleeping references.
70
+ * When you first access an entity's data, if it is a sleeping reference, it
71
+ * will fill its data from the DB. You can call clearCache() to turn all the
72
+ * entities back into sleeping references.
73
+ */
74
+ export default class Entity {
75
+ /**
76
+ * The instance of Nymph to use for queries.
77
+ */
78
+ static nymph = {};
79
+ /**
80
+ * A unique name for this type of entity used to separate its data from other
81
+ * types of entities in the database.
82
+ */
83
+ static ETYPE = 'entity';
84
+ /**
85
+ * The lookup name for this entity.
86
+ *
87
+ * This is used for reference arrays (and sleeping references) and client
88
+ * requests.
89
+ */
90
+ static class = 'Entity';
91
+ $nymph;
92
+ guid = null;
93
+ cdate = null;
94
+ mdate = null;
95
+ tags = [];
96
+ /**
97
+ * The data proxy handler.
98
+ */
99
+ $dataHandler;
100
+ /**
101
+ * The actual data store.
102
+ */
103
+ $dataStore;
104
+ /**
105
+ * The actual sdata store.
106
+ */
107
+ $sdata;
108
+ /**
109
+ * The data proxy object.
110
+ */
111
+ $data;
112
+ /**
113
+ * Whether this instance is a sleeping reference.
114
+ */
115
+ $isASleepingReference = false;
116
+ /**
117
+ * The reference to use to wake.
118
+ */
119
+ $sleepingReference = null;
120
+ /**
121
+ * A promise that resolved when the entity's data is wake.
122
+ */
123
+ $wakePromise = null;
124
+ /**
125
+ * Properties that will not be serialized into JSON with toJSON(). This
126
+ * can be considered a denylist, because these properties will not be set
127
+ * with incoming JSON.
128
+ *
129
+ * Clients CAN still determine what is in these properties, unless they are
130
+ * also listed in searchRestrictedData.
131
+ */
132
+ $privateData = [];
133
+ /**
134
+ * Whether this entity should publish changes to PubSub servers.
135
+ */
136
+ static pubSubEnabled = true;
137
+ /**
138
+ * Whether this entity should be accessible on the frontend through the REST
139
+ * server.
140
+ *
141
+ * If this is false, any request from the client that attempts to use this
142
+ * entity will fail.
143
+ */
144
+ static restEnabled = true;
145
+ /**
146
+ * Properties that will not be searchable from the frontend. If the frontend
147
+ * includes any of these properties in any of their clauses, they will be
148
+ * filtered out before the search is executed.
149
+ */
150
+ static searchRestrictedData = [];
151
+ /**
152
+ * Properties that can only be modified by server side code. They will still
153
+ * be visible on the frontend, unlike $privateData, but any changes to them
154
+ * that come from the frontend will be ignored.
155
+ *
156
+ * In addition to what's listed here, all of the access control properties
157
+ * will be included when Tilmeld is being used. These are:
158
+ *
159
+ * - acUser
160
+ * - acGroup
161
+ * - acOther
162
+ * - acRead
163
+ * - acWrite
164
+ * - acFull
165
+ * - user
166
+ * - group
167
+ *
168
+ * You should modify these through client enabled methods or the $save method
169
+ * instead, for safety.
170
+ */
171
+ $protectedData = [];
172
+ /**
173
+ * If this is defined, then it lists the only properties that will be
174
+ * accepted from incoming JSON. Any other properties will be ignored.
175
+ *
176
+ * If you use an allowlist, you don't need to use protectedData, since you
177
+ * can simply leave those entries out of allowlistData.
178
+ */
179
+ $allowlistData;
180
+ /**
181
+ * Tags that can only be added/removed by server side code. They will still be
182
+ * visible on the frontend, but any changes to them that come from the
183
+ * frontend will be ignored.
184
+ */
185
+ $protectedTags = [];
186
+ /**
187
+ * If this is defined, then it lists the only tags that will be accepted from
188
+ * incoming JSON. Any other tags will be ignored.
189
+ */
190
+ $allowlistTags;
191
+ /**
192
+ * The names of methods allowed to be called by the frontend with serverCall.
193
+ */
194
+ $clientEnabledMethods = [];
195
+ /**
196
+ * The names of static methods allowed to be called by the frontend with
197
+ * serverCallStatic.
198
+ */
199
+ static clientEnabledStaticMethods = [];
200
+ /**
201
+ * Whether to use "skipAc" when accessing entity references.
202
+ */
203
+ $skipAc = false;
204
+ /**
205
+ * The AC properties' values when the entity was loaded.
206
+ */
207
+ $originalAcValues = null;
208
+ /**
209
+ * This is used to hold a generated GUID for a new entity.
210
+ */
211
+ $guaranteedGUID = null;
212
+ /**
213
+ * Initialize an entity.
214
+ */
215
+ constructor(..._rest) {
20
216
  this.$nymph = this.constructor.nymph;
21
217
  this.$dataHandler = {
22
218
  has: (data, name) => {
23
- this.$referenceWake();
219
+ this.$check();
24
220
  return name in data || name in this.$sdata;
25
221
  },
26
222
  get: (data, name) => {
27
- this.$referenceWake();
223
+ this.$check();
28
224
  if (this.$sdata.hasOwnProperty(name)) {
29
- data[name] = (0, utils_1.referencesToEntities)(JSON.parse(this.$sdata[name]), this.$nymph, this.$skipAc);
225
+ data[name] = referencesToEntities(JSON.parse(this.$sdata[name]), this.$nymph, this.$skipAc);
30
226
  delete this.$sdata[name];
31
227
  }
32
228
  if (data.hasOwnProperty(name)) {
@@ -35,7 +231,7 @@ class Entity {
35
231
  return undefined;
36
232
  },
37
233
  set: (data, name, value) => {
38
- this.$referenceWake();
234
+ this.$check();
39
235
  if (this.$sdata.hasOwnProperty(name)) {
40
236
  delete this.$sdata[name];
41
237
  }
@@ -43,7 +239,7 @@ class Entity {
43
239
  return true;
44
240
  },
45
241
  deleteProperty: (data, name) => {
46
- this.$referenceWake();
242
+ this.$check();
47
243
  if (this.$sdata.hasOwnProperty(name)) {
48
244
  return delete this.$sdata[name];
49
245
  }
@@ -53,7 +249,7 @@ class Entity {
53
249
  return true;
54
250
  },
55
251
  defineProperty: (data, name, descriptor) => {
56
- this.$referenceWake();
252
+ this.$check();
57
253
  if (this.$sdata.hasOwnProperty(name)) {
58
254
  delete this.$sdata[name];
59
255
  }
@@ -61,31 +257,21 @@ class Entity {
61
257
  return true;
62
258
  },
63
259
  getOwnPropertyDescriptor: (data, name) => {
64
- this.$referenceWake();
260
+ this.$check();
65
261
  if (this.$sdata.hasOwnProperty(name)) {
66
- data[name] = (0, utils_1.referencesToEntities)(JSON.parse(this.$sdata[name]), this.$nymph, this.$skipAc);
262
+ data[name] = referencesToEntities(JSON.parse(this.$sdata[name]), this.$nymph, this.$skipAc);
67
263
  delete this.$sdata[name];
68
264
  }
69
265
  return Object.getOwnPropertyDescriptor(data, name);
70
266
  },
71
267
  ownKeys: (data) => {
72
- this.$referenceWake();
268
+ this.$check();
73
269
  return Object.getOwnPropertyNames(data).concat(Object.getOwnPropertyNames(this.$sdata));
74
270
  },
75
271
  };
76
272
  this.$dataStore = {};
77
273
  this.$sdata = {};
78
274
  this.$data = new Proxy(this.$dataStore, this.$dataHandler);
79
- if (guid != null) {
80
- const entity = this.$nymph.driver.getEntitySync({ class: this.constructor }, { type: '&', guid });
81
- if (entity) {
82
- this.guid = entity.guid;
83
- this.tags = entity.tags;
84
- this.cdate = entity.cdate;
85
- this.mdate = entity.mdate;
86
- this.$putData(entity.$getData(), entity.$getSData());
87
- }
88
- }
89
275
  return new Proxy(this, {
90
276
  has: (entity, name) => {
91
277
  if (typeof name !== 'string' ||
@@ -93,7 +279,7 @@ class Entity {
93
279
  name.substring(0, 1) === '$') {
94
280
  return name in entity;
95
281
  }
96
- this.$referenceWake();
282
+ this.$check();
97
283
  return name in entity.$data;
98
284
  },
99
285
  get: (entity, name) => {
@@ -101,11 +287,11 @@ class Entity {
101
287
  name in entity ||
102
288
  name.substring(0, 1) === '$') {
103
289
  if (name === 'tags' || name === 'cdate' || name === 'mdate') {
104
- this.$referenceWake();
290
+ this.$check();
105
291
  }
106
292
  return entity[name];
107
293
  }
108
- this.$referenceWake();
294
+ this.$check();
109
295
  if (name in entity.$data) {
110
296
  return entity.$data[name];
111
297
  }
@@ -116,12 +302,12 @@ class Entity {
116
302
  name in entity ||
117
303
  name.substring(0, 1) === '$') {
118
304
  if (name === 'tags' || name === 'cdate' || name === 'mdate') {
119
- this.$referenceWake();
305
+ this.$check();
120
306
  }
121
307
  entity[name] = value;
122
308
  }
123
309
  else {
124
- this.$referenceWake();
310
+ this.$check();
125
311
  entity.$data[name] = value;
126
312
  }
127
313
  return true;
@@ -131,7 +317,7 @@ class Entity {
131
317
  return delete entity[name];
132
318
  }
133
319
  else if (name in entity.$data) {
134
- this.$referenceWake();
320
+ this.$check();
135
321
  return delete entity.$data[name];
136
322
  }
137
323
  return true;
@@ -144,12 +330,12 @@ class Entity {
144
330
  name in entity ||
145
331
  name.substring(0, 1) === '$') {
146
332
  if (name === 'tags' || name === 'cdate' || name === 'mdate') {
147
- this.$referenceWake();
333
+ this.$check();
148
334
  }
149
335
  Object.defineProperty(entity, name, descriptor);
150
336
  }
151
337
  else {
152
- this.$referenceWake();
338
+ this.$check();
153
339
  Object.defineProperty(entity.$data, name, descriptor);
154
340
  }
155
341
  return true;
@@ -159,21 +345,28 @@ class Entity {
159
345
  name in entity ||
160
346
  name.substring(0, 1) === '$') {
161
347
  if (name === 'tags' || name === 'cdate' || name === 'mdate') {
162
- this.$referenceWake();
348
+ this.$check();
163
349
  }
164
350
  return Object.getOwnPropertyDescriptor(entity, name);
165
351
  }
166
352
  else {
167
- this.$referenceWake();
353
+ this.$check();
168
354
  return Object.getOwnPropertyDescriptor(entity.$data, name);
169
355
  }
170
356
  },
171
357
  ownKeys: (entity) => {
172
- this.$referenceWake();
358
+ this.$check();
173
359
  return Object.getOwnPropertyNames(entity).concat(Object.getOwnPropertyNames(entity.$data));
174
360
  },
175
361
  });
176
362
  }
363
+ /**
364
+ * Create or retrieve a new entity instance.
365
+ *
366
+ * Note that this will always return an entity, even if the GUID is not found.
367
+ *
368
+ * @param guid An optional GUID to retrieve.
369
+ */
177
370
  static async factory(guid) {
178
371
  const entity = new this();
179
372
  if (guid != null) {
@@ -186,14 +379,76 @@ class Entity {
186
379
  }
187
380
  return entity;
188
381
  }
189
- static factorySync(guid) {
190
- return new this(guid);
382
+ /**
383
+ * Create a new entity instance.
384
+ */
385
+ static factorySync() {
386
+ return new this();
191
387
  }
388
+ /**
389
+ * Create a new sleeping reference instance.
390
+ *
391
+ * Sleeping references won't retrieve their data from the database until they
392
+ * are readied with `$wake()` or a parent's `$wakeAll()`.
393
+ *
394
+ * @param reference The Nymph Entity Reference to use to wake.
395
+ * @returns The new instance.
396
+ */
192
397
  static factoryReference(reference) {
193
398
  const entity = new this();
194
399
  entity.$referenceSleep(reference);
195
400
  return entity;
196
401
  }
402
+ /**
403
+ * Get an array of strings that **must** be unique across the current etype.
404
+ *
405
+ * When you try to save another entity with any of the same unique strings,
406
+ * Nymph will throw an error.
407
+ *
408
+ * The default implementation of this static method instantiates the entity,
409
+ * assigns all of the given data, then calls `$getUniques` and returns its
410
+ * output. This can have a performance impact if a lot of extra processing
411
+ * happens during any of these steps. You can override this method to
412
+ * calculate the unique strings faster, but you must return the same strings
413
+ * that would be returned by `$getUniques`.
414
+ *
415
+ * @returns Resolves to an array of entity's unique constraint strings.
416
+ */
417
+ static async getUniques({ guid, cdate, mdate, tags, data, sdata, }) {
418
+ const entity = new this();
419
+ if (guid != null) {
420
+ entity.guid = guid;
421
+ }
422
+ if (cdate != null) {
423
+ entity.cdate = cdate;
424
+ }
425
+ if (mdate != null) {
426
+ entity.mdate = mdate;
427
+ }
428
+ entity.tags = tags;
429
+ entity.$putData(data, sdata);
430
+ return await entity.$getUniques();
431
+ }
432
+ /**
433
+ * Get a string for full text search for one of an entity's properties.
434
+ *
435
+ * The result will be tokenized and stored as the full text search index for
436
+ * use with "search" clauses.
437
+ *
438
+ * Return null to not include any tokens in the full text search storage.
439
+ *
440
+ * You shouldn't use any other entity data to transform the text, as it won't
441
+ * always be available. This function is meant for things like stripping HTML
442
+ * tags.
443
+ *
444
+ * @returns By default, returns the value if it is a string.
445
+ */
446
+ static getFTSText(_name, value) {
447
+ if (typeof value === 'string') {
448
+ return value;
449
+ }
450
+ return null;
451
+ }
197
452
  toJSON() {
198
453
  if (this.$isASleepingReference) {
199
454
  return this.$sleepingReference;
@@ -206,23 +461,34 @@ class Entity {
206
461
  tags: [...this.tags],
207
462
  data: {},
208
463
  };
209
- const data = this.$getData(true);
210
- for (const key in data) {
464
+ for (const key of [
465
+ ...Object.keys(this.$dataStore),
466
+ ...Object.keys(this.$sdata),
467
+ ]) {
211
468
  if (this.$privateData.indexOf(key) === -1) {
212
- obj.data[key] = (0, utils_1.entitiesToReferences)(data[key]);
469
+ obj.data[key] = entitiesToReferences(this.$data[key], true);
213
470
  }
214
471
  }
215
472
  return obj;
216
473
  }
474
+ $getGuaranteedGUID() {
475
+ if (this.guid !== null) {
476
+ return this.guid;
477
+ }
478
+ if (this.$guaranteedGUID == null) {
479
+ this.$guaranteedGUID = guid();
480
+ }
481
+ return this.$guaranteedGUID;
482
+ }
217
483
  $addTag(...tags) {
218
- this.$referenceWake();
484
+ this.$check();
219
485
  if (tags.length < 1) {
220
486
  return;
221
487
  }
222
- this.tags = (0, utils_1.uniqueStrings)([...this.tags, ...tags]);
488
+ this.tags = uniqueStrings([...this.tags, ...tags]);
223
489
  }
224
490
  $arraySearch(array, strict = false) {
225
- this.$referenceWake();
491
+ this.$check();
226
492
  if (!Array.isArray(array)) {
227
493
  return -1;
228
494
  }
@@ -235,18 +501,18 @@ class Entity {
235
501
  return -1;
236
502
  }
237
503
  $clearCache() {
238
- this.$referenceWake();
239
- this.$putData(this.$getData(), this.$getSData());
504
+ this.$check();
505
+ this.$putData(this.$getData(false, true), this.$getSData());
240
506
  }
241
507
  $getClientEnabledMethods() {
242
508
  return this.$clientEnabledMethods;
243
509
  }
244
510
  async $delete() {
245
- this.$referenceWake();
511
+ this.$check();
246
512
  return await this.$nymph.deleteEntity(this);
247
513
  }
248
514
  $equals(object) {
249
- this.$referenceWake();
515
+ this.$check();
250
516
  if (!(object instanceof Entity)) {
251
517
  return false;
252
518
  }
@@ -263,41 +529,82 @@ class Entity {
263
529
  }
264
530
  const obTags = [...object.tags].sort();
265
531
  const myTags = [...this.tags].sort();
266
- if (!(0, lodash_1.isEqual)(obTags, myTags)) {
532
+ if (!isEqual(obTags, myTags)) {
267
533
  return false;
268
534
  }
269
- const obData = object.$getData(true);
270
- const myData = this.$getData(true);
271
- return (0, lodash_1.isEqual)(obData, myData);
535
+ const obData = object.$getData(true, true);
536
+ const myData = this.$getData(true, true);
537
+ return isEqual(obData, myData);
272
538
  }
273
- $getData(includeSData = false) {
274
- this.$referenceWake();
539
+ $getData(includeSData = false, referenceOnlyExisting) {
540
+ this.$check();
275
541
  if (includeSData) {
542
+ // Access all the serialized properties to initialize them.
276
543
  for (const key in this.$sdata) {
277
544
  const _unused = this[key];
278
545
  }
279
546
  }
280
- return (0, utils_1.entitiesToReferences)({ ...this.$dataStore });
547
+ return entitiesToReferences({ ...this.$dataStore }, referenceOnlyExisting);
281
548
  }
282
549
  $getSData() {
283
- this.$referenceWake();
550
+ this.$check();
284
551
  return this.$sdata;
285
552
  }
553
+ async $getUniques() {
554
+ return [];
555
+ }
286
556
  $getOriginalAcValues() {
287
- this.$referenceWake();
288
- return (this.$originalAcValues ?? {
289
- user: this.$data.user,
290
- group: this.$data.group,
291
- acUser: this.$data.acUser,
292
- acGroup: this.$data.acGroup,
293
- acOther: this.$data.acOther,
294
- acRead: this.$data.acRead,
295
- acWrite: this.$data.acWrite,
296
- acFull: this.$data.acFull,
297
- });
557
+ this.$check();
558
+ return this.$originalAcValues ?? this.$getCurrentAcValues();
559
+ }
560
+ $getCurrentAcValues() {
561
+ this.$check();
562
+ return {
563
+ user: this.$getAcUid(),
564
+ group: this.$getAcGid(),
565
+ acUser: this.$data.acUser ?? null,
566
+ acGroup: this.$data.acGroup ?? null,
567
+ acOther: this.$data.acOther ?? null,
568
+ acRead: this.$getAcReadIds(),
569
+ acWrite: this.$getAcWriteIds(),
570
+ acFull: this.$getAcFullIds(),
571
+ };
572
+ }
573
+ $getAcUid() {
574
+ if ('user' in this.$sdata) {
575
+ const userValue = JSON.parse(this.$sdata.user);
576
+ if (Array.isArray(userValue) &&
577
+ userValue[0] === 'nymph_entity_reference') {
578
+ return userValue[1];
579
+ }
580
+ }
581
+ return (this.$data.user?.guid ?? null);
582
+ }
583
+ $getAcGid() {
584
+ if ('group' in this.$sdata) {
585
+ const groupValue = JSON.parse(this.$sdata.group);
586
+ if (Array.isArray(groupValue) &&
587
+ groupValue[0] === 'nymph_entity_reference') {
588
+ return groupValue[1];
589
+ }
590
+ }
591
+ return (this.$data.group?.guid ?? null);
592
+ }
593
+ $getAcReadIds() {
594
+ return (this.$data.acRead?.map((entity) => entity.guid) ??
595
+ null);
596
+ }
597
+ $getAcWriteIds() {
598
+ return (this.$data.acWrite?.map((entity) => entity.guid) ??
599
+ null);
600
+ }
601
+ $getAcFullIds() {
602
+ return (this.$data.acFull?.map((entity) => entity.guid) ??
603
+ null);
298
604
  }
299
605
  $getValidatable() {
300
- this.$referenceWake();
606
+ this.$check();
607
+ // Access all the serialized properties to initialize them.
301
608
  for (const key in this.$sdata) {
302
609
  const _unused = this[key];
303
610
  }
@@ -306,15 +613,15 @@ class Entity {
306
613
  cdate: this.cdate,
307
614
  mdate: this.mdate,
308
615
  tags: this.tags,
309
- ...(0, utils_1.referencesToEntities)(this.$dataStore, this.$nymph, this.$skipAc),
616
+ ...referencesToEntities(this.$dataStore, this.$nymph, this.$skipAc),
310
617
  };
311
618
  }
312
619
  $getTags() {
313
- this.$referenceWake();
620
+ this.$check();
314
621
  return this.tags;
315
622
  }
316
623
  $hasTag(...tags) {
317
- this.$referenceWake();
624
+ this.$check();
318
625
  if (!tags.length) {
319
626
  return false;
320
627
  }
@@ -332,44 +639,50 @@ class Entity {
332
639
  if (!(object instanceof Entity)) {
333
640
  return false;
334
641
  }
335
- if (this.guid || object.guid) {
642
+ if (this === object) {
643
+ return true;
644
+ }
645
+ if (this.guid != null || object.guid != null) {
336
646
  return this.guid === object.guid;
337
647
  }
338
- this.$referenceWake();
648
+ this.$check();
339
649
  if (typeof object.$getData !== 'function') {
340
650
  return false;
341
651
  }
342
652
  else {
343
653
  const obTags = [...object.tags].sort();
344
654
  const myTags = [...this.tags].sort();
345
- if (!(0, lodash_1.isEqual)(obTags, myTags)) {
655
+ if (!isEqual(obTags, myTags)) {
346
656
  return false;
347
657
  }
348
- const obData = object.$getData(true);
349
- const myData = this.$getData(true);
350
- return (0, lodash_1.isEqual)(obData, myData);
658
+ const obData = object.$getData(true, true);
659
+ const myData = this.$getData(true, true);
660
+ return isEqual(obData, myData);
351
661
  }
352
662
  }
353
663
  $jsonAcceptData(input, allowConflict = false) {
354
- this.$referenceWake();
664
+ this.$check();
355
665
  if (this.guid != input.guid) {
356
- throw new errors_1.EntityConflictError('Tried to accept JSON input for the wrong entity.');
666
+ throw new EntityConflictError('Tried to accept JSON input for the wrong entity.');
357
667
  }
668
+ // Accept the modified date.
358
669
  const mdate = input.mdate ?? 0;
359
670
  const thismdate = this.mdate ?? 0;
360
671
  if (mdate < thismdate && !allowConflict) {
361
- throw new errors_1.EntityConflictError('This entity is newer than JSON input.');
672
+ throw new EntityConflictError('This entity is newer than JSON input.');
362
673
  }
363
674
  this.mdate = input.mdate;
675
+ // Accept the tags.
364
676
  const currentTags = this.$getTags();
365
- const protectedTags = (0, lodash_1.intersection)(this.$protectedTags, currentTags);
366
- let tags = (0, lodash_1.difference)(input.tags, this.$protectedTags);
677
+ const protectedTags = intersection(this.$protectedTags, currentTags);
678
+ let tags = difference(input.tags, this.$protectedTags);
367
679
  if (this.$allowlistTags != null) {
368
- tags = (0, lodash_1.intersection)(tags, this.$allowlistTags);
680
+ tags = intersection(tags, this.$allowlistTags);
369
681
  }
370
682
  this.$removeTag(...currentTags);
371
683
  this.$addTag(...protectedTags, ...tags);
372
- const data = input.data;
684
+ // Accept the data.
685
+ const data = { ...input.data };
373
686
  const privateData = {};
374
687
  for (const name of this.$privateData) {
375
688
  if (name in this.$data) {
@@ -381,10 +694,21 @@ class Entity {
381
694
  }
382
695
  const protectedData = {};
383
696
  const protectedProps = [...this.$protectedData];
384
- if (this.$nymph.tilmeld &&
385
- !this.$nymph.tilmeld.gatekeeper('tilmeld/admin')) {
386
- protectedProps.push('user');
387
- protectedProps.push('group');
697
+ if (this.$nymph.tilmeld) {
698
+ protectedProps.push('acUser');
699
+ protectedProps.push('acGroup');
700
+ protectedProps.push('acOther');
701
+ protectedProps.push('acRead');
702
+ protectedProps.push('acWrite');
703
+ protectedProps.push('acFull');
704
+ if ((this.constructor.class !== 'User' &&
705
+ this.constructor.class !== 'Group') ||
706
+ !this.$nymph.tilmeld.currentUser ||
707
+ (!this.$nymph.tilmeld.currentUser.abilities?.includes('tilmeld/admin') &&
708
+ !this.$nymph.tilmeld.currentUser.abilities?.includes('system/admin'))) {
709
+ protectedProps.push('user');
710
+ protectedProps.push('group');
711
+ }
388
712
  }
389
713
  for (const name of protectedProps) {
390
714
  if (name in this.$data) {
@@ -396,7 +720,7 @@ class Entity {
396
720
  }
397
721
  let nonAllowlistData = {};
398
722
  if (this.$allowlistData != null) {
399
- nonAllowlistData = { ...this.$getData(true) };
723
+ nonAllowlistData = { ...this.$getData(true, true) };
400
724
  for (const name of this.$allowlistData) {
401
725
  delete nonAllowlistData[name];
402
726
  }
@@ -414,29 +738,47 @@ class Entity {
414
738
  });
415
739
  }
416
740
  $jsonAcceptPatch(patch, allowConflict = false) {
417
- this.$referenceWake();
741
+ this.$check();
418
742
  if (this.guid != patch.guid) {
419
- throw new errors_1.EntityConflictError('Tried to accept JSON patch for the wrong entity.');
743
+ throw new EntityConflictError('Tried to accept JSON patch for the wrong entity.');
420
744
  }
745
+ // Accept the modified date.
421
746
  const mdate = patch.mdate ?? 0;
422
747
  const thismdate = this.mdate ?? 0;
423
748
  if (mdate < thismdate && !allowConflict) {
424
- throw new errors_1.EntityConflictError('This entity is newer than JSON patch.');
749
+ throw new EntityConflictError('This entity is newer than JSON patch.');
425
750
  }
426
751
  this.mdate = patch.mdate;
752
+ const protectedProps = [...this.$protectedData];
753
+ if (this.$nymph.tilmeld) {
754
+ protectedProps.push('acUser');
755
+ protectedProps.push('acGroup');
756
+ protectedProps.push('acOther');
757
+ protectedProps.push('acRead');
758
+ protectedProps.push('acWrite');
759
+ protectedProps.push('acFull');
760
+ if ((this.constructor.class !== 'User' &&
761
+ this.constructor.class !== 'Group') ||
762
+ !this.$nymph.tilmeld.currentUser ||
763
+ (!this.$nymph.tilmeld.currentUser.abilities?.includes('tilmeld/admin') &&
764
+ !this.$nymph.tilmeld.currentUser.abilities?.includes('system/admin'))) {
765
+ protectedProps.push('user');
766
+ protectedProps.push('group');
767
+ }
768
+ }
427
769
  for (const name in patch.set) {
428
770
  if ((this.$allowlistData != null &&
429
771
  this.$allowlistData.indexOf(name) === -1) ||
430
- this.$protectedData.indexOf(name) !== -1 ||
772
+ protectedProps.indexOf(name) !== -1 ||
431
773
  this.$privateData.indexOf(name) !== -1) {
432
774
  continue;
433
775
  }
434
- this.$data[name] = (0, utils_1.referencesToEntities)(patch.set[name], this.$nymph, this.$skipAc);
776
+ this.$data[name] = referencesToEntities(patch.set[name], this.$nymph, this.$skipAc);
435
777
  }
436
778
  for (const name of patch.unset) {
437
779
  if ((this.$allowlistData != null &&
438
780
  this.$allowlistData.indexOf(name) === -1) ||
439
- this.$protectedData.indexOf(name) !== -1 ||
781
+ protectedProps.indexOf(name) !== -1 ||
440
782
  this.$privateData.indexOf(name) !== -1) {
441
783
  continue;
442
784
  }
@@ -459,8 +801,8 @@ class Entity {
459
801
  this.$removeTag(tag);
460
802
  }
461
803
  }
462
- $putData(data, sdata) {
463
- this.$referenceWake();
804
+ $putData(data, sdata, _source) {
805
+ this.$check();
464
806
  const mySdata = sdata ?? this.$getSData();
465
807
  for (const name in data) {
466
808
  delete mySdata[name];
@@ -469,58 +811,137 @@ class Entity {
469
811
  delete this.$dataStore[name];
470
812
  }
471
813
  for (const name in data) {
472
- this.$dataStore[name] = (0, utils_1.referencesToEntities)(data[name], this.$nymph, this.$skipAc);
814
+ this.$dataStore[name] = referencesToEntities(data[name], this.$nymph, this.$skipAc);
473
815
  }
474
816
  this.$sdata = mySdata;
817
+ // Set original AC values if not set..
475
818
  if (this.$originalAcValues == null) {
476
- this.$originalAcValues = this.$getOriginalAcValues();
819
+ this.$originalAcValues = this.$getCurrentAcValues();
477
820
  }
478
821
  }
822
+ /**
823
+ * Set up a sleeping reference.
824
+ * @param array $reference The reference to use to wake.
825
+ */
479
826
  $referenceSleep(reference) {
480
827
  if (reference.length !== 3 ||
481
828
  reference[0] !== 'nymph_entity_reference' ||
482
829
  typeof reference[1] !== 'string' ||
483
830
  typeof reference[2] !== 'string') {
484
- throw new errors_1.InvalidParametersError('referenceSleep expects parameter 1 to be a valid Nymph entity ' +
831
+ throw new InvalidParametersError('referenceSleep expects parameter 1 to be a valid Nymph entity ' +
485
832
  'reference.');
486
833
  }
487
834
  const thisClass = this.constructor.class;
488
835
  if (reference[2] !== thisClass) {
489
- throw new errors_1.InvalidParametersError('referenceSleep can only be called with an entity reference of the ' +
836
+ throw new InvalidParametersError('referenceSleep can only be called with an entity reference of the ' +
490
837
  `same class. Given class: ${reference[2]}; this class: ${thisClass}.`);
491
838
  }
492
839
  this.$isASleepingReference = true;
493
840
  this.guid = reference[1];
494
841
  this.$sleepingReference = reference;
495
842
  }
496
- $referenceWake() {
497
- if (!this.$isASleepingReference || this.$sleepingReference == null) {
498
- return true;
499
- }
500
- const EntityClass = this.$nymph.getEntityClass(this.$sleepingReference[2]);
501
- const entity = this.$nymph.driver.getEntitySync({
502
- class: EntityClass,
503
- skipAc: this.$skipAc,
504
- }, { type: '&', guid: this.$sleepingReference[1] });
505
- if (entity == null) {
506
- return false;
843
+ /**
844
+ * Check if this is a sleeping reference and throw an error if so.
845
+ */
846
+ $check() {
847
+ if (this.$isASleepingReference || this.$sleepingReference != null) {
848
+ throw new EntityIsSleepingReferenceError('This entity is in a sleeping reference state. You must use .$wake() to wake it.');
507
849
  }
508
- this.$isASleepingReference = false;
509
- this.$sleepingReference = null;
510
- this.guid = entity.guid;
511
- this.tags = entity.tags;
512
- this.cdate = entity.cdate;
513
- this.mdate = entity.mdate;
514
- this.$putData(entity.$getData(), entity.$getSData());
515
- return true;
850
+ }
851
+ /**
852
+ * Check if this is a sleeping reference.
853
+ */
854
+ $asleep() {
855
+ return this.$isASleepingReference || this.$sleepingReference != null;
856
+ }
857
+ /**
858
+ * Wake from a sleeping reference.
859
+ */
860
+ $wake() {
861
+ if (!this.$isASleepingReference || this.$sleepingReference == null) {
862
+ this.$wakePromise = null;
863
+ return Promise.resolve(this);
864
+ }
865
+ if (this.$sleepingReference?.[1] == null) {
866
+ throw new InvalidStateError('Tried to wake a sleeping reference with no GUID.');
867
+ }
868
+ if (!this.$wakePromise) {
869
+ const EntityClass = this.$nymph.getEntityClass(this.$sleepingReference[2]);
870
+ this.$wakePromise = this.$nymph
871
+ .getEntity({
872
+ class: EntityClass,
873
+ skipAc: this.$skipAc,
874
+ }, { type: '&', guid: this.$sleepingReference[1] })
875
+ .then((entity) => {
876
+ if (entity == null || entity.guid == null) {
877
+ return Promise.reject(new InvalidStateError('The sleeping reference could not be retrieved.'));
878
+ }
879
+ this.$isASleepingReference = false;
880
+ this.$sleepingReference = null;
881
+ this.guid = entity.guid;
882
+ this.tags = entity.tags;
883
+ this.cdate = entity.cdate;
884
+ this.mdate = entity.mdate;
885
+ this.$putData(entity.$getData(false, true), entity.$getSData(), 'server');
886
+ return this;
887
+ })
888
+ .finally(() => {
889
+ this.$wakePromise = null;
890
+ });
891
+ }
892
+ return this.$wakePromise;
893
+ }
894
+ $wakeAll(level) {
895
+ return new Promise((resolve, reject) => {
896
+ // Run this once this entity is wake.
897
+ const wakeProps = () => {
898
+ let newLevel;
899
+ // If level is undefined, keep going forever, otherwise, stop once we've
900
+ // gone deep enough.
901
+ if (level !== undefined) {
902
+ newLevel = level - 1;
903
+ }
904
+ if (newLevel !== undefined && newLevel < 0) {
905
+ resolve(this);
906
+ return;
907
+ }
908
+ const promises = [];
909
+ // Go through data looking for entities to wake.
910
+ for (let [key, value] of Object.entries(this.$data)) {
911
+ if (value instanceof Entity && value.$isASleepingReference) {
912
+ promises.push(value.$wakeAll(newLevel));
913
+ }
914
+ else if (Array.isArray(value)) {
915
+ for (let i = 0; i < value.length; i++) {
916
+ if (value[i] instanceof Entity &&
917
+ value[i].$isASleepingReference) {
918
+ promises.push(value[i].$wakeAll(newLevel));
919
+ }
920
+ }
921
+ }
922
+ }
923
+ if (promises.length) {
924
+ Promise.all(promises).then(() => resolve(this), (errObj) => reject(errObj));
925
+ }
926
+ else {
927
+ resolve(this);
928
+ }
929
+ };
930
+ if (this.$isASleepingReference) {
931
+ this.$wake().then(wakeProps, (errObj) => reject(errObj));
932
+ }
933
+ else {
934
+ wakeProps();
935
+ }
936
+ });
516
937
  }
517
938
  async $refresh() {
518
- this.$referenceWake();
939
+ await this.$wake();
519
940
  if (this.guid == null) {
520
941
  return false;
521
942
  }
522
943
  const refresh = await this.$nymph.getEntity({
523
- class: this.constructor,
944
+ class: this.$nymph.getEntityClass(this.constructor),
524
945
  skipCache: true,
525
946
  skipAc: this.$skipAc,
526
947
  }, { type: '&', guid: this.guid });
@@ -530,27 +951,27 @@ class Entity {
530
951
  this.tags = refresh.tags;
531
952
  this.cdate = refresh.cdate;
532
953
  this.mdate = refresh.mdate;
533
- this.$putData(refresh.$getData(), refresh.$getSData());
954
+ this.$putData(refresh.$getData(false, true), refresh.$getSData(), 'server');
534
955
  return true;
535
956
  }
536
957
  $removeTag(...tags) {
537
- this.$referenceWake();
538
- this.tags = (0, lodash_1.difference)(this.tags, tags);
958
+ this.$check();
959
+ this.tags = difference(this.tags, tags);
539
960
  }
540
961
  async $save() {
541
- this.$referenceWake();
962
+ await this.$wake();
542
963
  return await this.$nymph.saveEntity(this);
543
964
  }
544
- $toReference() {
965
+ $toReference(existingOnly) {
545
966
  if (this.$isASleepingReference && this.$sleepingReference != null) {
546
967
  return this.$sleepingReference;
547
968
  }
548
- if (this.guid == null) {
969
+ if (existingOnly && this.guid == null) {
549
970
  return this;
550
971
  }
551
972
  return [
552
973
  'nymph_entity_reference',
553
- this.guid,
974
+ this.$getGuaranteedGUID(),
554
975
  this.constructor.class,
555
976
  ];
556
977
  }
@@ -558,10 +979,4 @@ class Entity {
558
979
  this.$skipAc = !!skipAc;
559
980
  }
560
981
  }
561
- exports.default = Entity;
562
- Entity.nymph = {};
563
- Entity.ETYPE = 'entity';
564
- Entity.class = 'Entity';
565
- Entity.searchRestrictedData = [];
566
- Entity.clientEnabledStaticMethods = [];
567
982
  //# sourceMappingURL=Entity.js.map