@acodeninja/persist 3.0.0-next.9 → 3.0.1-next.1

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 -472
  40. package/src/engine/storage/FileStorageEngine.js +0 -213
  41. package/src/type/index.js +0 -32
@@ -1,22 +1,22 @@
1
1
  # Models as Properties
2
2
 
3
- In addition to assigning basic types to model properties, you can assign entire models as properties. This allows for the creation of complex relationships between models. For information on using basic types for properties, refer to [model property types](./model-property-types.md).
3
+ In addition to assigning basic types to model properties, you can assign entire models as properties. This allows for the creation of complex relationships between models. For information on using basic types for properties, refer to [model property types](./model-properties.md).
4
4
 
5
5
  We’ll explore different types of relationships between models using examples of `Person` and `Address` models, evolving the definition step by step.
6
6
 
7
7
  ```javascript
8
8
  import Persist from "@acodeninja/persist";
9
9
 
10
- export class Person extends Persist.Type.Model {
10
+ export class Person extends Persist.Model {
11
11
  static {
12
- this.name = Persist.Type.String.required;
12
+ Person.name = Persist.Property.String.required;
13
13
  }
14
14
  }
15
15
 
16
- export class Address extends Persist.Type.Model {
16
+ export class Address extends Persist.Model {
17
17
  static {
18
- this.address = Persist.Type.String.required;
19
- this.postcode = Persist.Type.String.required;
18
+ Address.address = Persist.Property.String.required;
19
+ Address.postcode = Persist.Property.String.required;
20
20
  }
21
21
  }
22
22
  ```
@@ -28,17 +28,17 @@ To define a **one-to-one** relationship between two models, set a static propert
28
28
  ```javascript
29
29
  import Persist from "@acodeninja/persist";
30
30
 
31
- export class Person extends Persist.Type.Model {
31
+ export class Person extends Persist.Model {
32
32
  static {
33
- this.name = Persist.Type.String.required;
34
- this.address = () => Address;
33
+ Person.name = Persist.Property.String.required;
34
+ Person.address = () => Address;
35
35
  }
36
36
  }
37
37
 
38
- export class Address extends Persist.Type.Model {
38
+ export class Address extends Persist.Model {
39
39
  static {
40
- this.address = Persist.Type.String.required;
41
- this.postcode = Persist.Type.String.required;
40
+ Address.address = Persist.Property.String.required;
41
+ Address.postcode = Persist.Property.String.required;
42
42
  }
43
43
  }
44
44
  ```
