@nymphjs/nymph 1.0.0-beta.9 → 1.0.0-beta.90

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +432 -0
  2. package/README.md +10 -20
  3. package/dist/Entity.d.ts +277 -10
  4. package/dist/Entity.js +543 -148
  5. package/dist/Entity.js.map +1 -1
  6. package/dist/Entity.test.js +53 -13
  7. package/dist/Entity.test.js.map +1 -1
  8. package/dist/Entity.types.d.ts +249 -23
  9. package/dist/Entity.types.js +1 -2
  10. package/dist/Nymph.d.ts +431 -12
  11. package/dist/Nymph.js +692 -180
  12. package/dist/Nymph.js.map +1 -1
  13. package/dist/Nymph.types.d.ts +65 -7
  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 +137 -39
  25. package/dist/driver/NymphDriver.js +344 -194
  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 +557 -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.js +10 -15
  63. package/dist/nymphJoiProps.js.map +1 -1
  64. package/dist/testArtifacts.d.ts +18 -6
  65. package/dist/testArtifacts.js +32 -37
  66. package/dist/testArtifacts.js.map +1 -1
  67. package/dist/testMocks.d.ts +15 -12
  68. package/dist/testMocks.js +40 -29
  69. package/dist/testMocks.js.map +1 -1
  70. package/dist/utils.d.ts +4 -4
  71. package/dist/utils.js +40 -29
  72. package/dist/utils.js.map +1 -1
  73. package/jest.config.js +11 -2
  74. package/package.json +22 -19
  75. package/src/Entity.test.ts +52 -7
  76. package/src/Entity.ts +458 -147
  77. package/src/Entity.types.ts +64 -74
  78. package/src/Nymph.ts +754 -321
  79. package/src/Nymph.types.ts +131 -26
  80. package/src/conf/d.ts +25 -0
  81. package/src/conf/defaults.ts +34 -1
  82. package/src/conf/index.ts +2 -2
  83. package/src/driver/NymphDriver.ts +362 -359
  84. package/src/driver/index.ts +2 -2
  85. package/src/driver/testArtifacts.ts +609 -185
  86. package/src/errors/EntityUniqueConstraintError.ts +6 -0
  87. package/src/errors/InvalidStateError.ts +6 -0
  88. package/src/errors/index.ts +12 -8
  89. package/src/index.ts +11 -11
  90. package/src/nymphJoiProps.ts +2 -2
  91. package/src/testArtifacts.ts +19 -20
  92. package/src/testMocks.ts +60 -35
  93. package/src/type/locutus.d.ts +3 -0
  94. package/src/utils.ts +39 -18
  95. package/tsconfig.json +5 -3
  96. package/typedoc.json +4 -0
