@nymphjs/nymph 1.0.0-beta.1 → 1.0.0-beta.100

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 +535 -0
  2. package/README.md +14 -22
  3. package/dist/Entity.d.ts +293 -10
  4. package/dist/Entity.js +571 -149
  5. package/dist/Entity.js.map +1 -1
  6. package/dist/Entity.test.js +67 -26
  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 +497 -13
  11. package/dist/Nymph.js +761 -188
  12. package/dist/Nymph.js.map +1 -1
  13. package/dist/Nymph.types.d.ts +95 -26
  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 +181 -43
  25. package/dist/driver/NymphDriver.js +532 -285
  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 +939 -219
  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 +19 -6
  65. package/dist/testArtifacts.js +32 -37
  66. package/dist/testArtifacts.js.map +1 -1
  67. package/dist/testMocks.d.ts +28 -11
  68. package/dist/testMocks.js +44 -28
  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 +24 -20
  75. package/src/Entity.test.ts +63 -13
  76. package/src/Entity.ts +498 -156
  77. package/src/Entity.types.ts +72 -74
  78. package/src/Nymph.ts +904 -336
  79. package/src/Nymph.types.ts +179 -44
  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 +650 -460
  84. package/src/driver/index.ts +2 -2
  85. package/src/driver/testArtifacts.ts +966 -196
  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 +20 -20
  92. package/src/testMocks.ts +85 -37
  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,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
  }
@@ -255,47 +521,90 @@ class Entity {
255
521
  return false;
256
522
  }
257
523
  }
258
- if (object.constructor !== this.constructor) {
259
- return false;
260
- }
261
524
  if (object.cdate !== this.cdate) {
262
525
  return false;
263
526
  }
264
527
  if (object.mdate !== this.mdate) {
265
528
  return false;
266
529
  }
267
- const obData = object.$getData(true);
268
- const myData = this.$getData(true);
269
- return (0, lodash_1.isEqual)(obData, myData);
530
+ const obTags = [...object.tags].sort();
531
+ const myTags = [...this.tags].sort();
532
+ if (!isEqual(obTags, myTags)) {
533
+ return false;
534
+ }
535
+ const obData = object.$getData(true, true);
536
+ const myData = this.$getData(true, true);
537
+ return isEqual(obData, myData);
270
538
  }
271
- $getData(includeSData = false) {
272
- this.$referenceWake();
539
+ $getData(includeSData = false, referenceOnlyExisting) {
540
+ this.$check();
273
541
  if (includeSData) {
542
+ // Access all the serialized properties to initialize them.
274
543
  for (const key in this.$sdata) {
275
544
  const _unused = this[key];
276
545
  }
277
546
  }
278
- return (0, utils_1.entitiesToReferences)({ ...this.$dataStore });
547
+ return entitiesToReferences({ ...this.$dataStore }, referenceOnlyExisting);
279
548
  }
280
549
  $getSData() {
281
- this.$referenceWake();
550
+ this.$check();
282
551
  return this.$sdata;
283
552
  }
553
+ async $getUniques() {
554
+ return [];
555
+ }
284
556
  $getOriginalAcValues() {
285
- this.$referenceWake();
286
- return (this.$originalAcValues ?? {
287
- user: this.$data.user,
288
- group: this.$data.group,
289
- acUser: this.$data.acUser,
290
- acGroup: this.$data.acGroup,
291
- acOther: this.$data.acOther,
292
- acRead: this.$data.acRead,
293
- acWrite: this.$data.acWrite,
294
- acFull: this.$data.acFull,
295
- });
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);
296
604
  }
297
605
  $getValidatable() {
298
- this.$referenceWake();
606
+ this.$check();
607
+ // Access all the serialized properties to initialize them.
299
608
  for (const key in this.$sdata) {
300
609
  const _unused = this[key];
301
610
  }
@@ -304,15 +613,15 @@ class Entity {
304
613
  cdate: this.cdate,
305
614
  mdate: this.mdate,
306
615
  tags: this.tags,
307
- ...(0, utils_1.referencesToEntities)(this.$dataStore, this.$nymph, this.$skipAc),
616
+ ...referencesToEntities(this.$dataStore, this.$nymph, this.$skipAc),
308
617
  };
309
618
  }
310
619
  $getTags() {
311
- this.$referenceWake();
620
+ this.$check();
312
621
  return this.tags;
313
622
  }