@@ -46,7 +46,7 @@ export class Address extends Persist.Type.Model {
46
46
  > [!IMPORTANT]
47
47
  > **Why Use an Arrow Function?**
48
48
  >
49
- > The arrow function allows the model to reference another model that may not have been defined yet. Without it, you might encounter an error like `ReferenceError: Cannot access 'Address' before initialization`.
49
+ > The arrow function allows the model to reference another model that may not have been defined yet. Without it, you might encounter an error like `ReferenceError: Cannot access 'Address' before initialization`. See [Reference Errors](./code-quirks.md#reference-errors).
50
50
 
51
51
  ### Circular One-to-One Relationships
52
52
 
@@ -55,41 +55,41 @@ You can extend the previous example by allowing both models to reference each ot
55
55
  ```javascript
56
56
  import Persist from "@acodeninja/persist";
57
57
 
58
- export class Person extends Persist.Type.Model {
58
+ export class Person extends Persist.Model {
59
59
  static {
60
- this.name = Persist.Type.String.required;
61
- this.address = () => Address;
60
+ Person.name = Persist.Property.String.required;
61
+ Person.address = () => Address;
62
62
  }
63
63
  }
64
64
 
65
- export class Address extends Persist.Type.Model {
65
+ export class Address extends Persist.Model {
66
66
  static {
67
- this.person = () => Person;
68
- this.address = Persist.Type.String.required;
69
- this.postcode = Persist.Type.String.required;
67
+ Address.person = () => Person;
68
+ Address.address = Persist.Property.String.required;
69
+ Address.postcode = Persist.Property.String.required;
70
70
  }
71
71
  }
72
72
  ```
73
73
 
74
74
  ## One-to-Many Relationships
75
75
 
76
- To model a **one-to-many** relationship, use `Persist.Type.Array` to store an array of related models. For instance, if a `Person` can have multiple addresses, this is how it would be defined:
76
+ To model a **one-to-many** relationship, use `Persist.Property.Array` to store an array of related models. For instance, if a `Person` can have multiple addresses, this is how it would be defined:
77
77
 
78
78
  ```javascript
79
79
  import Persist from "@acodeninja/persist";
80
80
 
81
- export class Person extends Persist.Type.Model {
81
+ export class Person extends Persist.Model {
82
82
  static {
83
- this.name = Persist.Type.String.required;
84
- this.addresses = () => Persist.Type.Array.of(Address);
83
+ Person.name = Persist.Property.String.required;
84
+ Person.addresses = () => Persist.Property.Array.of(Address);
85
85
  }
86
86
  }
87
87
 
88
- export class Address extends Persist.Type.Model {
88
+ export class Address extends Persist.Model {
89
89
  static {
90
- this.person = () => Person;
91
- this.address = Persist.Type.String.required;
92
- this.postcode = Persist.Type.String.required;
90
+ Address.person = () => Person;
91
+ Address.address = Persist.Property.String.required;
92
+ Address.postcode = Persist.Property.String.required;
93
93
  }
94
94
  }
95
95
  ```
@@ -103,18 +103,18 @@ In some cases, you may want to model a many-to-many relationship. For example, i
103
103
  ```javascript
104
104
  import Persist from "@acodeninja/persist";
105
105
 
106
- export class Person extends Persist.Type.Model {
106
+ export class Person extends Persist.Model {
107
107
  static {
108
- this.name = Persist.Type.String.required;
109
- this.addresses = () => Persist.Type.Array.of(Address);
108
+ Person.name = Persist.Property.String.required;
109
+ Person.addresses = () => Persist.Property.Array.of(Address);
110
110
  }
111
111
  }
112
112
 
113
- export class Address extends Persist.Type.Model {
113
+ export class Address extends Persist.Model {
114
114
  static {
115
- this.people = () => Persist.Type.Array.of(Person);
116
- this.address = Persist.Type.String.required;
117
- this.postcode = Persist.Type.String.required;
115
+ Address.people = () => Persist.Property.Array.of(Person);
116
+ Address.address = Persist.Property.String.required;
117
+ Address.postcode = Persist.Property.String.required;
118
118
  }
119
119
  }
120
120
  ```
@@ -128,26 +128,26 @@ In more complex scenarios, you may want to capture additional information about
128
128
  ```javascript
129
129
  import Persist from "@acodeninja/persist";
130
130
 
131
- export class Person extends Persist.Type.Model {
131
+ export class Person extends Persist.Model {
132
132
  static {
133
- this.name = Persist.Type.String.required;
134
- this.addresses = () => Persist.Type.Array.of(Abode);
133
+ Person.name = Persist.Property.String.required;
134
+ Person.addresses = () => Persist.Property.Array.of(Abode);
135
135
  }
136
136
  }
137
137
 
138
- export class Abode extends Persist.Type.Model {
138
+ export class Abode extends Persist.Model {
139
139
  static {
140
- this.moveInDate = Persist.Type.Date.required;
141
- this.address = () => Address;
142
- this.person = () => Person;
140
+ Abode.moveInDate = Persist.Property.Date.required;
141
+ Abode.address = () => Address;
142
+ Abode.person = () => Person;
143
143
  }
144
144
  }
145
145
 
146
- export class Address extends Persist.Type.Model {
146
+ export class Address extends Persist.Model {
147
147
  static {
148
- this.people = () => Persist.Type.Array.of(Person);
149
- this.address = Persist.Type.String.required;
150
- this.postcode = Persist.Type.String.required;
148
+ Address.people = () => Persist.Property.Array.of(Person);
149
+ Address.address = Persist.Property.String.required;
150
+ Address.postcode = Persist.Property.String.required;
151
151
  }
152
152
  }
153
153
  ```
@@ -11,19 +11,19 @@ Let's consider the following models:
11
11
  ```javascript
12
12
  import Persist from "@acodeninja/persist";
13
13
 
14
- export class Person extends Persist.Type.Model {
14
+ export class Person extends Persist.Model {
15
15
  static {
16
- this.name = Persist.Type.String.required;
17
- this.address = () => Address;
18
- this.searchProperties = () => ['name', 'address.address'];
16
+ Person.name = Persist.Property.String.required;
17
+ Person.address = () => Address;
18
+ Person.searchProperties = () => ['name', 'address.address'];
19
19
  }
20
20
  }
21
21
 
22
- export class Address extends Persist.Type.Model {
22
+ export class Address extends Persist.Model {
23
23
  static {
24
- this.address = Persist.Type.String.required;
25
- this.postcode = Persist.Type.String.required;
26
- this.searchProperties = () => ['address', 'postcode'];
24
+ Address.address = Persist.Property.String.required;
25
+ Address.postcode = Persist.Property.String.required;
26
+ Address.searchProperties = () => ['address', 'postcode'];
27
27
  }
28
28
  }
29
29
  ```
@@ -36,12 +36,10 @@ To search for any `Person` who lives on station road, the following search query
36
36
 
37
37
  ```javascript
38
38
  import Persist from "@acodeninja/persist";
39
- import Person from "./Person";
40
- import FileStorageEngine from "@acodeninja/persist/engine/storage/file"
41
39
 
42
- FileStorageEngine
43
- .configure(configuration)
44
- .search(Person, 'station road');
40
+ const connection = Persist.getConnections('people');
41
+
42
+ await connection.search(Person, 'station road');
45
43
  ```
46
44
 
47
45
  This will find all matches for people who live at any address that includes `station road`.
@@ -2,23 +2,25 @@
2
2
 
3
3
  Persist makes several storage engines available for use with the library
4
4
 
5
- ## Filesystem Storage StorageEngine
5
+ ## S3 Storage StorageEngine
6
6
 
7
- To store models using the local file system, use the `File` storage engine.
7
+ To store models using an S3 Bucket, use the `S3` storage engine. To use the `S3` engine you must also add the `@aws-sdk/client-s3` dependency to your `package.json` file.
8
8
 
9
9
  ```javascript
10
10
  import Persist from "@acodeninja/persist";
11
- import FileStorageEngine from "@acodeninja/persist/engine/storage/file";
11
+ import {S3Client} from "@aws-sdk/client-s3";
12
+ import S3StorageEngine from "@acodeninja/persist/storage/s3";
12
13
 
13
- Persist.addEngine('local', FileStorageEngine, {
14
- path: '/app/storage',
15
- });
14
+ const connection = Persist.registerConnection('remote', new S3StorageEngine({
15
+ bucket: 'test-bucket',
16
+ client: new S3Client(),
17
+ }));
16
18
 
17
- export class Tag extends Persist.Type.Model {
18
- static tag = Persist.Type.String.required;
19
+ export class Tag extends Persist.Model {
20
+ static tag = Persist.Property.String.required;
19
21
  }
20
22
 
21
- await Persist.getEngine('local', FileStorageEngine).put(new Tag({tag: 'documentation'}));
23
+ await connection.put(new Tag({tag: 'documentation'}));
22
24
  ```
23
25
 
24
26
  ## HTTP Storage StorageEngine
@@ -27,35 +29,17 @@ To store models using an HTTP server, use the `HTTP` storage engine. When using
27
29
 
28
30
  ```javascript
29
31
  import Persist from "@acodeninja/persist";
30
- import HTTPStorageEngine from "@acodeninja/persist/engine/storage/http";
32
+ import HTTPStorageEngine from "@acodeninja/persist/storage/http";
31
33
 
32
- Persist.addEngine('remote', HTTPStorageEngine, {
33
- host: 'https://api.example.com',
34
- });
34
+ const connection = Persist.registerConnection('remote', new HTTPStorageEngine({
35
+ baseUrl: 'https://api.example.com',
36
+ }));
35
37
 
36
- export class Tag extends Persist.Type.Model {
37
- static tag = Persist.Type.String.required;
38
+ export class Tag extends Persist.Model {
39
+ static tag = Persist.Property.String.required;
38
40
  }
39
41
 
40
- await Persist.getEngine('remote', HTTPStorageEngine).put(new Tag({tag: 'documentation'}));
42
+ await connection.put(new Tag({tag: 'documentation'}));
41
43
  ```
42
44
 
43
- ## S3 Storage StorageEngine
44
-
45
- To store models using an S3 Bucket, use the `S3` storage engine. To use the `S3` engine you must also add the `@aws-sdk/client-s3` dependency to your `package.json` file.
46
-
47
- ```javascript
48
- import Persist from "@acodeninja/persist";
49
- import S3StorageEngine from "@acodeninja/persist/engine/storage/s3";
50
-
51
- Persist.addEngine('remote', S3StorageEngine, {
52
- bucket: 'test-bucket',
53
- client: new S3Client(),
54
- });
55
-
56
- export class Tag extends Persist.Type.Model {
57
- static tag = Persist.Type.String.required;
58
- }
59
-
60
- await Persist.getEngine('remote', S3StorageEngine).put(new Tag({tag: 'documentation'}));
61
- ```
45
+ A generic Open API specification for an HTTP server integration with `Persist` can be found [here](./http.openapi.yml).
@@ -4,26 +4,27 @@ Use structured queries when you need to filter a collection of models using a se
4
4
 
5
5
  ## Indexing Data
6
6
 
7
- To set index properties on a model, define the static function `indexProperties` as an arrow function that returns an array of fields that should be indexed for querying.
7
+ To set index properties on a model, define the static function `indexedProperties` as an arrow function that returns an array of fields that should be indexed for querying.
8
8
 
9
9
  Let's consider the following models:
10
10
 
11
11
  ```javascript
12
12
  import Persist from "@acodeninja/persist";
13
13
 
14
- export class Person extends Persist.Type.Model {
14
+ export class Person extends Persist.Model {
15
15
  static {
16
- this.name = Persist.Type.String.required;
17
- this.address = () => Address;
18
- this.indexProperties = () => ['name', 'address.postcode'];
16
+ Person.name = Persist.Property.String.required;
17
+ Person.address = () => Address;
18
+ Person.indexedProperties = () => ['name', 'address.postcode'];
19
19
  }
20
20
  }
21
21
 
22
- export class Address extends Persist.Type.Model {
22
+ export class Address extends Persist.Model {
23
23
  static {
24
- this.address = Persist.Type.String.required;
25
- this.postcode = Persist.Type.String.required;
26
- this.indexProperties = () => ['postcode'];
24
+ Address.address = Persist.Property.String.required;
25
+ Address.postcode = Persist.Property.String.required;
26
+ Address.people = () => Persist.Property.Array.of(Person)
27
+ Address.indexedProperties = () => ['postcode', 'people.[*].name'];
27
28
  }
28
29
  }
29
30
  ```
@@ -39,14 +40,12 @@ To query for a `Person` called `Joe Bloggs` an exact query can be written:
39
40
 
40
41
  ```javascript
41
42
  import Persist from "@acodeninja/persist";
42
- import Person from "./Person";
43
- import FileStorageEngine from "@acodeninja/persist/engine/storage/file"
44
-
45
- FileStorageEngine
46
- .configure(configuration)
47
- .find(Person, {
48
- name: {$is: 'Joe Bloggs'},
49
- });
43
+
44
+ const connection = Persist.getConnection('people');
45
+
46
+ await connection.find(Person, {
47
+ name: {$is: 'Joe Bloggs'},
48
+ });
50
49
  ```
51
50
 
52
51
  ## Querying Partial Matches
@@ -55,14 +54,30 @@ To query for a `Person` with name `Joe` a contains query can be written:
55
54
 
56
55
  ```javascript
57
56
  import Persist from "@acodeninja/persist";
58
- import Person from "./Person";
59
- import FileStorageEngine from "@acodeninja/persist/engine/storage/file"
60
-
61
- FileStorageEngine
62
- .configure(configuration)
63
- .find(Person, {
64
- name: {$contains: 'Joe'},
65
- });
57
+
58
+ const connection = Persist.getConnection('people');
59
+
60
+ await connection.find(Person, {
61
+ name: {$contains: 'Joe'},
62
+ });
63
+ ```
64
+
65
+ ## Querying One-to-Many Model links
66
+
67
+ To query for all instances of `Address` with a linked `Person` with a name that contains `Joe` you can write:
68
+
69
+ ```javascript
70
+ import Persist from "@acodeninja/persist";
71
+
72
+ const connection = Persist.getConnection('people');
73
+
74
+ await connection.find(Address, {
75
+ people: {
76
+ $contains: {
77
+ name: {$contains: 'Joe'},
78
+ },
79
+ },
80
+ });
66
81
  ```
67
82
 
68
83
  ## Querying Combination Matches
@@ -71,18 +86,16 @@ To query for a `Person` who lives at `SW1 1AA` a combination of contains and exa
71
86
 
72
87
  ```javascript
73
88
  import Persist from "@acodeninja/persist";
74
- import Person from "./Person";
75
- import FileStorageEngine from "@acodeninja/persist/engine/storage/file"
76
-
77
- FileStorageEngine
78
- .configure(configuration)
79
- .find(Person, {
80
- address: {
81
- $contains: {
82
- postcode: {$is: 'SW1 1AA'},
83
- },
89
+
90
+ const connection = Persist.getConnection('people');
91
+
92
+ await connection.find(Person, {
93
+ address: {
94
+ $contains: {
95
+ postcode: {$is: 'SW1 1AA'},
84
96
  },
85
- });
97
+ },
98
+ });
86
99
  ```
87
100
 
88
101
  ## Multiple Queries
@@ -91,17 +104,15 @@ To query for anyone called `Joe Bloggs` who lives in the `SW1` postcode area, we
91
104
 
92
105
  ```javascript
93
106
  import Persist from "@acodeninja/persist";
94
- import Person from "./Person";
95
- import FileStorageEngine from "@acodeninja/persist/engine/storage/file"
96
-
97
- FileStorageEngine
98
- .configure(configuration)
99
- .find(Person, {
100
- name: {$is: 'Joe Bloggs'},
101
- address: {
102
- $contains: {
103
- postcode: {$contains: 'SW1'},
104
- },
107
+
108
+ const connection = Persist.getConnection('people');
109
+
110
+ await connection.find(Person, {
111
+ name: {$is: 'Joe Bloggs'},
112
+ address: {
113
+ $contains: {
114
+ postcode: {$contains: 'SW1'},
105
115
  },
106
- });
116
+ },
117
+ });
107
118
  ```
@@ -4,19 +4,18 @@ Create transactions to automatically roll back on failure.
4
4
 
5
5
  ```javascript
6
6
  import Persist from "@acodeninja/persist";
7
- import S3StorageEngine from "@acodeninja/persist/engine/storage/s3";
7
+ import S3StorageEngine from "@acodeninja/persist/storage/s3";
8
8
 
9
- Persist.addEngine('remote', S3StorageEngine, {
9
+ const connection = Persist.registerConnection('remote', new S3StorageEngine({
10
10
  bucket: 'test-bucket',
11
11
  client: new S3Client(),
12
- transactions: true,
13
- });
12
+ }));
14
13
 
15
- export class Tag extends Persist.Type.Model {
16
- static tag = Persist.Type.String.required;
14
+ export class Tag extends Persist.Model {
15
+ static tag = Persist.Property.String.required;
17
16
  }
18
17
 
19
- const transaction = Persist.getEngine('remote', S3StorageEngine).start();
18
+ const transaction = connection.start();
20
19
 
21
20
  await transaction.put(new Tag({tag: 'documentation'}));
22
21
  await transaction.commit();
@@ -0,0 +1,3 @@
1
+ import HTTPStorageEngine from '../../src/engine/storage/HTTPStorageEngine.js';
2
+
3
+ export default HTTPStorageEngine;
@@ -0,0 +1,3 @@
1
+ import S3StorageEngine from '../../src/engine/storage/S3StorageEngine.js';
2
+
3
+ export default S3StorageEngine;
package/jest.config.cjs CHANGED
@@ -1,10 +1,11 @@
1
1
  /** @type {import('jest').Config} */
2
2
  const config = {
3
- coveragePathIgnorePatterns: [
4
- 'node_modules',
5
- 'test/fixtures',
6
- 'test/mocks',
7
- 'test/scripts',
3
+ collectCoverage: true,
4
+ coveragePathIgnorePatterns: ['test/fixtures/minified/'],
5
+ collectCoverageFrom: [
6
+ '**/*.js',
7
+ '!{node_modules,coverage,exports}/**',
8
+ '!*.config.js',
8
9
  ],
9
10
  coverageThreshold: {
10
11
  global: {
@@ -14,13 +15,8 @@ const config = {
14
15
  statements: 100,
15
16
  },
16
17
  },
17
- testMatch: [
18
- '**/*.test.js',
19
- ],
20
- watchPathIgnorePatterns: [
21
- 'coverage/',
22
- 'test/fixtures/minified',
23
- ],
18
+ testMatch: ['**/*.test.js'],
19
+ watchPathIgnorePatterns: ['coverage/', 'test/fixtures/minified'],
24
20
  };
25
21
 
26
22
  module.exports = config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acodeninja/persist",
3
- "version": "3.0.0-next.9",
3
+ "version": "3.0.1-next.1",
4
4
  "description": "A JSON based data modelling and persistence module with alternate storage mechanisms.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "exports": {
14
14
  ".": "./exports/default.js",
15
- "./engine/*": "./exports/engine/*.js"
15
+ "./storage/*": "./exports/storage/*.js"
16
16
  },
17
17
  "repository": {
18
18
  "url": "https://github.com/acodeninja/persist"