@acodeninja/persist 3.0.0-next.8 → 3.0.0

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 (41) hide show
  1. package/README.md +60 -4
  2. package/docs/code-quirks.md +14 -14
  3. package/docs/defining-models.md +61 -0
  4. package/docs/http.openapi.yml +138 -0
  5. package/docs/{model-property-types.md → model-properties.md} +76 -43
  6. package/docs/models-as-properties.md +46 -46
  7. package/docs/search-queries.md +11 -13
  8. package/docs/storage-engines.md +19 -35
  9. package/docs/structured-queries.md +59 -48
  10. package/docs/transactions.md +6 -7
  11. package/exports/storage/http.js +3 -0
  12. package/exports/storage/s3.js +3 -0
  13. package/jest.config.cjs +8 -12
  14. package/package.json +2 -2
  15. package/src/Connection.js +750 -0
  16. package/src/Persist.js +29 -30
  17. package/src/Schema.js +175 -0
  18. package/src/{Query.js → data/FindIndex.js} +40 -24
  19. package/src/{type → data}/Model.js +95 -55
  20. package/src/data/Property.js +21 -0
  21. package/src/data/SearchIndex.js +106 -0
  22. package/src/{type/complex → data/properties}/ArrayType.js +5 -3
  23. package/src/{type/simple → data/properties}/BooleanType.js +3 -3
  24. package/src/{type/complex → data/properties}/CustomType.js +5 -5
  25. package/src/{type/simple → data/properties}/DateType.js +4 -4
  26. package/src/{type/simple → data/properties}/NumberType.js +3 -3
  27. package/src/{type/resolved → data/properties}/ResolvedType.js +3 -2
  28. package/src/{type/resolved → data/properties}/SlugType.js +1 -1
  29. package/src/{type/simple → data/properties}/StringType.js +3 -3
  30. package/src/{type → data/properties}/Type.js +13 -3
  31. package/src/engine/storage/HTTPStorageEngine.js +149 -253
  32. package/src/engine/storage/S3StorageEngine.js +108 -195
  33. package/src/engine/storage/StorageEngine.js +131 -549
  34. package/exports/engine/storage/file.js +0 -3
  35. package/exports/engine/storage/http.js +0 -3
  36. package/exports/engine/storage/s3.js +0 -3
  37. package/src/SchemaCompiler.js +0 -196
  38. package/src/Transactions.js +0 -145
  39. package/src/engine/StorageEngine.js +0 -350
  40. package/src/engine/storage/FileStorageEngine.js +0 -213
  41. package/src/type/index.js +0 -32
