@acodeninja/persist 3.0.0-next.6 → 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.6",
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
  ]);
@@ -170,7 +170,10 @@ export default class StorageEngine {
170
170
  .map(([propertyName, propertyType]) => [
171
171
  registeredModelName,
172
172
  propertyName,
173
- typeof propertyType === 'function' && !Type.Model.isModel(propertyType) ? propertyType() : propertyType,
173
+ typeof propertyType === 'function' &&
174
+ !/^class/.test(Function.prototype.toString.call(propertyType)) &&
175
+ !Type.Model.isModel(propertyType) ?
176
+ propertyType() : propertyType,
174
177
  ])
175
178
  .filter(([_m, _p, type]) => Type.Model.isModel(type))
176
179
  .map(([containingModel, propertyName, propertyType]) => ({
@@ -276,18 +279,14 @@ export default class StorageEngine {
276
279
 
277
280
 
278
281
  /**
279
- * Decide if two models indexable fields have changed
282
+ * Decide if two models indexable fields are different
280
283
  * @param {Type.Model} currentModel
281
284
  * @param {Type.Model} modelToProcess
282
285
  * @return {boolean}
283
286
  * @private
284
287
  */
285
288
  function indexedFieldsHaveChanged(currentModel, modelToProcess) {
286
- return !currentModel || Boolean(
287
- modelToProcess.constructor.indexedProperties()
288
- .filter(field => JSON.stringify(_.get(currentModel, field)) !== JSON.stringify(_.get(modelToProcess, field)))
289
- .length,
290
- );
289
+ return !currentModel || JSON.stringify(currentModel.toIndexData()) !== JSON.stringify(modelToProcess.toIndexData());
291
290
  }
292
291
 
293
292
  /**
@@ -298,11 +297,7 @@ function indexedFieldsHaveChanged(currentModel, modelToProcess) {
298
297
  * @private
299
298
  */
300
299
  function searchableFieldsHaveChanged(currentModel, modelToProcess) {
301
- return !currentModel || Boolean(
302
- modelToProcess.constructor.searchProperties()
303
- .filter(field => JSON.stringify(_.get(currentModel, field)) !== JSON.stringify(_.get(modelToProcess, field)))
304
- .length,
305
- );
300
+ return !currentModel || JSON.stringify(currentModel.toSearchData()) !== JSON.stringify(modelToProcess.toSearchData());
306
301
  }
307
302
 
308
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
  *