@acodeninja/persist 3.0.0-next.28 → 3.0.0-next.29

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acodeninja/persist",
3
- "version": "3.0.0-next.28",
3
+ "version": "3.0.0-next.29",
4
4
  "description": "A JSON based data modelling and persistence module with alternate storage mechanisms.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/Connection.js CHANGED
@@ -537,7 +537,7 @@ export default class Connection {
537
537
  }
538
538
  }
539
539
  } catch (error) {
540
- for (const operation of operations) {
540
+ for (const operation of operations.slice().reverse()) {
541
541
  if (operation.committed && operation.original) {
542
542
  if (['putModel', 'deleteModel'].includes(operation.method))
543
543
  await this.#storage.putModel(operation.original);
package/src/data/Model.js CHANGED
@@ -151,7 +151,7 @@ class Model {
151
151
  * @static
152
152
  */
153
153
  static get required() {
154
- const ThisModel = this;
154
+ const ModelClass = this;
155
155
 
156
156
  /**
157
157
  * A subclass of the current model with the `_required` flag set to `true`.
@@ -161,11 +161,11 @@ class Model {
161
161
  * @extends {Model}
162
162
  * @private
163
163
  */
164
- class Required extends ThisModel {
164
+ class Required extends ModelClass {
165
165
  static _required = true;
166
166
  }
167
167
 
168
- Object.defineProperty(Required, 'name', {value: ThisModel.name});
168
+ Object.defineProperty(Required, 'name', {value: ModelClass.name});
169
169
 
170
170
  return Required;
171
171
  }
@@ -193,20 +193,21 @@ class Model {
193
193
  * @static
194
194
  */
195
195
  static indexedPropertiesResolved() {
196
+ const ModelClass = this;
196
197
  return [
197
- ...Object.entries(this.properties)
198
- .filter(([name, type]) => !['indexedProperties', 'searchProperties'].includes(name) && !type._type && (this.isModel(type) || (typeof type === 'function' && this.isModel(type()))))
198
+ ...Object.entries(ModelClass.properties)
199
+ .filter(([name, type]) => !['indexedProperties', 'searchProperties'].includes(name) && !type._type && (ModelClass.isModel(type) || (typeof type === 'function' && ModelClass.isModel(type()))))
199
200
  .map(([name, _type]) => `${name}.id`),
200
- ...Object.entries(this.properties)
201
+ ...Object.entries(ModelClass.properties)
201
202
  .filter(([_name, type]) => {
202
203
  return !Model.isModel(type) && (
203
- (type._type === 'array' && this.isModel(type._items))
204
+ (type._type === 'array' && ModelClass.isModel(type._items))
204
205
  ||
205
- (!type._type && typeof type === 'function' && this.isModel(type()._items))
206
+ (!type._type && typeof type === 'function' && ModelClass.isModel(type()._items))
206
207
  );
207
208
  })
208
209
  .map(([name, _type]) => `${name}.[*].id`),
209
- ...this.indexedProperties(),
210
+ ...ModelClass.indexedProperties(),
210
211
  'id',
211
212
  ];
212
213
  }
@@ -230,17 +231,18 @@ class Model {
230
231
  * @static
231
232
  */
232
233
  static fromData(data) {
233
- const model = new this();
234
+ const ModelClass = this;
235
+ const model = new ModelClass();
234
236
 
235
237
  for (const [name, value] of Object.entries(data)) {
236
- if (this[name]?._resolved) continue;
238
+ if (ModelClass[name]?._resolved) continue;
237
239
 
238
- if (this[name]?.name.endsWith('Date')) {
240
+ if (ModelClass[name]?.name.endsWith('Date')) {
239
241
  model[name] = new Date(value);
240
242
  continue;
241
243
  }
242
244
 
243
- if (this[name]?.name.endsWith('ArrayOf(Date)')) {
245
+ if (ModelClass[name]?.name.endsWith('ArrayOf(Date)')) {
244
246
  model[name] = data[name].map(d => new Date(d));
245
247
  continue;
246
248
  }
@@ -275,7 +277,7 @@ class Model {
275
277
  static isDryModel(possibleDryModel) {
276
278
  try {
277
279
  return (
278
- !this.isModel(possibleDryModel) &&
280
+ !Model.isModel(possibleDryModel) &&
279
281
  Object.keys(possibleDryModel).includes('id') &&
280
282
  new RegExp(/[A-Za-z]+\/[A-Z0-9]+/).test(possibleDryModel.id)
281
283
  );
@@ -301,7 +303,8 @@ class Model {
301
303
  * }
302
304
  */
303
305
  static withName(name) {
304
- Object.defineProperty(this, 'name', {value: name});
306
+ const ModelClass = this;
307
+ Object.defineProperty(ModelClass, 'name', {value: name});
305
308
  }
306
309
 
307
310
  /**
@@ -310,11 +313,11 @@ class Model {
310
313
  * @return {Model}
311
314
  */
312
315
  static get properties() {
313
- const modelClass = this;
316
+ const ModelClass = this;
314
317
  const props = {};
315
318
  const chain = [];
316
319
 
317
- let current = modelClass;
320
+ let current = ModelClass;
318
321
  while (current !== Function.prototype) {
319
322
  chain.push(current);
320
323
  current = Object.getPrototypeOf(current);
@@ -347,7 +350,7 @@ class Model {
347
350
  }
348
351
  }
349
352
 
350
- return Object.assign(modelClass, props);
353
+ return Object.assign(ModelClass, props);
351
354
  }
352
355
  }
353
356