@acodeninja/persist 3.0.0-next.2 → 3.0.0-next.21

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 +6 -6
  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} +37 -35
  6. package/docs/models-as-properties.md +40 -40
  7. package/docs/search-queries.md +3 -5
  8. package/docs/storage-engines.md +19 -35
  9. package/docs/structured-queries.md +56 -45
  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 +631 -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 +41 -26
  20. package/src/data/Property.js +19 -0
  21. package/src/data/SearchIndex.js +106 -0
  22. package/src/{type/complex → data/properties}/ArrayType.js +1 -1
  23. package/src/{type/simple → data/properties}/BooleanType.js +1 -1
  24. package/src/{type/complex → data/properties}/CustomType.js +1 -1
  25. package/src/{type/simple → data/properties}/DateType.js +1 -1
  26. package/src/{type/simple → data/properties}/NumberType.js +1 -1
  27. package/src/{type/resolved → data/properties}/ResolvedType.js +3 -2
  28. package/src/{type/simple → data/properties}/StringType.js +1 -1
  29. package/src/{type → data/properties}/Type.js +8 -0
  30. package/src/engine/storage/HTTPStorageEngine.js +149 -253
  31. package/src/engine/storage/S3StorageEngine.js +108 -195
  32. package/src/engine/storage/StorageEngine.js +114 -550
  33. package/exports/engine/storage/file.js +0 -3
  34. package/exports/engine/storage/http.js +0 -3
  35. package/exports/engine/storage/s3.js +0 -3
  36. package/src/SchemaCompiler.js +0 -192
  37. package/src/Transactions.js +0 -145
  38. package/src/engine/StorageEngine.js +0 -250
  39. package/src/engine/storage/FileStorageEngine.js +0 -213
  40. package/src/type/index.js +0 -32
  41. /package/src/{type/resolved → data/properties}/SlugType.js +0 -0
