@acodeninja/persist 2.4.1-next.2 → 3.0.0-next.10

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.
@@ -1,22 +1,22 @@
1
- import Engine, {EngineError, MissConfiguredError} from './Engine.js';
1
+ import StorageEngine, {EngineError, MissConfiguredError} from './StorageEngine.js';
2
2
 
3
3
  /**
4
4
  * Represents an error specific to HTTP engine operations.
5
- * @class HTTPEngineError
5
+ * @class HTTPStorageEngineError
6
6
  * @extends EngineError
7
7
  */
8
- export class HTTPEngineError extends EngineError {}
8
+ export class HTTPStorageEngineError extends EngineError {}
9
9
 
10
10
  /**
11
11
  * Error indicating a failed HTTP request.
12
12
  * @class HTTPRequestFailedError
13
- * @extends HTTPEngineError
13
+ * @extends HTTPStorageEngineError
14
14
  *
15
15
  * @param {string} url - The URL of the failed request.
16
16
  * @param {Object} options - The options used in the fetch request.
17
17
  * @param {Response} response - The HTTP response object.
18
18
  */
19
- export class HTTPRequestFailedError extends HTTPEngineError {
19
+ export class HTTPRequestFailedError extends HTTPStorageEngineError {
20
20
  constructor(url, options, response) {
21
21
  const method = options.method?.toLowerCase() || 'get';
22
22
  super(`Failed to ${method} ${url}`);
@@ -27,13 +27,13 @@ export class HTTPRequestFailedError extends HTTPEngineError {
27
27
  }
28
28
 
29
29
  /**
30
- * HTTPEngine is an extension of the Engine class that provides methods for interacting with HTTP-based APIs.
30
+ * HTTPStorageEngine is an extension of the StorageEngine class that provides methods for interacting with HTTP-based APIs.
31
31
  * It uses the Fetch API for sending and receiving data.
32
32
  *
33
- * @class HTTPEngine
34
- * @extends Engine
33
+ * @class HTTPStorageEngine
34
+ * @extends StorageEngine
35
35
  */
36
- class HTTPEngine extends Engine {
36
+ class HTTPStorageEngine extends StorageEngine {
37
37
 
38
38
  /**
39
39
  * Configures the HTTP engine with additional fetch options.
@@ -186,6 +186,12 @@ class HTTPEngine extends Engine {
186
186
  * @throws {HTTPRequestFailedError} Thrown if the PUT request fails.
187
187
  */
188
188
  static async putIndex(index) {
189
+ /**
190
+ * Process an index of models
191
+ * @param {string} location
192
+ * @param {Array<Model>} models
193
+ * @return {Promise<void>}
194
+ */
189
195
  const processIndex = async (location, models) => {
190
196
  const url = new URL([
191
197
  this.configuration.host,
@@ -315,4 +321,4 @@ class HTTPEngine extends Engine {
315
321
  }
316
322
  }
317
323
 
318
- export default HTTPEngine;
324
+ export default HTTPStorageEngine;
@@ -1,28 +1,28 @@
1
1
  import {DeleteObjectCommand, GetObjectCommand, PutObjectCommand} from '@aws-sdk/client-s3';
2
- import Engine, {EngineError, MissConfiguredError} from './Engine.js';
2
+ import StorageEngine, {EngineError, MissConfiguredError} from './StorageEngine.js';
3
3
 
4
4
  /**
5
5
  * Represents an error specific to the S3 engine operations.
6
- * @class S3EngineError
6
+ * @class S3StorageEngineError
7
7
  * @extends EngineError
8
8
  */
9
- class S3EngineError extends EngineError {}
9
+ class S3StorageEngineError extends EngineError {}
10
10
 
11
11
  /**
12
12
  * Error indicating a failure when putting an object to S3.
13
- * @class FailedPutS3EngineError
14
- * @extends S3EngineError
13
+ * @class FailedPutS3StorageEngineError
14
+ * @extends S3StorageEngineError
15
15
  */
16
- class FailedPutS3EngineError extends S3EngineError {}
16
+ class FailedPutS3StorageEngineError extends S3StorageEngineError {}
17
17
 
18
18
  /**
19
- * S3Engine is an extension of the Engine class that provides methods for interacting with AWS S3.
19
+ * S3StorageEngine is an extension of the StorageEngine class that provides methods for interacting with AWS S3.
20
20
  * It allows for storing, retrieving, and managing model data in an S3 bucket.
21
21
  *
22
- * @class S3Engine
23
- * @extends Engine
22
+ * @class S3StorageEngine
23
+ * @extends StorageEngine
24
24
  */
25
- class S3Engine extends Engine {
25
+ class S3StorageEngine extends StorageEngine {
26
26
  /**
27
27
  * Configures the S3 engine with additional options.
28
28
  *
@@ -92,7 +92,7 @@ class S3Engine extends Engine {
92
92
  * @param {Model} model - The model object to upload.
93
93
  * @returns {Promise<void>}
94
94
  *
95
- * @throws {FailedPutS3EngineError} Thrown if there is an error during the S3 PutObject operation.
95
+ * @throws {FailedPutS3StorageEngineError} Thrown if there is an error during the S3 PutObject operation.
96
96
  */
97
97
  static async putModel(model) {
98
98
  const Key = [this.configuration.prefix, `${model.id}.json`].join('/');
@@ -105,7 +105,7 @@ class S3Engine extends Engine {
105
105
  ContentType: 'application/json',
106
106
  }));
107
107
  } catch (error) {
108
- throw new FailedPutS3EngineError(`Failed to put s3://${this.configuration.bucket}/${Key}`, error);
108
+ throw new FailedPutS3StorageEngineError(`Failed to put s3://${this.configuration.bucket}/${Key}`, error);
109
109
  }
110
110
  }
111
111
 
@@ -133,9 +133,16 @@ class S3Engine extends Engine {
133
133
  *
134
134
  * @param {Object} index - An object where keys are locations and values are key value pairs of models and their ids.
135
135
  * @returns {Promise<void>}
136
- * @throws {FailedPutS3EngineError} Thrown if there is an error during the S3 PutObject operation.
136
+ * @throws {FailedPutS3StorageEngineError} Thrown if there is an error during the S3 PutObject operation.
137
137
  */
138
138
  static async putIndex(index) {
139
+ /**
140
+ * Process an index of models
141
+ * @param {string} location
142
+ * @param {Array<Model>} models
143
+ * @throws FailedPutS3StorageEngineError
144
+ * @return {Promise<void>}
145
+ */
139
146
  const processIndex = async (location, models) => {
140
147
  const Key = [this.configuration.prefix, location, '_index.json'].filter(e => Boolean(e)).join('/');
141
148
  const currentIndex = await this.getIndex(location);
@@ -153,7 +160,7 @@ class S3Engine extends Engine {
153
160
  }),
154
161
  }));
155
162
  } catch (error) {
156
- throw new FailedPutS3EngineError(`Failed to put s3://${this.configuration.bucket}/${Key}`, error);
163
+ throw new FailedPutS3StorageEngineError(`Failed to put s3://${this.configuration.bucket}/${Key}`, error);
157
164
  }
158
165
  };
159
166
 
@@ -205,7 +212,7 @@ class S3Engine extends Engine {
205
212
  * @param {Object} compiledIndex - The compiled search index data.
206
213
  * @returns {Promise<void>}
207
214
  *
208
- * @throws {FailedPutS3EngineError} Thrown if there is an error during the S3 PutObject operation.
215
+ * @throws {FailedPutS3StorageEngineError} Thrown if there is an error during the S3 PutObject operation.
209
216
  */
210
217
  static async putSearchIndexCompiled(model, compiledIndex) {
211
218
  const Key = [this.configuration.prefix, model.toString(), '_search_index.json'].join('/');
@@ -218,7 +225,7 @@ class S3Engine extends Engine {
218
225
  ContentType: 'application/json',
219
226
  }));
220
227
  } catch (error) {
221
- throw new FailedPutS3EngineError(`Failed to put s3://${this.configuration.bucket}/${Key}`, error);
228
+ throw new FailedPutS3StorageEngineError(`Failed to put s3://${this.configuration.bucket}/${Key}`, error);
222
229
  }
223
230
  }
224
231
 
@@ -229,7 +236,7 @@ class S3Engine extends Engine {
229
236
  * @param {Object} rawIndex - The raw search index data.
230
237
  * @returns {Promise<void>}
231
238
  *
232
- * @throws {FailedPutS3EngineError} Thrown if there is an error during the S3 PutObject operation.
239
+ * @throws {FailedPutS3StorageEngineError} Thrown if there is an error during the S3 PutObject operation.
233
240
  */
234
241
  static async putSearchIndexRaw(model, rawIndex) {
235
242
  const Key = [this.configuration.prefix, model.toString(), '_search_index_raw.json'].join('/');
@@ -242,9 +249,9 @@ class S3Engine extends Engine {
242
249
  ContentType: 'application/json',
243
250
  }));
244
251
  } catch (error) {
245
- throw new FailedPutS3EngineError(`Failed to put s3://${this.configuration.bucket}/${Key}`, error);
252
+ throw new FailedPutS3StorageEngineError(`Failed to put s3://${this.configuration.bucket}/${Key}`, error);
246
253
  }
247
254
  }