package/dist/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,56 @@ 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
+ }
197
432
  toJSON() {
198
433
  if (this.$isASleepingReference) {
199
434
  return this.$sleepingReference;
@@ -206,23 +441,34 @@ class Entity {
206
441
  tags: [...this.tags],
207
442
  data: {},
208
443
  };
209
- const data = this.$getData(true);
210
- for (const key in data) {
444
+ for (const key of [
445
+ ...Object.keys(this.$dataStore),
446
+ ...Object.keys(this.$sdata),
447
+ ]) {
211
448
  if (this.$privateData.indexOf(key) === -1) {
212
- obj.data[key] = (0, utils_1.entitiesToReferences)(data[key]);
449
+ obj.data[key] = entitiesToReferences(this.$data[key], true);
213
450
  }
214
451
  }
215
452
  return obj;
216
453
  }
454
+ $getGuaranteedGUID() {
455
+ if (this.guid !== null) {
456
+ return this.guid;
457
+ }
458
+ if (this.$guaranteedGUID == null) {
459
+ this.$guaranteedGUID = guid();
460
+ }
461
+ return this.$guaranteedGUID;
462
+ }
217
463
  $addTag(...tags) {
218
- this.$referenceWake();
464
+ this.$check();
219
465
  if (tags.length < 1) {
220
466
  return;
221
467
  }
222
- this.tags = (0, utils_1.uniqueStrings)([...this.tags, ...tags]);
468
+ this.tags = uniqueStrings([...this.tags, ...tags]);
223
469
  }
224
470
  $arraySearch(array, strict = false) {
225
- this.$referenceWake();
471
+ this.$check();
226
472
  if (!Array.isArray(array)) {
227
473
  return -1;
228
474
  }
@@ -235,18 +481,18 @@ class Entity {
235
481
  return -1;
236
482
  }
237
483
  $clearCache() {
238
- this.$referenceWake();
239
- this.$putData(this.$getData(), this.$getSData());
484
+ this.$check();
485
+ this.$putData(this.$getData(false, true), this.$getSData());
240
486
  }
241
487
  $getClientEnabledMethods() {
242
488
  return this.$clientEnabledMethods;
243
489
  }
244
490
  async $delete() {
245
- this.$referenceWake();
491
+ this.$check();
246
492
  return await this.$nymph.deleteEntity(this);
247
493
  }
248
494
  $equals(object) {
249
- this.$referenceWake();
495
+ this.$check();
250
496
  if (!(object instanceof Entity)) {
251
497
  return false;
252
498
  }
@@ -263,41 +509,82 @@ class Entity {
263
509
  }
264
510
  const obTags = [...object.tags].sort();
265
511
  const myTags = [...this.tags].sort();
266
- if (!(0, lodash_1.isEqual)(obTags, myTags)) {
512
+ if (!isEqual(obTags, myTags)) {
267
513
  return false;
268
514
  }
269
- const obData = object.$getData(true);
270
- const myData = this.$getData(true);
271
- return (0, lodash_1.isEqual)(obData, myData);
515
+ const obData = object.$getData(true, true);
516
+ const myData = this.$getData(true, true);
517
+ return isEqual(obData, myData);
272
518
  }
273
- $getData(includeSData = false) {
274
- this.$referenceWake();
519
+ $getData(includeSData = false, referenceOnlyExisting) {
520
+ this.$check();
275
521
  if (includeSData) {
522
+ // Access all the serialized properties to initialize them.
276
523
  for (const key in this.$sdata) {
277
524
  const _unused = this[key];
278
525
  }
279
526
  }
280
- return (0, utils_1.entitiesToReferences)({ ...this.$dataStore });
527
+ return entitiesToReferences({ ...this.$dataStore }, referenceOnlyExisting);
281
528
  }
282
529
  $getSData() {
283
- this.$referenceWake();
530
+ this.$check();
284
531
  return this.$sdata;
285
532
  }
533
+ async $getUniques() {
534
+ return [];
535
+ }
286
536
  $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
- });
537
+ this.$check();
538
+ return this.$originalAcValues ?? this.$getCurrentAcValues();
539
+ }
540
+ $getCurrentAcValues() {
541
+ this.$check();
542
+ return {
543
+ user: this.$getAcUid(),
544
+ group: this.$getAcGid(),
545
+ acUser: this.$data.acUser ?? null,
546
+ acGroup: this.$data.acGroup ?? null,
547
+ acOther: this.$data.acOther ?? null,
548
+ acRead: this.$getAcReadIds(),
549
+ acWrite: this.$getAcWriteIds(),
550
+ acFull: this.$getAcFullIds(),
551
+ };
552
+ }
553
+ $getAcUid() {
554
+ if ('user' in this.$sdata) {
555
+ const userValue = JSON.parse(this.$sdata.user);
556
+ if (Array.isArray(userValue) &&
557
+ userValue[0] === 'nymph_entity_reference') {
558
+ return userValue[1];
559
+ }
560
+ }
561
+ return (this.$data.user?.guid ?? null);
562
+ }
563
+ $getAcGid() {
564
+ if ('group' in this.$sdata) {
565
+ const groupValue = JSON.parse(this.$sdata.group);
566
+ if (Array.isArray(groupValue) &&
567
+ groupValue[0] === 'nymph_entity_reference') {
568
+ return groupValue[1];
569
+ }
570
+ }
571
+ return (this.$data.group?.guid ?? null);
572
+ }
573
+ $getAcReadIds() {
574
+ return (this.$data.acRead?.map((entity) => entity.guid) ??
575
+ null);
576
+ }
577
+ $getAcWriteIds() {
578
+ return (this.$data.acWrite?.map((entity) => entity.guid) ??
579
+ null);
580
+ }
581
+ $getAcFullIds() {
582
+ return (this.$data.acFull?.map((entity) => entity.guid) ??
583
+ null);
298
584
  }
299
585
  $getValidatable() {
300
- this.$referenceWake();
586
+ this.$check();
587
+ // Access all the serialized properties to initialize them.
301
588
  for (const key in this.$sdata) {
302
589
  const _unused = this[key];
303
590
  }
@@ -306,15 +593,15 @@ class Entity {
306
593
  cdate: this.cdate,
307
594
  mdate: this.mdate,
308
595
  tags: this.tags,
309
- ...(0, utils_1.referencesToEntities)(this.$dataStore, this.$nymph, this.$skipAc),
596
+ ...referencesToEntities(this.$dataStore, this.$nymph, this.$skipAc),
310
597
  };
311
598
  }
312
599
  $getTags() {
313
- this.$referenceWake();
600
+ this.$check();
314
601
  return this.tags;
315
602
  }
316
603
  $hasTag(...tags) {
317
- this.$referenceWake();
604
+ this.$check();
318
605
  if (!tags.length) {
319
606
  return false;
320
607
  }
@@ -332,44 +619,50 @@ class Entity {
332
619
  if (!(object instanceof Entity)) {
333
620
  return false;
334
621
  }
335
- if (this.guid || object.guid) {
622
+ if (this === object) {
623
+ return true;
624
+ }
625
+ if (this.guid != null || object.guid != null) {
336
626
  return this.guid === object.guid;
337
627
  }
338
- this.$referenceWake();
628
+ this.$check();
339
629
  if (typeof object.$getData !== 'function') {
340
630
  return false;
341
631
  }
342
632
  else {
343
633
  const obTags = [...object.tags].sort();
344
634
  const myTags = [...this.tags].sort();
345
- if (!(0, lodash_1.isEqual)(obTags, myTags)) {
635
+ if (!isEqual(obTags, myTags)) {
346
636
  return false;
347
637
  }
348
- const obData = object.$getData(true);
349
- const myData = this.$getData(true);
350
- return (0, lodash_1.isEqual)(obData, myData);
638
+ const obData = object.$getData(true, true);
639
+ const myData = this.$getData(true, true);
640
+ return isEqual(obData, myData);
351
641
  }
352
642
  }
353
643
  $jsonAcceptData(input, allowConflict = false) {
354
- this.$referenceWake();
644
+ this.$check();
355
645
  if (this.guid != input.guid) {
356
- throw new errors_1.EntityConflictError('Tried to accept JSON input for the wrong entity.');
646
+ throw new EntityConflictError('Tried to accept JSON input for the wrong entity.');
357
647
  }
648
+ // Accept the modified date.
358
649
  const mdate = input.mdate ?? 0;
359
650
  const thismdate = this.mdate ?? 0;
360
651
  if (mdate < thismdate && !allowConflict) {
361
- throw new errors_1.EntityConflictError('This entity is newer than JSON input.');
652
+ throw new EntityConflictError('This entity is newer than JSON input.');
362
653
  }
363
654
  this.mdate = input.mdate;
655
+ // Accept the tags.
364
656
  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);
657
+ const protectedTags = intersection(this.$protectedTags, currentTags);
658
+ let tags = difference(input.tags, this.$protectedTags);
367
659
  if (this.$allowlistTags != null) {
368
- tags = (0, lodash_1.intersection)(tags, this.$allowlistTags);
660
+ tags = intersection(tags, this.$allowlistTags);
369
661
  }
370
662
  this.$removeTag(...currentTags);
371
663
  this.$addTag(...protectedTags, ...tags);
372
- const data = input.data;
664
+ // Accept the data.
665
+ const data = { ...input.data };
373
666
  const privateData = {};
374
667
  for (const name of this.$privateData) {
375
668
  if (name in this.$data) {
@@ -381,10 +674,21 @@ class Entity {
381
674
  }
382
675
  const protectedData = {};
383
676
  const protectedProps = [...this.$protectedData];
384
- if (this.$nymph.tilmeld &&
385
- !this.$nymph.tilmeld.gatekeeper('tilmeld/admin')) {
386
- protectedProps.push('user');
387
- protectedProps.push('group');
677
+ if (this.$nymph.tilmeld) {
678
+ protectedProps.push('acUser');
679
+ protectedProps.push('acGroup');
680
+ protectedProps.push('acOther');
681
+ protectedProps.push('acRead');
682
+ protectedProps.push('acWrite');
683
+ protectedProps.push('acFull');
684
+ if ((this.constructor.class !== 'User' &&
685
+ this.constructor.class !== 'Group') ||
686
+ !this.$nymph.tilmeld.currentUser ||
687
+ (!this.$nymph.tilmeld.currentUser.abilities?.includes('tilmeld/admin') &&
688
+ !this.$nymph.tilmeld.currentUser.abilities?.includes('system/admin'))) {
689
+ protectedProps.push('user');
690
+ protectedProps.push('group');
691
+ }
388
692
  }
389
693
  for (const name of protectedProps) {
390
694
  if (name in this.$data) {
@@ -396,7 +700,7 @@ class Entity {
396
700
  }
397
701
  let nonAllowlistData = {};
398
702
  if (this.$allowlistData != null) {
399
- nonAllowlistData = { ...this.$getData(true) };
703
+ nonAllowlistData = { ...this.$getData(true, true) };
400
704
  for (const name of this.$allowlistData) {
401
705
  delete nonAllowlistData[name];
402
706
  }
@@ -414,29 +718,47 @@ class Entity {
414
718
  });
415
719
  }
416
720
  $jsonAcceptPatch(patch, allowConflict = false) {
417
- this.$referenceWake();
721
+ this.$check();
418
722
  if (this.guid != patch.guid) {
419
- throw new errors_1.EntityConflictError('Tried to accept JSON patch for the wrong entity.');
723
+ throw new EntityConflictError('Tried to accept JSON patch for the wrong entity.');
420
724
  }
725
+ // Accept the modified date.
421
726
  const mdate = patch.mdate ?? 0;
422
727
  const thismdate = this.mdate ?? 0;
423
728
  if (mdate < thismdate && !allowConflict) {
424
- throw new errors_1.EntityConflictError('This entity is newer than JSON patch.');
729
+ throw new EntityConflictError('This entity is newer than JSON patch.');
425
730
  }
426
731
  this.mdate = patch.mdate;
732
+ const protectedProps = [...this.$protectedData];
733
+ if (this.$nymph.tilmeld) {
734
+ protectedProps.push('acUser');
735
+ protectedProps.push('acGroup');
736
+ protectedProps.push('acOther');
737
+ protectedProps.push('acRead');
738
+ protectedProps.push('acWrite');
739
+ protectedProps.push('acFull');
740
+ if ((this.constructor.class !== 'User' &&
741
+ this.constructor.class !== 'Group') ||
742
+ !this.$nymph.tilmeld.currentUser ||
743
+ (!this.$nymph.tilmeld.currentUser.abilities?.includes('tilmeld/admin') &&
744
+ !this.$nymph.tilmeld.currentUser.abilities?.includes('system/admin'))) {
745
+ protectedProps.push('user');
746
+ protectedProps.push('group');
747
+ }
748
+ }
427
749
  for (const name in patch.set) {
428
750
  if ((this.$allowlistData != null &&
429
751
  this.$allowlistData.indexOf(name) === -1) ||
430
- this.$protectedData.indexOf(name) !== -1 ||
752
+ protectedProps.indexOf(name) !== -1 ||
431
753
  this.$privateData.indexOf(name) !== -1) {
432
754
  continue;
433
755
  }
434
- this.$data[name] = (0, utils_1.referencesToEntities)(patch.set[name], this.$nymph, this.$skipAc);
756
+ this.$data[name] = referencesToEntities(patch.set[name], this.$nymph, this.$skipAc);
435
757
  }
436
758
  for (const name of patch.unset) {
437
759
  if ((this.$allowlistData != null &&
438
760
  this.$allowlistData.indexOf(name) === -1) ||
439
- this.$protectedData.indexOf(name) !== -1 ||
761
+ protectedProps.indexOf(name) !== -1 ||
440
762
  this.$privateData.indexOf(name) !== -1) {
441
763
  continue;
442
764
  }
@@ -459,8 +781,8 @@ class Entity {
459
781
  this.$removeTag(tag);
460
782
  }
461
783
  }
462
- $putData(data, sdata) {
463
- this.$referenceWake();
784
+ $putData(data, sdata, _source) {
785
+ this.$check();
464
786
  const mySdata = sdata ?? this.$getSData();
465
787
  for (const name in data) {
466
788
  delete mySdata[name];
@@ -469,58 +791,137 @@ class Entity {
469
791
  delete this.$dataStore[name];
470
792
  }
471
793
  for (const name in data) {
472
- this.$dataStore[name] = (0, utils_1.referencesToEntities)(data[name], this.$nymph, this.$skipAc);
794
+ this.$dataStore[name] = referencesToEntities(data[name], this.$nymph, this.$skipAc);
473
795
  }
474
796
  this.$sdata = mySdata;
797
+ // Set original AC values if not set..
475
798
  if (this.$originalAcValues == null) {
476
- this.$originalAcValues = this.$getOriginalAcValues();
799
+ this.$originalAcValues = this.$getCurrentAcValues();
477
800
  }
478
801
  }
802
+ /**
803
+ * Set up a sleeping reference.
804
+ * @param array $reference The reference to use to wake.
805
+ */
479
806
  $referenceSleep(reference) {
480
807
  if (reference.length !== 3 ||
481
808
  reference[0] !== 'nymph_entity_reference' ||
482
809
  typeof reference[1] !== 'string' ||
483
810
  typeof reference[2] !== 'string') {
484
- throw new errors_1.InvalidParametersError('referenceSleep expects parameter 1 to be a valid Nymph entity ' +
811
+ throw new InvalidParametersError('referenceSleep expects parameter 1 to be a valid Nymph entity ' +
485
812
  'reference.');
486
813
  }
487
814
  const thisClass = this.constructor.class;
488
815
  if (reference[2] !== thisClass) {
489
- throw new errors_1.InvalidParametersError('referenceSleep can only be called with an entity reference of the ' +
816
+ throw new InvalidParametersError('referenceSleep can only be called with an entity reference of the ' +
490
817
  `same class. Given class: ${reference[2]}; this class: ${thisClass}.`);
491
818
  }
492
819
  this.$isASleepingReference = true;
493
820
  this.guid = reference[1];
494
821
  this.$sleepingReference = reference;
495
822
  }
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;
823
+ /**
824
+ * Check if this is a sleeping reference and throw an error if so.
825
+ */
826
+ $check() {
827
+ if (this.$isASleepingReference || this.$sleepingReference != null) {
828
+ throw new EntityIsSleepingReferenceError('This entity is in a sleeping reference state. You must use .$wake() to wake it.');
507
829
  }
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;
830
+ }
831
+ /**
832
+ * Check if this is a sleeping reference.
833
+ */
834
+ $asleep() {
835
+ return this.$isASleepingReference || this.$sleepingReference != null;
836
+ }
837
+ /**
838
+ * Wake from a sleeping reference.
839
+ */
840
+ $wake() {
841
+ if (!this.$isASleepingReference || this.$sleepingReference == null) {
842
+ this.$wakePromise = null;
843
+ return Promise.resolve(this);
844
+ }
845
+ if (this.$sleepingReference?.[1] == null) {
846
+ throw new InvalidStateError('Tried to wake a sleeping reference with no GUID.');
847
+ }
848
+ if (!this.$wakePromise) {
849
+ const EntityClass = this.$nymph.getEntityClass(this.$sleepingReference[2]);
850
+ this.$wakePromise = this.$nymph
851
+ .getEntity({
852
+ class: EntityClass,
853
+ skipAc: this.$skipAc,
854
+ }, { type: '&', guid: this.$sleepingReference[1] })
855
+ .then((entity) => {
856
+ if (entity == null || entity.guid == null) {
857
+ return Promise.reject(new InvalidStateError('The sleeping reference could not be retrieved.'));
858
+ }
859
+ this.$isASleepingReference = false;
860
+ this.$sleepingReference = null;
861
+ this.guid = entity.guid;
862
+ this.tags = entity.tags;
863
+ this.cdate = entity.cdate;
864
+ this.mdate = entity.mdate;
865
+ this.$putData(entity.$getData(false, true), entity.$getSData(), 'server');
866
+ return this;
867
+ })
868
+ .finally(() => {
869
+ this.$wakePromise = null;
870
+ });
871
+ }
872
+ return this.$wakePromise;
873
+ }
874
+ $wakeAll(level) {
875
+ return new Promise((resolve, reject) => {
876
+ // Run this once this entity is wake.
877
+ const wakeProps = () => {
878
+ let newLevel;
879
+ // If level is undefined, keep going forever, otherwise, stop once we've
880
+ // gone deep enough.
881
+ if (level !== undefined) {
882
+ newLevel = level - 1;
883
+ }
884
+ if (newLevel !== undefined && newLevel < 0) {
885
+ resolve(this);
886
+ return;
887
+ }
888
+ const promises = [];
889
+ // Go through data looking for entities to wake.
890
+ for (let [key, value] of Object.entries(this.$data)) {
891
+ if (value instanceof Entity && value.$isASleepingReference) {
892
+ promises.push(value.$wakeAll(newLevel));
893
+ }
894
+ else if (Array.isArray(value)) {
895
+ for (let i = 0; i < value.length; i++) {
896
+ if (value[i] instanceof Entity &&
897
+ value[i].$isASleepingReference) {
898
+ promises.push(value[i].$wakeAll(newLevel));
899
+ }
900
+ }
901
+ }
902
+ }
903
+ if (promises.length) {
904
+ Promise.all(promises).then(() => resolve(this), (errObj) => reject(errObj));
905
+ }
906
+ else {
907
+ resolve(this);
908
+ }
909
+ };
910
+ if (this.$isASleepingReference) {
911
+ this.$wake().then(wakeProps, (errObj) => reject(errObj));
912
+ }
913
+ else {
914
+ wakeProps();
915
+ }
916
+ });
516
917
  }
517
918
  async $refresh() {
518
- this.$referenceWake();
919
+ await this.$wake();
519
920
  if (this.guid == null) {
520
921
  return false;
521
922
  }
522
923
  const refresh = await this.$nymph.getEntity({
523
- class: this.constructor,
924
+ class: this.$nymph.getEntityClass(this.constructor),
524
925
  skipCache: true,
525
926
  skipAc: this.$skipAc,
526
927
  }, { type: '&', guid: this.guid });
@@ -530,27 +931,27 @@ class Entity {
530
931
  this.tags = refresh.tags;
531
932
  this.cdate = refresh.cdate;
532
933
  this.mdate = refresh.mdate;
533
- this.$putData(refresh.$getData(), refresh.$getSData());
934
+ this.$putData(refresh.$getData(false, true), refresh.$getSData(), 'server');
534
935
  return true;
535
936
  }
536
937
  $removeTag(...tags) {
537
- this.$referenceWake();
538
- this.tags = (0, lodash_1.difference)(this.tags, tags);
938
+ this.$check();
939
+ this.tags = difference(this.tags, tags);
539
940
  }
540
941
  async $save() {
541
- this.$referenceWake();
942
+ await this.$wake();
542
943
  return await this.$nymph.saveEntity(this);
543
944
  }
544
- $toReference() {
945
+ $toReference(existingOnly) {
545
946
  if (this.$isASleepingReference && this.$sleepingReference != null) {
546
947
  return this.$sleepingReference;
547
948
  }
548
- if (this.guid == null) {
949
+ if (existingOnly && this.guid == null) {
549
950
  return this;
550
951
  }
551
952
  return [
552
953
  'nymph_entity_reference',
553
- this.guid,
954
+ this.$getGuaranteedGUID(),
554
955
  this.constructor.class,
555
956
  ];
556
957
  }
@@ -558,10 +959,4 @@ class Entity {
558
959
  this.$skipAc = !!skipAc;
559
960
  }
560
961
  }
561
- exports.default = Entity;
562
- Entity.nymph = {};
563
- Entity.ETYPE = 'entity';
564
- Entity.class = 'Entity';
565
- Entity.searchRestrictedData = [];
566
- Entity.clientEnabledStaticMethods = [];
567
962
  //# sourceMappingURL=Entity.js.map