@@ -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,192 +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
- if (Type.Model.isModel(property?._items)) {
83
- schema.properties[name].items = {
84
- type: 'object',
85
- additionalProperties: false,
86
- required: ['id'],
87
- properties: {
88
- id: {
89
- type: 'string',
90
- pattern: `^${property?._items.toString()}/[A-Z0-9]+$`,
91
- },
92
- },
93
- };
94
- }
95
- }
96
- }
97
-
98
- class Schema extends CompiledSchema {
99
- /**
100
- * The compiled schema definition.
101
- * @type {Object}
102
- * @static
103
- * @private
104
- */
105
- static _schema = schema;
106
-
107
- /**
108
- * The AJV validator function compiled from the schema.
109
- * @type {Function}
110
- * @static
111
- * @private
112
- */
113
- static _validator = validation.compile(schema);
114
- }
115
-
116
- return Schema;
117
- }
118
- }
119
-
120
-
121
- /**
122
- * Represents a compiled schema used for validating data models.
123
- * This class provides a mechanism to validate data using a precompiled schema and a validator function.
124
- */
125
- export class CompiledSchema {
126
- /**
127
- * The schema definition for validation, typically a precompiled JSON schema or similar.
128
- * @type {?Object}
129
- * @static
130
- * @private
131
- */
132
- static _schema = null;
133
-
134
- /**
135
- * The validator function used to validate data against the schema.
136
- * @type {?Function}
137
- * @static
138
- * @private
139
- */
140
- static _validator = null;
141
-
142
- /**
143
- * Validates the given data against the compiled schema.
144
- *
145
- * If the data is an instance of a model, it will be converted to a plain object via `toData()` before validation.
146
- *
147
- * @param {Object|Model} data - The data or model instance to be validated.
148
- * @returns {boolean} - Returns `true` if the data is valid according to the schema.
149
- * @throws {ValidationError} - Throws a `ValidationError` if the data is invalid.
150
- */
151
- static validate(data) {
152
- let inputData = Object.assign({}, data);
153
-
154
- if (Type.Model.isModel(data)) {
155
- inputData = data.toData();
156
- }
157
-
158
- const valid = this._validator?.(inputData);
159
-
160
- if (valid) return valid;
161
-
162
- throw new ValidationError(inputData, this._validator.errors);
163
- }
164
- }
165
-
166
- /**
167
- * Represents a validation error that occurs when a model or data fails validation.
168
- * Extends the built-in JavaScript `Error` class.
169
- */
170
- export class ValidationError extends Error {
171
- /**
172
- * Creates an instance of `ValidationError`.
173
- *
174
- * @param {Object} data - The data that failed validation.
175
- * @param {Array<Object>} errors - A list of validation errors, each typically containing details about what failed.
176
- */
177
- constructor(data, errors) {
178
- super('Validation failed');
179
- /**
180
- * An array of validation errors, containing details about each failed validation.
181
- * @type {Array<Object>}
182
- */
183
- this.errors = errors;
184
- /**
185
- * The data that caused the validation error.
186
- * @type {Object}
187
- */
188
- this.data = data;
189
- }
190
- }
191
-
192
- 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,250 +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
-
24
- /**
25
- * @param {Type.Model} modelToProcess
26
- * @return {Promise<void>}
27
- */
28
- const processModel = async (modelToProcess) => {
29
- if (processedModels.includes(modelToProcess.id)) return;
30
-
31
- processedModels.push(modelToProcess.id);
32
-
33
- if (!Object.keys(this.models).includes(modelToProcess.constructor.name)) throw new ModelNotRegisteredStorageEngineError(modelToProcess, this);
34
-
35
- modelToProcess.validate();
36
- const currentModel = await this.get(modelToProcess.id).catch(() => null);
37
-
38
- const modelToProcessHasChanged = (JSON.stringify(currentModel?.toData() || {}) !== JSON.stringify(modelToProcess.toData()));
39
-
40
- if (modelToProcessHasChanged) modelsToPut.push(modelToProcess);
41
-
42
- if (
43
- Boolean(modelToProcess.constructor.indexedProperties().length) &&
44
- this._indexedFieldsHaveChanged(currentModel, modelToProcess)
45
- ) {
46
- const modelToProcessConstructor = this.getModelConstructorFromId(modelToProcess.id);
47
- modelsToReindex[modelToProcessConstructor] = modelsToReindex[modelToProcessConstructor] || [];
48
- modelsToReindex[modelToProcessConstructor].push(modelToProcess);
49
- }
50
-
51
- for (const [field, value] of Object.entries(modelToProcess)) {
52
- if (Type.Model.isModel(value)) {
53
- await processModel(modelToProcess[field]);
54
- }
55
- }
56
- };
57
-
58
- await processModel(model);
59
-
60
- await Promise.all(modelsToPut.map(m => this._putModel(m.toData())));
61
- await Promise.all(Object.entries(modelsToReindex).map(async ([constructor, models]) => {
62
- const modelConstructor = this.models[constructor];
63
- const index = await this._getIndex(modelConstructor);
64
-
65
- await this._putIndex(modelConstructor, {
66
- ...index || {},
67
- ...Object.fromEntries(models.map(m => [m.id, m.toIndexData()])),
68
- });
69
- }));
70
- }
71
-
72
- /**
73
- * Decide if two models indexable fields have changed
74
- * @param {Type.Model} currentModel
75
- * @param {Type.Model} modelToProcess
76
- * @return {boolean}
77
- * @private
78
- */
79
- _indexedFieldsHaveChanged(currentModel, modelToProcess) {
80
- return !currentModel || Boolean(
81
- modelToProcess.constructor.indexedProperties()
82
- .filter(field => JSON.stringify(_.get(currentModel, field)) !== JSON.stringify(_.get(modelToProcess, field)))
83
- .length,
84
- );
85
- }
86
-
87
- /**
88
- * Get a model by its id
89
- * @param {string} modelId
90
- * @return {Promise<void>}
91
- */
92
- get(modelId) {
93
- try {
94
- this.getModelConstructorFromId(modelId);
95
- } catch (e) {
96
- return Promise.reject(e);
97
- }
98
- return this._getModel(modelId);
99
- }
100
-
101
- /**
102
- * Get the model constructor from a model id
103
- * @param {string} modelId
104
- * @return {Model.constructor}
105
- */
106
- getModelConstructorFromId(modelId) {
107
- const modelName = modelId.split('/')[0];
108
- const constructor = this.models[modelName];
109
-
110
- if (!constructor) throw new ModelNotRegisteredStorageEngineError(modelName, this);
111
-
112
- return constructor;
113
- }
114
-
115
- /**
116
- * Get model classes that are directly linked to the given model in either direction
117
- * @param {Type.Model.constructor} model
118
- * @return {Record<string, Record<string, Type.Model.constructor>>}
119
- */
120
- getLinksFor(model) {
121
- return Object.fromEntries(
122
- Object.entries(this.getAllModelLinks())
123
- .filter(([modelName, links]) =>
124
- model.name === modelName ||
125
- Object.values(links).some((link) => link.name === model.name),
126
- ),
127
- );
128
- }
129
-
130
- /**
131
- * Get all model links
132
- * @return {Record<string, Record<string, Type.Model.constructor>>}
133
- */
134
- getAllModelLinks() {
135
- return Object.entries(this.models)
136
- .map(([registeredModelName, registeredModelClass]) =>
137
- Object.entries(registeredModelClass)
138
- .map(([propertyName, propertyType]) => [
139
- registeredModelName,
140
- propertyName,
141
- typeof propertyType === 'function' && !Type.Model.isModel(propertyType) ? propertyType() : propertyType,
142
- ])
143
- .filter(([_m, _p, type]) => Type.Model.isModel(type))
144
- .map(([containingModel, propertyName, propertyType]) => ({
145
- containingModel,
146
- propertyName,
147
- propertyType,
148
- })),
149
- )
150
- .flat()
151
- .reduce((accumulator, {containingModel, propertyName, propertyType}) => ({
152
- ...accumulator,
153
- [containingModel]: {
154
- ...accumulator[containingModel] || {},
155
- [propertyName]: propertyType,
156
- },
157
- }), {});
158
- }
159
-
160
- /**
161
- * Update a model
162
- * @param {Model} _model
163
- * @throws MethodNotImplementedStorageEngineError
164
- * @return Promise<void>
165
- */
166
- _putModel(_model) {
167
- return Promise.reject(new MethodNotImplementedStorageEngineError('_putModel', this));
168
- }
169
-
170
- /**
171
- * Get a model
172
- * @param {string} _id
173
- * @throws MethodNotImplementedStorageEngineError
174
- * @throws ModelNotFoundStorageEngineError
175
- * @return Promise<void>
176
- */
177
- _getModel(_id) {
178
- return Promise.reject(new MethodNotImplementedStorageEngineError('_getModel', this));
179
- }
180
-
181
- /**
182
- * Get a model's index data
183
- * @param {Model.constructor} _modelConstructor
184
- * @throws MethodNotImplementedStorageEngineError
185
- * @return Promise<void>
186
- */
187
- _getIndex(_modelConstructor) {
188
- return Promise.reject(new MethodNotImplementedStorageEngineError('_getIndex', this));
189
- }
190
-
191
- /**
192
- * Put a model's index data
193
- * @param {Model.constructor} _modelConstructor
194
- * @param {object} _data
195
- * @throws MethodNotImplementedStorageEngineError
196
- * @return Promise<void>
197
- */
198
- _putIndex(_modelConstructor, _data) {
199
- return Promise.reject(new MethodNotImplementedStorageEngineError('_putIndex', this));
200
- }
201
- }
202
-
203
- /**
204
- * @class StorageEngineError
205
- * @extends Error
206
- */
207
- export class StorageEngineError extends Error {
208
- }
209
-
210
- /**
211
- * @class ModelNotRegisteredStorageEngineError
212
- * @extends StorageEngineError
213
- */
214
- export class ModelNotRegisteredStorageEngineError extends StorageEngineError {
215
- /**
216
- * @param {Type.Model} model
217
- * @param {StorageEngine} storageEngine
218
- */
219
- constructor(model, storageEngine) {
220
- const modelName = typeof model === 'string' ? model : model.constructor.name;
221
- super(`The model ${modelName} is not registered in the storage engine ${storageEngine.constructor.name}`);
222
- }
223
- }
224
-
225
- /**
226
- * @class MethodNotImplementedStorageEngineError
227
- * @extends StorageEngineError
228
- */
229
- export class MethodNotImplementedStorageEngineError extends StorageEngineError {
230
- /**
231
- * @param {string} method
232
- * @param {StorageEngine} storageEngine
233
- */
234
- constructor(method, storageEngine) {
235
- super(`The method ${method} is not implemented in the storage engine ${storageEngine.constructor.name}`);
236
- }
237
- }
238
-
239
- /**
240
- * @class ModelNotFoundStorageEngineError
241
- * @extends StorageEngineError
242
- */
243
- export class ModelNotFoundStorageEngineError extends StorageEngineError {
244
- /**
245
- * @param {string} modelId
246
- */
247
- constructor(modelId) {
248
- super(`The model ${modelId} was not found`);
249
- }
250
- }