248
255
  }
249
256
 
250
- export default S3Engine;
257
+ export default S3StorageEngine;
@@ -1,14 +1,14 @@
1
- import Query from '../Query.js';
2
- import Type from '../type/index.js';
1
+ import Query from '../../Query.js';
2
+ import Type from '../../type/index.js';
3
3
  import lunr from 'lunr';
4
4
 
5
5
  /**
6
- * The `Engine` class provides a base interface for implementing data storage and retrieval engines.
6
+ * The `StorageEngine` class provides a base interface for implementing data storage and retrieval engines.
7
7
  * It includes methods for handling models, indexes, and search functionality.
8
8
  *
9
- * @class Engine
9
+ * @class StorageEngine
10
10
  */
11
- class Engine {
11
+ class StorageEngine {
12
12
  static configuration = undefined;
13
13
  static _searchCache = undefined;
14
14
 
@@ -186,6 +186,11 @@ class Engine {
186
186
  const uploadedModels = [];
187
187
  const indexUpdates = {};
188
188
 
189
+ /**
190
+ * Process a model, putting updates to the model and all linked models.
191
+ * @param {Model} m
192
+ * @return {Promise<void>}
193
+ */
189
194
  const processModel = async (m) => {
190
195
  if (!uploadedModels.includes(m.id)) {
191
196
  m.validate();
@@ -389,6 +394,11 @@ class Engine {
389
394
  this.checkConfiguration();
390
395
  const hydratedModels = {};
391
396
 
397
+ /**
398
+ * Hydrate a model
399
+ * @param {Model} modelToProcess
400
+ * @return {Promise<Model>}
401
+ */
392
402
  const hydrateModel = async (modelToProcess) => {
393
403
  hydratedModels[modelToProcess.id] = modelToProcess;
394
404
 
@@ -405,6 +415,13 @@ class Engine {
405
415
  return modelToProcess;
406
416
  };
407
417
 
418
+ /**
419
+ * Hydrate a dry sub model
420
+ * @param property
421
+ * @param modelToProcess
422
+ * @param name
423
+ * @return {Promise<Model>}
424
+ */
408
425
  const hydrateSubModel = async (property, modelToProcess, name) => {
409
426
  if (hydratedModels[property.id]) {
410
427
  return hydratedModels[property.id];
@@ -418,6 +435,13 @@ class Engine {
418
435
  return hydratedSubModel;
419
436
  };
420
437
 
438
+ /**
439
+ * Hydrate an array of dry models
440
+ * @param property
441
+ * @param modelToProcess
442
+ * @param name
443
+ * @return {Promise<Awaited<*>[]>}
444
+ */
421
445
  const hydrateModelList = async (property, modelToProcess, name) => {
422
446
  const subModelClass = getSubModelClass(modelToProcess, name, true);
423
447
 
@@ -440,6 +464,13 @@ class Engine {
440
464
  }));
441
465
  };
442
466
 
467
+ /**
468
+ * Get the class of a sub model
469
+ * @param modelToProcess
470
+ * @param name
471
+ * @param isArray
472
+ * @return {Model.constructor|Type}
473
+ */
443
474
  function getSubModelClass(modelToProcess, name, isArray = false) {
444
475
  const constructorField = modelToProcess.constructor[name];
445
476
 
@@ -457,9 +488,13 @@ class Engine {
457
488
  * Configures the engine with specific settings.
458
489
  *
459
490
  * @param {Object} configuration - The configuration settings for the engine.
460
- * @returns {Engine} A new engine instance with the applied configuration.
491
+ * @returns {StorageEngine} A new engine instance with the applied configuration.
461
492
  */
462
493
  static configure(configuration) {
494
+ /**
495
+ * @class ConfiguredStore
496
+ * @extends StorageEngine
497
+ */
463
498
  class ConfiguredStore extends this {
464
499
  static configuration = configuration;
465
500
  }
@@ -476,7 +511,7 @@ class Engine {
476
511
  * @abstract
477
512
  */
478
513
  static checkConfiguration() {
479
- // Implemented in extending Engine class
514
+ // Implemented in extending StorageEngine class
480
515
  }
481
516
 
482
517
  /**
@@ -568,9 +603,9 @@ export class MissConfiguredError extends EngineError {
568
603
  * @param {Object} configuration - The configuration object that caused the misconfiguration.
569
604
  */
570
605
  constructor(configuration) {
571
- super('Engine is miss-configured');
606
+ super('StorageEngine is miss-configured');
572
607
  this.configuration = configuration;
573
608
  }
574
609
  }
575
610
 
576
- export default Engine;
611
+ export default StorageEngine;
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(([_name, type]) =>
202
+ this.isModel(
203
+ typeof type === 'function' &&
204
+ !/^class/.test(Function.prototype.toString.call(type)) ?
205
+ type() : type,
206
+ ),
207
+ )
208
+ .map(([name, _type]) => `${name}.id`),
209
+ )
210
+ .concat(
211
+ Object.entries(this)
212
+ .filter(([_name, 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, _type]) => `${name}.[*].id`),
219
+ )
220
+ .concat(this.indexedProperties());
221
+ }
222
+
190
223
  /**
191
224
  * Returns a list of properties used for search.
192
225
  *
package/src/type/index.js CHANGED
@@ -15,23 +15,18 @@ import StringType from './simple/StringType.js';
15
15
  * @property {DateType} Date
16
16
  * @property {ArrayType} Array
17
17
  * @property {CustomType} Custom
18
- * @property {ResolvedType} Resolved
18
+ * @property {{Slug: SlugType}} Resolved
19
19
  * @property {Model} Model
20
20
  */
21
- const Type = {};
22
-
23
- Type.String = StringType;
24
- Type.Number = NumberType;
25
- Type.Boolean = BooleanType;
26
- Type.Date = DateType;
27
- Type.Array = ArrayType;
28
- Type.Custom = CustomType;
29
-
30
- /**
31
- * @class ResolvedType
32
- * @property {SlugType} Slug
33
- */
34
- Type.Resolved = {Slug: SlugType};
35
- Type.Model = Model;
21
+ class Type {
22
+ static Model = Model;
23
+ static String = StringType;
24
+ static Number = NumberType;
25
+ static Boolean = BooleanType;
26
+ static Date = DateType;
27
+ static Array = ArrayType;
28
+ static Custom = CustomType;
29
+ static Resolved = {Slug: SlugType};
30
+ }
36
31
 
37
32
  export default Type;
@@ -1,15 +1,15 @@
1
- import SimpleType from './SimpleType.js';
1
+ import Type from '../Type.js';
2
2
 
3
3
  /**
4
4
  * Class representing a boolean type.
5
5
  *
6
6
  * This class is used to define and handle data of the boolean type.
7
- * It extends the {@link SimpleType} class to represent string-specific behavior.
7
+ * It extends the {@link Type} class to represent string-specific behavior.
8
8
  *
9
9
  * @class BooleanType
10
- * @extends SimpleType
10
+ * @extends Type
11
11
  */
12
- class BooleanType extends SimpleType {
12
+ class BooleanType extends Type {
13
13
  static {
14
14
  /**
15
15
  * @static
@@ -1,15 +1,15 @@
1
- import SimpleType from './SimpleType.js';
1
+ import Type from '../Type.js';
2
2
 
3
3
  /**
4
4
  * Class representing a date type with ISO date-time format.
5
5
  *
6
6
  * This class is used to define and handle data of the date type.
7
- * It extends the {@link SimpleType} class to represent string-specific behavior.
7
+ * It extends the {@link Type} class to represent string-specific behavior.
8
8
  *
9
9
  * @class DateType
10
- * @extends SimpleType
10
+ * @extends Type
11
11
  */
12
- class DateType extends SimpleType {
12
+ class DateType extends Type {
13
13
  static {
14
14
  /**
15
15
  * @static
@@ -1,15 +1,15 @@
1
- import SimpleType from './SimpleType.js';
1
+ import Type from '../Type.js';
2
2
 
3
3
  /**
4
4
  * Class representing a number type.
5
5
  *
6
6
  * This class is used to define and handle data of the number type.
7
- * It extends the {@link SimpleType} class to represent string-specific behavior.
7
+ * It extends the {@link Type} class to represent string-specific behavior.
8
8
  *
9
9
  * @class NumberType
10
- * @extends SimpleType
10
+ * @extends Type
11
11
  */
12
- class NumberType extends SimpleType {
12
+ class NumberType extends Type {
13
13
  static {
14
14
  /**
15
15
  * @static
@@ -1,15 +1,15 @@
1
- import SimpleType from './SimpleType.js';
1
+ import Type from '../Type.js';
2
2
 
3
3
  /**
4
4
  * Class representing a string type.
5
5
  *
6
6
  * This class is used to define and handle data of the string type.
7
- * It extends the {@link SimpleType} class to represent string-specific behavior.
7
+ * It extends the {@link Type} class to represent string-specific behavior.
8
8
  *
9
9
  * @class StringType
10
- * @extends SimpleType
10
+ * @extends Type
11
11
  */
12
- class StringType extends SimpleType {
12
+ class StringType extends Type {
13
13
  static {
14
14
  /**
15
15
  * @static
@@ -1,3 +0,0 @@
1
- import FileEngine from '../../src/engine/FileEngine.js';
2
-
3
- export default FileEngine;
@@ -1,3 +0,0 @@
1
- import HTTPEngine from '../../src/engine/HTTPEngine.js';
2
-
3
- export default HTTPEngine;
@@ -1,3 +0,0 @@
1
- import S3Engine from '../../src/engine/S3Engine.js';
2
-
3
- export default S3Engine;
@@ -1,14 +0,0 @@
1
- import Type from '../Type.js';
2
-
3
- /**
4
- * Class representing a simple type.
5
- *
6
- * This serves as a base class for primitive or simple types such as string, number, or boolean.
7
- *
8
- * @class SimpleType
9
- * @extends Type
10
- */
11
- class SimpleType extends Type {
12
- }
13
-
14
- export default SimpleType;