@acodeninja/persist 3.0.0-next.7 → 3.0.0-next.8

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.7",
3
+ "version": "3.0.0-next.8",
4
4
  "description": "A JSON based data modelling and persistence module with alternate storage mechanisms.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -79,7 +79,11 @@ class SchemaCompiler {
79
79
  schema.properties[name].items.format = property?._items._format;
80
80
  }
81
81
 
82
- if (Type.Model.isModel(property?._items)) {
82
+ const prop = typeof property?._items === 'function' &&
83
+ !/^class/.test(Function.prototype.toString.call(property?._items)) ?
84
+ property?._items() : property?._items;
85
+
86
+ if (Type.Model.isModel(prop)) {
83
87
  schema.properties[name].items = {
84
88
  type: 'object',
85
89
  additionalProperties: false,
@@ -87,7 +91,7 @@ class SchemaCompiler {
87
91
  properties: {
88
92
  id: {
89
93
  type: 'string',
90
- pattern: `^${property?._items.toString()}/[A-Z0-9]+$`,
94
+ pattern: `^${prop.toString()}/[A-Z0-9]+$`,
91
95
  },
92
96
  },
93
97
  };
@@ -86,7 +86,7 @@ export default class StorageEngine {
86
86
 
87
87
  await this._putSearchIndex(modelConstructor, {
88
88
  ...index || {},
89
- ...Object.fromEntries(models.map(m => [m.id, m.toIndexData()])),
89
+ ...Object.fromEntries(models.map(m => [m.id, m.toSearchData()])),
90
90
  });
91
91
  })),
92
92
  ]);
@@ -279,18 +279,14 @@ export default class StorageEngine {
279
279
 
280
280
 
281
281
  /**
282
- * Decide if two models indexable fields have changed
282
+ * Decide if two models indexable fields are different
283
283
  * @param {Type.Model} currentModel
284
284
  * @param {Type.Model} modelToProcess
285
285
  * @return {boolean}
286
286
  * @private
287
287
  */
288
288
  function indexedFieldsHaveChanged(currentModel, modelToProcess) {
289
- return !currentModel || Boolean(
290
- modelToProcess.constructor.indexedProperties()
291
- .filter(field => JSON.stringify(_.get(currentModel, field)) !== JSON.stringify(_.get(modelToProcess, field)))
292
- .length,
293
- );
289
+ return !currentModel || JSON.stringify(currentModel.toIndexData()) !== JSON.stringify(modelToProcess.toIndexData());
294
290
  }
295
291
 
296
292
  /**
@@ -301,11 +297,7 @@ function indexedFieldsHaveChanged(currentModel, modelToProcess) {
301
297
  * @private
302
298
  */
303
299
  function searchableFieldsHaveChanged(currentModel, modelToProcess) {
304
- return !currentModel || Boolean(
305
- modelToProcess.constructor.searchProperties()
306
- .filter(field => JSON.stringify(_.get(currentModel, field)) !== JSON.stringify(_.get(modelToProcess, field)))
307
- .length,
308
- );
300
+ return !currentModel || JSON.stringify(currentModel.toSearchData()) !== JSON.stringify(modelToProcess.toSearchData());
309
301
  }
310
302
 
311
303
  /**
package/src/type/Model.js CHANGED
@@ -100,11 +100,11 @@ class Model {
100
100
 
101
101
  /**
102
102
  * Extracts data from the model based on the indexed properties defined in the class.
103
- *
103
+ * Includes the ID of any linked models.
104
104
  * @returns {Object} - A representation of the model's indexed data.
105
105
  */
106
106
  toIndexData() {
107
- return this._extractData(this.constructor.indexedProperties());
107
+ return this._extractData(this.constructor.indexedPropertiesResolved());
108
108
  }
109
109
 
110
110
  /**
@@ -187,6 +187,39 @@ class Model {
187
187
  return [];
188
188
  }
189
189
 
190
+ /**
191
+ * Returns a list of properties that are indexed including links to other models.
192
+ *
193
+ * @returns {Array<string>} - The indexed properties.
194
+ * @abstract
195
+ * @static
196
+ */
197
+ static indexedPropertiesResolved() {
198
+ return []
199
+ .concat(
200
+ Object.entries(this)
201
+ .filter(([_, type]) =>
202
+ this.isModel(
203
+ typeof type === 'function' &&
204
+ !/^class/.test(Function.prototype.toString.call(type)) ?
205
+ type() : type,
206
+ ),
207
+ )
208
+ .map(([name, _]) => `${name}.id`),
209
+ )
210
+ .concat(
211
+ Object.entries(this)
212
+ .filter(([_, type]) =>
213
+ type?._type === 'array' && this.isModel(
214
+ typeof type._items === 'function' &&
215
+ !/^class/.test(Function.prototype.toString.call(type._items)) ?
216
+ type._items() : type._items,
217
+ ),
218
+ ).map(([name, _]) => `${name}.[*].id`),
219
+ )
220
+ .concat(this.indexedProperties());
221
+ }
222
+
190
223
  /**
191
224
  * Returns a list of properties used for search.
192
225
  *