@@ -1,3 +0,0 @@
1
- import FileStorageEngine from '../../../src/engine/storage/FileStorageEngine.js';
2
-
3
- export default FileStorageEngine;
@@ -1,3 +0,0 @@
1
- import HTTPStorageEngine from '../../../src/engine/storage/HTTPStorageEngine.js';
2
-
3
- export default HTTPStorageEngine;
@@ -1,3 +0,0 @@
1
- import S3StorageEngine from '../../../src/engine/storage/S3StorageEngine.js';
2
-
3
- export default S3StorageEngine;
@@ -1,196 +0,0 @@
1
- import Type from './type/index.js';
2
- import ajv from 'ajv';
3
- import ajvErrors from 'ajv-errors';
4
- import ajvFormats from 'ajv-formats';
5
-
6
- /**
7
- * A class responsible for compiling raw schema definitions into a format that can be validated using the AJV (Another JSON Validator) library.
8
- */
9
- class SchemaCompiler {
10
- /**
11
- * Compiles a raw schema into a validation-ready schema, and returns a class that extends `CompiledSchema`.
12
- *
13
- * This method converts a given schema into a JSON schema-like format, setting up properties, types, formats, and validation rules.
14
- * It uses AJV for the validation process and integrates with model types and their specific validation rules.
15
- *
16
- * @param {Object|Model} rawSchema - The raw schema or model definition to be compiled.
17
- * @returns {CompiledSchema} - A class that extends `CompiledSchema`, with the compiled schema and validator attached.
18
- *
19
- * @example
20
- * const schemaClass = SchemaCompiler.compile(MyModelSchema);
21
- * const isValid = schemaClass.validate(data); // Throws ValidationError if data is invalid.
22
- */
23
- static compile(rawSchema) {
24
- const validation = new ajv({allErrors: true});
25
-
26
- ajvErrors(validation);
27
- ajvFormats(validation);
28
-
29
- const schema = {
30
- type: 'object',
31
- additionalProperties: false,
32
- properties: {},
33
- required: [],
34
- };
35
-
36
- if (Type.Model.isModel(rawSchema)) {
37
- schema.required.push('id');
38
- schema.properties.id = {type: 'string'};
39
- }
40
-
41
- for (const [name, type] of Object.entries(rawSchema)) {
42
- if (['indexedProperties', 'searchProperties'].includes(name)) continue;
43
-
44
- const property = type instanceof Function && !type.prototype ? type() : type;
45
-
46
- if (property?._required || property?._items?._type?._required)
47
- schema.required.push(name);
48
-
49
- if (Type.Model.isModel(property)) {
50
- schema.properties[name] = {
51
- type: 'object',
52
- additionalProperties: false,
53
- required: ['id'],
54
- properties: {
55
- id: {
56
- type: 'string',
57
- pattern: `^${property.toString()}/[A-Z0-9]+$`,
58
- },
59
- },
60
- };
61
- continue;
62
- }
63
-
64
- if (property?._schema) {
65
- schema.properties[name] = property._schema;
66
- continue;
67
- }
68
-
69
- schema.properties[name] = {type: property?._type};
70
-
71
- if (property?._format) {
72
- schema.properties[name].format = property._format;
73
- }
74
-
75
- if (property?._type === 'array') {
76
- schema.properties[name].items = {type: property?._items._type};
77
-
78
- if (property?._items?._format) {
79
- schema.properties[name].items.format = property?._items._format;
80
- }
81
-
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)) {
87
- schema.properties[name].items = {
88
- type: 'object',
89
- additionalProperties: false,
90
- required: ['id'],
91
- properties: {
92
- id: {
93
- type: 'string',
94
- pattern: `^${prop.toString()}/[A-Z0-9]+$`,
95
- },
96
- },
97
- };
98
- }
99
- }
100
- }
101
-
102
- class Schema extends CompiledSchema {
103
- /**
104
- * The compiled schema definition.
105
- * @type {Object}
106
- * @static
107
- * @private
108
- */
109
- static _schema = schema;
110
-
111
- /**
112
- * The AJV validator function compiled from the schema.
113
- * @type {Function}
114
- * @static
115
- * @private
116
- */
117
- static _validator = validation.compile(schema);
118
- }
119
-
120
- return Schema;
121
- }
122
- }
123
-
124
-
125
- /**
126
- * Represents a compiled schema used for validating data models.
127
- * This class provides a mechanism to validate data using a precompiled schema and a validator function.
128
- */
129
- export class CompiledSchema {
130
- /**
131
- * The schema definition for validation, typically a precompiled JSON schema or similar.
132
- * @type {?Object}
133
- * @static
134
- * @private
135
- */
136
- static _schema = null;
137
-
138
- /**
139
- * The validator function used to validate data against the schema.
140
- * @type {?Function}
141
- * @static
142
- * @private
143
- */
144
- static _validator = null;
145
-
146
- /**
147
- * Validates the given data against the compiled schema.
148
- *
149
- * If the data is an instance of a model, it will be converted to a plain object via `toData()` before validation.
150
- *
151
- * @param {Object|Model} data - The data or model instance to be validated.
152
- * @returns {boolean} - Returns `true` if the data is valid according to the schema.
153
- * @throws {ValidationError} - Throws a `ValidationError` if the data is invalid.
154
- */
155
- static validate(data) {
156
- let inputData = Object.assign({}, data);
157
-
158
- if (Type.Model.isModel(data)) {
159
- inputData = data.toData();
160
- }
161
-
162
- const valid = this._validator?.(inputData);
163
-
164
- if (valid) return valid;
165
-
166
- throw new ValidationError(inputData, this._validator.errors);
167
- }
168
- }
169
-
170
- /**
171
- * Represents a validation error that occurs when a model or data fails validation.
172
- * Extends the built-in JavaScript `Error` class.
173
- */
174
- export class ValidationError extends Error {
175
- /**
176
- * Creates an instance of `ValidationError`.
177
- *
178
- * @param {Object} data - The data that failed validation.
179
- * @param {Array<Object>} errors - A list of validation errors, each typically containing details about what failed.
180
- */
181
- constructor(data, errors) {
182
- super('Validation failed');
183
- /**
184
- * An array of validation errors, containing details about each failed validation.
185
- * @type {Array<Object>}
186
- */
187
- this.errors = errors;
188
- /**
189
- * The data that caused the validation error.
190
- * @type {Object}
191
- */
192
- this.data = data;
193
- }
194
- }
195
-
196
- export default SchemaCompiler;
@@ -1,145 +0,0 @@
1
- /**
2
- * Class representing a transaction-related error.
3
- *
4
- * @class TransactionError
5
- * @extends Error
6
- */
7
- class TransactionError extends Error {
8
- }
9
-
10
- /**
11
- * Error thrown when a transaction is already committed.
12
- *
13
- * @class TransactionCommittedError
14
- * @extends TransactionError
15
- */
16
- export class TransactionCommittedError extends TransactionError {
17
- /**
18
- * Creates an instance of TransactionCommittedError.
19
- * This error is thrown when attempting to commit an already committed transaction.
20
- * @property {string} message - The error message.
21
- */
22
- message = 'Transaction was already committed.';
23
- }
24
-
25
- /**
26
- * Enables transaction support for the provided engine.
27
- *
28
- * This function enhances an engine class with transaction capabilities, allowing multiple
29
- * changes to be grouped into a single transaction that can be committed or rolled back.
30
- *
31
- * @param {Engine.constructor} engine - The base engine class to be enhanced with transaction support.
32
- * @returns {TransactionalEngine.constructor} TransactionalEngine - The enhanced engine class with transaction functionality.
33
- */
34
- export default function enableTransactions(engine) {
35
- /**
36
- * A class representing an engine with transaction capabilities.
37
- * @class TransactionalEngine
38
- * @extends {engine}
39
- */
40
- class TransactionalEngine extends engine {
41
- }
42
-
43
- /**
44
- * Starts a transaction on the engine. Returns a Transaction class that can handle
45
- * put, commit, and rollback actions for the transaction.
46
- *
47
- * @returns {Transaction.constructor} Transaction - A class that manages the transaction's operations.
48
- */
49
- TransactionalEngine.start = () => {
50
- /**
51
- * A class representing an active transaction on the engine.
52
- * Contains methods to put changes, commit the transaction, or roll back in case of failure.
53
- *
54
- * @class Transaction
55
- */
56
- class Transaction extends TransactionalEngine {
57
- /**
58
- * @property {Array<Object>} transactions - An array storing all the operations within the transaction.
59
- * @static
60
- */
61
- static transactions = [];
62
-
63
- /**
64
- * @property {boolean} committed - Indicates if the transaction has been committed.
65
- * @static
66
- */
67
- static committed = false;
68
-
69
- /**
70
- * @property {boolean} failed - Indicates if the transaction has failed.
71
- * @static
72
- */
73
- static failed = false;
74
-
75
- /**
76
- * Adds a model to the transaction queue.
77
- *
78
- * @param {Object} model - The model to be added to the transaction.
79
- * @returns {Promise<void>} A promise that resolves once the model is added.
80
- */
81
- static put(model) {
82
- this.transactions.push({
83
- hasRun: false,
84
- hasRolledBack: false,
85
- model,
86
- });
87
-
88
- return Promise.resolve();
89
- }
90
-
91
- /**
92
- * Checks if the transaction has already been committed. If true, throws a TransactionCommittedError.
93
- *
94
- * @throws {TransactionCommittedError} If the transaction has already been committed.
95
- * @private
96
- */
97
- static _checkCommitted() {
98
- if (this.committed) throw new TransactionCommittedError();
99
- }
100
-
101
- /**
102
- * Commits the transaction, applying all the changes to the engine.
103
- * Rolls back if any part of the transaction fails.
104
- *
105
- * @returns {Promise<void>} A promise that resolves once the transaction is committed, or rejects if an error occurs.
106
- * @throws {Error} If any operation in the transaction fails.
107
- */
108
- static async commit() {
109
- this._checkCommitted();
110
-
111
- try {
112
- for (const [index, {model}] of this.transactions.entries()) {
113
- try {
114
- this.transactions[index].original = await engine.get(model.constructor, model.id);
115
- } catch (_error) {
116
- this.transactions[index].original = null;
117
- }
118
-
119
- await engine.put(model);
120
- this.transactions[index].hasRun = true;
121
- }
122
- } catch (e) {
123
- this.committed = true;
124
- this.failed = true;
125
- for (const [index, {original}] of this.transactions.entries()) {
126
- if (original) {
127
- await engine.put(this.transactions[index].original);
128
- }
129
- this.transactions[index].hasRolledBack = true;
130
- }
131
- throw e;
132
- }
133
-
134
- this.committed = true;
135
- this.failed = false;
136
- }
137
- }
138
-
139
- return Transaction;
140
- };
141
-
142
- Object.defineProperty(TransactionalEngine, 'name', {value: `${engine.toString()}`});
143
-
144
- return TransactionalEngine;
145
- }
@@ -1,350 +0,0 @@
1
- import Type from '../type/index.js';
2
- import _ from 'lodash';
3
-
4
- export default class StorageEngine {
5
- /**
6
- * @param {Object} configuration
7
- * @param {Array<Type.Model.constructor>?} models
8
- */
9
- constructor(configuration = {}, models = null) {
10
- this.configuration = configuration;
11
- this.models = Object.fromEntries((models ?? []).map(model => [model.name, model]));
12
- }
13
-
14
- /**
15
- * Persists a model if it has changed, and updates all related models and their indexes
16
- * @param {Type.Model} model
17
- * @return {Promise<void>}
18
- */
19
- async put(model) {
20
- const processedModels = [];
21
- const modelsToPut = [];
22
- const modelsToReindex = {};
23
- const modelsToReindexSearch = {};
24
-
25
- /**
26
- * @param {Type.Model} modelToProcess
27
- * @return {Promise<void>}
28
- */
29
- const processModel = async (modelToProcess) => {
30
- if (processedModels.includes(modelToProcess.id))
31
- return;
32
-
33
- processedModels.push(modelToProcess.id);
34
-
35
- if (!Object.keys(this.models).includes(modelToProcess.constructor.name))
36
- throw new ModelNotRegisteredStorageEngineError(modelToProcess, this);
37
-
38
- modelToProcess.validate();
39
- const currentModel = await this.get(modelToProcess.id).catch(() => null);
40
-
41
- const modelToProcessHasChanged = JSON.stringify(currentModel?.toData() || {}) !== JSON.stringify(modelToProcess.toData());
42
-
43
- if (modelToProcessHasChanged) modelsToPut.push(modelToProcess);
44
-
45
- if (
46
- Boolean(modelToProcess.constructor.indexedProperties().length) &&
47
- indexedFieldsHaveChanged(currentModel, modelToProcess)
48
- ) {
49
- const modelToProcessConstructor = this.getModelConstructorFromId(modelToProcess.id);
50
- modelsToReindex[modelToProcessConstructor] = modelsToReindex[modelToProcessConstructor] || [];
51
- modelsToReindex[modelToProcessConstructor].push(modelToProcess);
52
- }
53
-
54
- if (
55
- Boolean(modelToProcess.constructor.searchProperties().length) &&
56
- searchableFieldsHaveChanged(currentModel, modelToProcess)
57
- ) {
58
- const modelToProcessConstructor = this.getModelConstructorFromId(modelToProcess.id);
59
- modelsToReindexSearch[modelToProcessConstructor] = modelsToReindexSearch[modelToProcessConstructor] || [];
60
- modelsToReindexSearch[modelToProcessConstructor].push(modelToProcess);
61
- }
62
-
63
- for (const [field, value] of Object.entries(modelToProcess)) {
64
- if (Type.Model.isModel(value)) {
65
- await processModel(modelToProcess[field]);
66
- }
67
- }
68
- };
69
-
70
- await processModel(model);
71
-
72
- await Promise.all([
73
- Promise.all(modelsToPut.map(m => this._putModel(m.toData()))),
74
- Promise.all(Object.entries(modelsToReindex).map(async ([constructorName, models]) => {
75
- const modelConstructor = this.models[constructorName];
76
- const index = await this._getIndex(modelConstructor);
77
-
78
- await this._putIndex(modelConstructor, {
79
- ...index || {},
80
- ...Object.fromEntries(models.map(m => [m.id, m.toIndexData()])),
81
- });
82
- })),
83
- Promise.all(Object.entries(modelsToReindexSearch).map(async ([constructorName, models]) => {
84
- const modelConstructor = this.models[constructorName];
85
- const index = await this._getSearchIndex(modelConstructor);
86
-
87
- await this._putSearchIndex(modelConstructor, {
88
- ...index || {},
89
- ...Object.fromEntries(models.map(m => [m.id, m.toSearchData()])),
90
- });
91
- })),
92
- ]);
93
- }
94
-
95
- /**
96
- * Get a model by its id
97
- * @param {string} modelId
98
- * @throws {ModelNotFoundStorageEngineError}
99
- * @return {Promise<Type.Model>}
100
- */
101
- get(modelId) {
102
- try {
103
- this.getModelConstructorFromId(modelId);
104
- } catch (e) {
105
- return Promise.reject(e);
106
- }
107
- return this._getModel(modelId);
108
- }
109
-
110
- /**
111
- *
112
- * @param {Type.Model} model
113
- * @throws {ModelNotRegisteredStorageEngineError}
114
- * @throws {ModelNotFoundStorageEngineError}
115
- */
116
- async delete(model) {
117
- if (!Object.keys(this.models).includes(model.constructor.name))
118
- throw new ModelNotRegisteredStorageEngineError(model, this);
119
-
120
- const currentModel = await this.get(model.id);
121
-
122
- await this._deleteModel(currentModel.id);
123
-
124
- const currentIndex = this._getIndex(currentModel.constructor);
125
-
126
- await this._putIndex(currentModel.constructor, _.omit(currentIndex, [currentModel.id]));
127
-
128
- const currentSearchIndex = this._getSearchIndex(currentModel.constructor);
129
-
130
- await this._putSearchIndex(currentModel.constructor, _.omit(currentSearchIndex, [currentModel.id]));
131
- }
132
-
133
- /**
134
- * Get the model constructor from a model id
135
- * @param {string} modelId
136
- * @return {Model.constructor}
137
- */
138
- getModelConstructorFromId(modelId) {
139
- const modelName = modelId.split('/')[0];
140
- const constructor = this.models[modelName];
141
-
142
- if (!constructor) throw new ModelNotRegisteredStorageEngineError(modelName, this);
143
-
144
- return constructor;
145
- }
146
-
147
- /**
148
- * Get model classes that are directly linked to the given model in either direction
149
- * @param {Type.Model.constructor} model
150
- * @return {Record<string, Record<string, Type.Model.constructor>>}
151
- */
152
- getLinksFor(model) {
153
- return Object.fromEntries(
154
- Object.entries(this.getAllModelLinks())
155
- .filter(([modelName, links]) =>
156
- model.name === modelName ||
157
- Object.values(links).some((link) => link.name === model.name),
158
- ),
159
- );
160
- }
161
-
162
- /**
163
- * Get all model links
164
- * @return {Record<string, Record<string, Type.Model.constructor>>}
165
- */
166
- getAllModelLinks() {
167
- return Object.entries(this.models)
168
- .map(([registeredModelName, registeredModelClass]) =>
169
- Object.entries(registeredModelClass)
170
- .map(([propertyName, propertyType]) => [
171
- registeredModelName,
172
- propertyName,
173
- typeof propertyType === 'function' &&
174
- !/^class/.test(Function.prototype.toString.call(propertyType)) &&
175
- !Type.Model.isModel(propertyType) ?
176
- propertyType() : propertyType,
177
- ])
178
- .filter(([_m, _p, type]) => Type.Model.isModel(type))
179
- .map(([containingModel, propertyName, propertyType]) => ({
180
- containingModel,
181
- propertyName,
182
- propertyType,
183
- })),
184
- )
185
- .flat()
186
- .reduce((accumulator, {containingModel, propertyName, propertyType}) => ({
187
- ...accumulator,
188
- [containingModel]: {
189
- ...accumulator[containingModel] || {},
190
- [propertyName]: propertyType,
191
- },
192
- }), {});
193
- }
194
-
195
- /**
196
- * Update a model
197
- * @param {Model} _model
198
- * @throws MethodNotImplementedStorageEngineError
199
- * @return Promise<void>
200
- */
201
- _putModel(_model) {
202
- return Promise.reject(new MethodNotImplementedStorageEngineError('_putModel', this));
203
- }
204
-
205
- /**
206
- * Get a model
207
- * @param {string} _id
208
- * @throws MethodNotImplementedStorageEngineError
209
- * @throws ModelNotFoundStorageEngineError
210
- * @return Promise<Model>
211
- */
212
- _getModel(_id) {
213
- return Promise.reject(new MethodNotImplementedStorageEngineError('_getModel', this));
214
- }
215
-
216
- /**
217
- * Delete a model
218
- * @param {string} _id
219
- * @throws MethodNotImplementedStorageEngineError
220
- * @throws ModelNotFoundStorageEngineError
221
- * @return Promise<void>
222
- */
223
- _deleteModel(_id) {
224
- return Promise.reject(new MethodNotImplementedStorageEngineError('_deleteModel', this));
225
- }
226
-
227
- /**
228
- * Get a model's index data
229
- * @param {Model.constructor} _modelConstructor
230
- * @throws MethodNotImplementedStorageEngineError
231
- * @return Promise<void>
232
- */
233
- _getIndex(_modelConstructor) {
234
- return Promise.reject(new MethodNotImplementedStorageEngineError('_getIndex', this));
235
- }
236
-
237
- /**
238
- * Put a model's index data
239
- * @param {Model.constructor} _modelConstructor
240
- * @param {object} _data
241
- * @throws MethodNotImplementedStorageEngineError
242
- * @return Promise<void>
243
- */
244
- _putIndex(_modelConstructor, _data) {
245
- return Promise.reject(new MethodNotImplementedStorageEngineError('_putIndex', this));
246
- }
247
-
248
- /**
249
- * Get a model's raw search index data
250
- * @param {Model.constructor} _modelConstructor
251
- * @throws MethodNotImplementedStorageEngineError
252
- * @return Promise<void>
253
- */
254
- _getSearchIndex(_modelConstructor) {
255
- return Promise.reject(new MethodNotImplementedStorageEngineError('_getSearchIndex', this));
256
- }
257
-
258
- /**
259
- * Get a model's raw search index data
260
- * @param {Model.constructor} _modelConstructor
261
- * @throws MethodNotImplementedStorageEngineError
262
- * @return Promise<void>
263
- */
264
- _getSearchIndexCompiled(_modelConstructor) {
265
- return Promise.reject(new MethodNotImplementedStorageEngineError('_getSearchIndexCompiled', this));
266
- }
267
-
268
- /**
269
- * Put a model's raw and compiled search index data
270
- * @param {Model.constructor} _modelConstructor
271
- * @param {object} _data
272
- * @throws MethodNotImplementedStorageEngineError
273
- * @return Promise<void>
274
- */
275
- _putSearchIndex(_modelConstructor, _data) {
276
- return Promise.reject(new MethodNotImplementedStorageEngineError('_putSearchIndex', this));
277
- }
278
- }
279
-
280
-
281
- /**
282
- * Decide if two models indexable fields are different
283
- * @param {Type.Model} currentModel
284
- * @param {Type.Model} modelToProcess
285
- * @return {boolean}
286
- * @private
287
- */
288
- function indexedFieldsHaveChanged(currentModel, modelToProcess) {
289
- return !currentModel || JSON.stringify(currentModel.toIndexData()) !== JSON.stringify(modelToProcess.toIndexData());
290
- }
291
-
292
- /**
293
- * Decide if two models searchable fields have changed
294
- * @param {Type.Model} currentModel
295
- * @param {Type.Model} modelToProcess
296
- * @return {boolean}
297
- * @private
298
- */
299
- function searchableFieldsHaveChanged(currentModel, modelToProcess) {
300
- return !currentModel || JSON.stringify(currentModel.toSearchData()) !== JSON.stringify(modelToProcess.toSearchData());
301
- }
302
-
303
- /**
304
- * @class StorageEngineError
305
- * @extends Error
306
- */
307
- export class StorageEngineError extends Error {
308
- }
309
-
310
- /**
311
- * @class ModelNotRegisteredStorageEngineError
312
- * @extends StorageEngineError
313
- */
314
- export class ModelNotRegisteredStorageEngineError extends StorageEngineError {
315
- /**
316
- * @param {Type.Model} model
317
- * @param {StorageEngine} storageEngine
318
- */
319
- constructor(model, storageEngine) {
320
- const modelName = typeof model === 'string' ? model : model.constructor.name;
321
- super(`The model ${modelName} is not registered in the storage engine ${storageEngine.constructor.name}`);
322
- }
323
- }
324
-
325
- /**
326
- * @class MethodNotImplementedStorageEngineError
327
- * @extends StorageEngineError
328
- */
329
- export class MethodNotImplementedStorageEngineError extends StorageEngineError {
330
- /**
331
- * @param {string} method
332
- * @param {StorageEngine} storageEngine
333
- */
334
- constructor(method, storageEngine) {
335
- super(`The method ${method} is not implemented in the storage engine ${storageEngine.constructor.name}`);
336
- }
337
- }
338
-
339
- /**
340
- * @class ModelNotFoundStorageEngineError
341
- * @extends StorageEngineError
342
- */
343
- export class ModelNotFoundStorageEngineError extends StorageEngineError {
344
- /**
345
- * @param {string} modelId
346
- */
347
- constructor(modelId) {
348
- super(`The model ${modelId} was not found`);
349
- }
350
- }