314
623
  $hasTag(...tags) {
315
- this.$referenceWake();
624
+ this.$check();
316
625
  if (!tags.length) {
317
626
  return false;
318
627
  }
@@ -330,39 +639,50 @@ class Entity {
330
639
  if (!(object instanceof Entity)) {
331
640
  return false;
332
641
  }
333
- if (this.guid || object.guid) {
642
+ if (this === object) {
643
+ return true;
644
+ }
645
+ if (this.guid != null || object.guid != null) {
334
646
  return this.guid === object.guid;
335
647
  }
336
- this.$referenceWake();
648
+ this.$check();
337
649
  if (typeof object.$getData !== 'function') {
338
650
  return false;
339
651
  }
340
652
  else {
341
- const obData = object.$getData(true);
342
- const myData = this.$getData(true);
343
- return (0, lodash_1.isEqual)(obData, myData);
653
+ const obTags = [...object.tags].sort();
654
+ const myTags = [...this.tags].sort();
655
+ if (!isEqual(obTags, myTags)) {
656
+ return false;
657
+ }
658
+ const obData = object.$getData(true, true);
659
+ const myData = this.$getData(true, true);
660
+ return isEqual(obData, myData);
344
661
  }
345
662
  }
346
663
  $jsonAcceptData(input, allowConflict = false) {
347
- this.$referenceWake();
664
+ this.$check();
348
665
  if (this.guid != input.guid) {
349
- 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.');
350
667
  }
668
+ // Accept the modified date.
351
669
  const mdate = input.mdate ?? 0;
352
670
  const thismdate = this.mdate ?? 0;
353
671
  if (mdate < thismdate && !allowConflict) {
354
- throw new errors_1.EntityConflictError('This entity is newer than JSON input.');
672
+ throw new EntityConflictError('This entity is newer than JSON input.');
355
673
  }
356
674
  this.mdate = input.mdate;
675
+ // Accept the tags.
357
676
  const currentTags = this.$getTags();
358
- const protectedTags = (0, lodash_1.intersection)(this.$protectedTags, currentTags);
359
- 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);
360
679
  if (this.$allowlistTags != null) {
361
- tags = (0, lodash_1.intersection)(tags, this.$allowlistTags);
680
+ tags = intersection(tags, this.$allowlistTags);
362
681
  }
363
682
  this.$removeTag(...currentTags);
364
683
  this.$addTag(...protectedTags, ...tags);
365
- const data = input.data;
684
+ // Accept the data.
685
+ const data = { ...input.data };
366
686
  const privateData = {};
367
687
  for (const name of this.$privateData) {
368
688
  if (name in this.$data) {
@@ -374,10 +694,21 @@ class Entity {
374
694
  }
375
695
  const protectedData = {};
376
696
  const protectedProps = [...this.$protectedData];
377
- if (this.$nymph.tilmeld &&
378
- !this.$nymph.tilmeld.gatekeeper('tilmeld/admin')) {
379
- protectedProps.push('user');
380
- 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
+ }
381
712
  }
382
713
  for (const name of protectedProps) {
383
714
  if (name in this.$data) {
@@ -389,7 +720,7 @@ class Entity {
389
720
  }
390
721
  let nonAllowlistData = {};
391
722
  if (this.$allowlistData != null) {
392
- nonAllowlistData = { ...this.$getData(true) };
723
+ nonAllowlistData = { ...this.$getData(true, true) };
393
724
  for (const name of this.$allowlistData) {
394
725
  delete nonAllowlistData[name];
395
726
  }
@@ -407,29 +738,47 @@ class Entity {
407
738
  });
408
739
  }
409
740
  $jsonAcceptPatch(patch, allowConflict = false) {
410
- this.$referenceWake();
741
+ this.$check();
411
742
  if (this.guid != patch.guid) {
412
- 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.');
413
744
  }
745
+ // Accept the modified date.
414
746
  const mdate = patch.mdate ?? 0;
415
747
  const thismdate = this.mdate ?? 0;
416
748
  if (mdate < thismdate && !allowConflict) {
417
- throw new errors_1.EntityConflictError('This entity is newer than JSON patch.');
749
+ throw new EntityConflictError('This entity is newer than JSON patch.');
418
750
  }
419
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
+ }
420
769
  for (const name in patch.set) {
421
770
  if ((this.$allowlistData != null &&
422
771
  this.$allowlistData.indexOf(name) === -1) ||
423
- this.$protectedData.indexOf(name) !== -1 ||
772
+ protectedProps.indexOf(name) !== -1 ||
424
773
  this.$privateData.indexOf(name) !== -1) {
425
774
  continue;
426
775
  }
427
- 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);
428
777
  }
429
778
  for (const name of patch.unset) {
430
779
  if ((this.$allowlistData != null &&
431
780
  this.$allowlistData.indexOf(name) === -1) ||
432
- this.$protectedData.indexOf(name) !== -1 ||
781
+ protectedProps.indexOf(name) !== -1 ||
433
782
  this.$privateData.indexOf(name) !== -1) {
434
783
  continue;
435
784
  }
@@ -452,8 +801,8 @@ class Entity {
452
801
  this.$removeTag(tag);
453
802
  }
454
803
  }
455
- $putData(data, sdata) {
456
- this.$referenceWake();
804
+ $putData(data, sdata, _source) {
805
+ this.$check();
457
806
  const mySdata = sdata ?? this.$getSData();
458
807
  for (const name in data) {
459
808
  delete mySdata[name];
@@ -462,58 +811,137 @@ class Entity {
462
811
  delete this.$dataStore[name];
463
812
  }
464
813
  for (const name in data) {
465
- this.$dataStore[name] = (0, utils_1.referencesToEntities)(data[name], this.$nymph, this.$skipAc);
814
+ this.$dataStore[name] = referencesToEntities(data[name], this.$nymph, this.$skipAc);
466
815
  }
467
816
  this.$sdata = mySdata;
817
+ // Set original AC values if not set..
468
818
  if (this.$originalAcValues == null) {
469
- this.$originalAcValues = this.$getOriginalAcValues();
819
+ this.$originalAcValues = this.$getCurrentAcValues();
470
820
  }
471
821
  }
822
+ /**
823
+ * Set up a sleeping reference.
824
+ * @param array $reference The reference to use to wake.
825
+ */
472
826
  $referenceSleep(reference) {
473
827
  if (reference.length !== 3 ||
474
828
  reference[0] !== 'nymph_entity_reference' ||
475
829
  typeof reference[1] !== 'string' ||
476
830
  typeof reference[2] !== 'string') {
477
- 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 ' +
478
832
  'reference.');
479
833
  }
480
834
  const thisClass = this.constructor.class;
481
835
  if (reference[2] !== thisClass) {
482
- 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 ' +
483
837
  `same class. Given class: ${reference[2]}; this class: ${thisClass}.`);
484
838
  }
485
839
  this.$isASleepingReference = true;
486
840
  this.guid = reference[1];
487
841
  this.$sleepingReference = reference;
488
842
  }
489
- $referenceWake() {
490
- if (!this.$isASleepingReference || this.$sleepingReference == null) {
491
- return true;
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.');
492
849
  }
493
- const EntityClass = this.$nymph.getEntityClass(this.$sleepingReference[2]);
494
- const entity = this.$nymph.driver.getEntitySync({
495
- class: EntityClass,
496
- skipAc: this.$skipAc,
497
- }, { type: '&', guid: this.$sleepingReference[1] });
498
- if (entity == null) {
499
- return false;
500
- }
501
- this.$isASleepingReference = false;
502
- this.$sleepingReference = null;
503
- this.guid = entity.guid;
504
- this.tags = entity.tags;
505
- this.cdate = entity.cdate;
506
- this.mdate = entity.mdate;
507
- this.$putData(entity.$getData(), entity.$getSData());
508
- 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
+ });
509
937
  }
510
938
  async $refresh() {
511
- this.$referenceWake();
939
+ await this.$wake();
512
940
  if (this.guid == null) {
513
941
  return false;
514
942
  }
515
943
  const refresh = await this.$nymph.getEntity({
516
- class: this.constructor,
944
+ class: this.$nymph.getEntityClass(this.constructor),
517
945
  skipCache: true,
518
946
  skipAc: this.$skipAc,
519
947
  }, { type: '&', guid: this.guid });
@@ -523,27 +951,27 @@ class Entity {
523
951
  this.tags = refresh.tags;
524
952
  this.cdate = refresh.cdate;
525
953
  this.mdate = refresh.mdate;
526
- this.$putData(refresh.$getData(), refresh.$getSData());
954
+ this.$putData(refresh.$getData(false, true), refresh.$getSData(), 'server');
527
955
  return true;
528
956
  }
529
957
  $removeTag(...tags) {
530
- this.$referenceWake();
531
- this.tags = (0, lodash_1.difference)(this.tags, tags);
958
+ this.$check();
959
+ this.tags = difference(this.tags, tags);
532
960
  }
533
961
  async $save() {
534
- this.$referenceWake();
962
+ await this.$wake();
535
963
  return await this.$nymph.saveEntity(this);
536
964
  }
537
- $toReference() {
965
+ $toReference(existingOnly) {
538
966
  if (this.$isASleepingReference && this.$sleepingReference != null) {
539
967
  return this.$sleepingReference;
540
968
  }
541
- if (this.guid == null) {
969
+ if (existingOnly && this.guid == null) {
542
970
  return this;
543
971
  }
544
972
  return [
545
973
  'nymph_entity_reference',
546
- this.guid,
974
+ this.$getGuaranteedGUID(),
547
975
  this.constructor.class,
548
976
  ];
549
977
  }
@@ -551,10 +979,4 @@ class Entity {
551
979
  this.$skipAc = !!skipAc;
552
980
  }
553
981
  }
554
- exports.default = Entity;
555
- Entity.nymph = {};
556
- Entity.ETYPE = 'entity';
557
- Entity.class = 'Entity';
558
- Entity.searchRestrictedData = [];
559
- Entity.clientEnabledStaticMethods = [];
560
982
  //# sourceMappingURL=Entity.js.map