@acodeninja/persist 3.0.0-next.26 → 3.0.0-next.28
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/README.md +4 -4
- package/docs/code-quirks.md +9 -9
- package/docs/defining-models.md +8 -8
- package/docs/model-properties.md +54 -23
- package/docs/models-as-properties.md +30 -30
- package/docs/search-queries.md +8 -8
- package/docs/structured-queries.md +8 -8
- package/package.json +1 -1
- package/src/Connection.js +344 -217
- package/src/Schema.js +3 -3
- package/src/data/Model.js +63 -9
- package/src/data/Property.js +2 -0
- package/src/data/properties/ArrayType.js +4 -2
- package/src/data/properties/BooleanType.js +2 -2
- package/src/data/properties/CustomType.js +4 -4
- package/src/data/properties/DateType.js +3 -3
- package/src/data/properties/NumberType.js +2 -2
- package/src/data/properties/SlugType.js +1 -1
- package/src/data/properties/StringType.js +2 -2
- package/src/data/properties/Type.js +5 -3
- package/src/engine/storage/StorageEngine.js +3 -1
package/README.md
CHANGED
@@ -41,10 +41,10 @@ import Persist from '@acodeninja/persist';
|
|
41
41
|
|
42
42
|
class Person extends Persist.Model {
|
43
43
|
static {
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
Person.name = Persist.Property.String.required;
|
45
|
+
Person.dateOfBirth = Persist.Property.Date.required;
|
46
|
+
Person.height = Persist.Property.Number.required;
|
47
|
+
Person.isStudent = Persist.Property.Boolean.required;
|
48
48
|
}
|
49
49
|
}
|
50
50
|
```
|
package/docs/code-quirks.md
CHANGED
@@ -14,10 +14,10 @@ To avoid this problem, you have two options:
|
|
14
14
|
```javascript
|
15
15
|
import Persist from "@acodeninja/persist";
|
16
16
|
|
17
|
-
export class Person extends Persist.
|
17
|
+
export class Person extends Persist.Model {
|
18
18
|
static {
|
19
|
-
|
20
|
-
|
19
|
+
Person.withName('Person');
|
20
|
+
Person.name = Persist.Property.String.required;
|
21
21
|
}
|
22
22
|
}
|
23
23
|
```
|
@@ -37,17 +37,17 @@ To avoid these errors, always define model relationships using arrow functions.
|
|
37
37
|
```javascript
|
38
38
|
import Persist from "@acodeninja/persist";
|
39
39
|
|
40
|
-
export class Person extends Persist.
|
40
|
+
export class Person extends Persist.Model {
|
41
41
|
static {
|
42
|
-
|
42
|
+
Person.address = () => Address;
|
43
43
|
}
|
44
44
|
}
|
45
45
|
|
46
|
-
export class Address extends Persist.
|
46
|
+
export class Address extends Persist.Model {
|
47
47
|
static {
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
Address.person = () => Person;
|
49
|
+
Address.address = Persist.Property.String.required;
|
50
|
+
Address.postcode = Persist.Property.String.required;
|
51
51
|
}
|
52
52
|
}
|
53
53
|
```
|
package/docs/defining-models.md
CHANGED
@@ -28,10 +28,10 @@ import Persist from '@acodeninja/persist';
|
|
28
28
|
|
29
29
|
class Person extends Persist.Model {
|
30
30
|
static {
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
Person.name = Persist.Property.String.required;
|
32
|
+
Person.dateOfBirth = Persist.Property.Date.required;
|
33
|
+
Person.height = Persist.Property.Number.required;
|
34
|
+
Person.isStudent = Persist.Property.Boolean.required;
|
35
35
|
}
|
36
36
|
}
|
37
37
|
```
|
@@ -45,15 +45,15 @@ Models can be linked to other models by declaring them as properties.
|
|
45
45
|
```javascript
|
46
46
|
class Address extends Persist.Model {
|
47
47
|
static {
|
48
|
-
|
49
|
-
|
48
|
+
Address.address = Persist.Property.String.required;
|
49
|
+
Address.postcode = Persist.Property.String.required;
|
50
50
|
}
|
51
51
|
}
|
52
52
|
|
53
53
|
class Person extends Persist.Model {
|
54
54
|
static {
|
55
|
-
|
56
|
-
|
55
|
+
Person.name = Persist.Property.String.required;
|
56
|
+
Person.address = Address;
|
57
57
|
}
|
58
58
|
}
|
59
59
|
```
|
package/docs/model-properties.md
CHANGED
@@ -13,8 +13,8 @@ import Persist from '@acodeninja/persist';
|
|
13
13
|
|
14
14
|
class Person extends Persist.Model {
|
15
15
|
static {
|
16
|
-
|
17
|
-
|
16
|
+
Person.firstName = Persist.Property.String;
|
17
|
+
Person.lastName = Persist.Property.String;
|
18
18
|
}
|
19
19
|
}
|
20
20
|
```
|
@@ -30,8 +30,8 @@ import Persist from '@acodeninja/persist';
|
|
30
30
|
|
31
31
|
class Person extends Persist.Model {
|
32
32
|
static {
|
33
|
-
|
34
|
-
|
33
|
+
Person.firstName = Persist.Property.String;
|
34
|
+
Person.lastName = Persist.Property.String.required;
|
35
35
|
}
|
36
36
|
}
|
37
37
|
```
|
@@ -45,8 +45,8 @@ import Persist from '@acodeninja/persist';
|
|
45
45
|
|
46
46
|
class Person extends Persist.Model {
|
47
47
|
static {
|
48
|
-
|
49
|
-
|
48
|
+
Person.marketingEmailsActive = Persist.Property.Boolean;
|
49
|
+
Person.accountActive = Persist.Property.Boolean.required;
|
50
50
|
}
|
51
51
|
}
|
52
52
|
```
|
@@ -60,8 +60,8 @@ import Persist from '@acodeninja/persist';
|
|
60
60
|
|
61
61
|
class Person extends Persist.Model {
|
62
62
|
static {
|
63
|
-
|
64
|
-
|
63
|
+
Person.loginToken = Persist.Property.Number;
|
64
|
+
Person.accountId = Persist.Property.Number.required;
|
65
65
|
}
|
66
66
|
}
|
67
67
|
```
|
@@ -77,8 +77,8 @@ import Persist from '@acodeninja/persist';
|
|
77
77
|
|
78
78
|
class Person extends Persist.Model {
|
79
79
|
static {
|
80
|
-
|
81
|
-
|
80
|
+
Person.lastLogin = Persist.Property.Date;
|
81
|
+
Person.createdAt = Persist.Property.Date.required;
|
82
82
|
}
|
83
83
|
}
|
84
84
|
```
|
@@ -94,8 +94,8 @@ import Persist from '@acodeninja/persist';
|
|
94
94
|
|
95
95
|
class Person extends Persist.Model {
|
96
96
|
static {
|
97
|
-
|
98
|
-
|
97
|
+
Person.failedLoginAttempts = Persist.Property.Array.of(Persist.Property.Date);
|
98
|
+
Person.fullName = Persist.Property.Array.of(Persist.Property.String).required;
|
99
99
|
}
|
100
100
|
}
|
101
101
|
```
|
@@ -109,7 +109,7 @@ import Persist from '@acodeninja/persist';
|
|
109
109
|
|
110
110
|
class Person extends Persist.Model {
|
111
111
|
static {
|
112
|
-
|
112
|
+
Person.address = Persist.Property.Custom.of({
|
113
113
|
type: 'object',
|
114
114
|
additionalProperties: false,
|
115
115
|
required: ['line1', 'city', 'postcode'],
|
@@ -140,8 +140,8 @@ import Persist from '@acodeninja/persist';
|
|
140
140
|
|
141
141
|
class Page extends Persist.Model {
|
142
142
|
static {
|
143
|
-
|
144
|
-
|
143
|
+
Page.title = Persist.Property.String;
|
144
|
+
Page.slug = Persist.Property.Resolved.Slug.of('title');
|
145
145
|
}
|
146
146
|
}
|
147
147
|
|
@@ -162,14 +162,45 @@ Most types support the `.required` modifier, which will alter validation to enfo
|
|
162
162
|
```javascript
|
163
163
|
class RequiredStringModel extends Persist.Model {
|
164
164
|
static {
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
165
|
+
RequiredStringModel.requiredString = Persist.Property.String.required;
|
166
|
+
RequiredStringModel.requiredNumber = Persist.Property.Number.required;
|
167
|
+
RequiredStringModel.requiredBoolean = Persist.Property.Boolean.required;
|
168
|
+
RequiredStringModel.requiredDate = Persist.Property.Date.required;
|
169
|
+
RequiredStringModel.requiredArrayOfString = Persist.Property.Array.of(Persist.Property.String).required;
|
170
|
+
RequiredStringModel.requiredArrayOfNumber = Persist.Property.Array.of(Persist.Property.Number).required;
|
171
|
+
RequiredStringModel.requiredArrayOfBoolean = Persist.Property.Array.of(Persist.Property.Boolean).required;
|
172
|
+
RequiredStringModel.requiredArrayOfDate = Persist.Property.Array.of(Persist.Property.Date).required;
|
173
173
|
}
|
174
174
|
}
|
175
175
|
```
|
176
|
+
|
177
|
+
## Custom Property Types
|
178
|
+
|
179
|
+
Under the hood, model validation uses the [ajv](https://ajv.js.org/) library with [ajv-formats](https://ajv.js.org/packages/ajv-formats.html) included. Because of this, you can create your own property types.
|
180
|
+
|
181
|
+
Say you want to attach an IPv4 address to your models. The following type class can accomplish this.
|
182
|
+
|
183
|
+
```javascript
|
184
|
+
import Persist from '@acodeninja/persist';
|
185
|
+
|
186
|
+
class IPv4Type extends Persist.Property.Type {
|
187
|
+
static {
|
188
|
+
// Set the type of the property to string
|
189
|
+
IPv4Type._type = 'string';
|
190
|
+
// Use the ajv extended format "ipv4"
|
191
|
+
IPv4Type._format = 'ipv4';
|
192
|
+
// Ensure that even when minified, the name of the constructor is IPv4
|
193
|
+
Object.defineProperty(IPv4Type, 'name', {value: 'IPv4'});
|
194
|
+
}
|
195
|
+
}
|
196
|
+
```
|
197
|
+
|
198
|
+
This type can then be used in any model as needed.
|
199
|
+
|
200
|
+
```javascript
|
201
|
+
import Persist from '@acodeninja/persist';
|
202
|
+
|
203
|
+
class StaticIP extends Persist.Model {
|
204
|
+
static ip = IPv4Type.required;
|
205
|
+
}
|
206
|
+
```
|
@@ -9,14 +9,14 @@ import Persist from "@acodeninja/persist";
|
|
9
9
|
|
10
10
|
export class Person extends Persist.Model {
|
11
11
|
static {
|
12
|
-
|
12
|
+
Person.name = Persist.Property.String.required;
|
13
13
|
}
|
14
14
|
}
|
15
15
|
|
16
16
|
export class Address extends Persist.Model {
|
17
17
|
static {
|
18
|
-
|
19
|
-
|
18
|
+
Address.address = Persist.Property.String.required;
|
19
|
+
Address.postcode = Persist.Property.String.required;
|
20
20
|
}
|
21
21
|
}
|
22
22
|
```
|
@@ -30,15 +30,15 @@ import Persist from "@acodeninja/persist";
|
|
30
30
|
|
31
31
|
export class Person extends Persist.Model {
|
32
32
|
static {
|
33
|
-
|
34
|
-
|
33
|
+
Person.name = Persist.Property.String.required;
|
34
|
+
Person.address = () => Address;
|
35
35
|
}
|
36
36
|
}
|
37
37
|
|
38
38
|
export class Address extends Persist.Model {
|
39
39
|
static {
|
40
|
-
|
41
|
-
|
40
|
+
Address.address = Persist.Property.String.required;
|
41
|
+
Address.postcode = Persist.Property.String.required;
|
42
42
|
}
|
43
43
|
}
|
44
44
|
```
|
@@ -57,16 +57,16 @@ import Persist from "@acodeninja/persist";
|
|
57
57
|
|
58
58
|
export class Person extends Persist.Model {
|
59
59
|
static {
|
60
|
-
|
61
|
-
|
60
|
+
Person.name = Persist.Property.String.required;
|
61
|
+
Person.address = () => Address;
|
62
62
|
}
|
63
63
|
}
|
64
64
|
|
65
65
|
export class Address extends Persist.Model {
|
66
66
|
static {
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
Address.person = () => Person;
|
68
|
+
Address.address = Persist.Property.String.required;
|
69
|
+
Address.postcode = Persist.Property.String.required;
|
70
70
|
}
|
71
71
|
}
|
72
72
|
```
|
@@ -80,16 +80,16 @@ import Persist from "@acodeninja/persist";
|
|
80
80
|
|
81
81
|
export class Person extends Persist.Model {
|
82
82
|
static {
|
83
|
-
|
84
|
-
|
83
|
+
Person.name = Persist.Property.String.required;
|
84
|
+
Person.addresses = () => Persist.Property.Array.of(Address);
|
85
85
|
}
|
86
86
|
}
|
87
87
|
|
88
88
|
export class Address extends Persist.Model {
|
89
89
|
static {
|
90
|
-
|
91
|
-
|
92
|
-
|
90
|
+
Address.person = () => Person;
|
91
|
+
Address.address = Persist.Property.String.required;
|
92
|
+
Address.postcode = Persist.Property.String.required;
|
93
93
|
}
|
94
94
|
}
|
95
95
|
```
|
@@ -105,16 +105,16 @@ import Persist from "@acodeninja/persist";
|
|
105
105
|
|
106
106
|
export class Person extends Persist.Model {
|
107
107
|
static {
|
108
|
-
|
109
|
-
|
108
|
+
Person.name = Persist.Property.String.required;
|
109
|
+
Person.addresses = () => Persist.Property.Array.of(Address);
|
110
110
|
}
|
111
111
|
}
|
112
112
|
|
113
113
|
export class Address extends Persist.Model {
|
114
114
|
static {
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
```
|
@@ -130,24 +130,24 @@ import Persist from "@acodeninja/persist";
|
|
130
130
|
|
131
131
|
export class Person extends Persist.Model {
|
132
132
|
static {
|
133
|
-
|
134
|
-
|
133
|
+
Person.name = Persist.Property.String.required;
|
134
|
+
Person.addresses = () => Persist.Property.Array.of(Abode);
|
135
135
|
}
|
136
136
|
}
|
137
137
|
|
138
138
|
export class Abode extends Persist.Model {
|
139
139
|
static {
|
140
|
-
|
141
|
-
|
142
|
-
|
140
|
+
Abode.moveInDate = Persist.Property.Date.required;
|
141
|
+
Abode.address = () => Address;
|
142
|
+
Abode.person = () => Person;
|
143
143
|
}
|
144
144
|
}
|
145
145
|
|
146
146
|
export class Address extends Persist.Model {
|
147
147
|
static {
|
148
|
-
|
149
|
-
|
150
|
-
|
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
|
```
|
package/docs/search-queries.md
CHANGED
@@ -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.
|
14
|
+
export class Person extends Persist.Model {
|
15
15
|
static {
|
16
|
-
|
17
|
-
|
18
|
-
|
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.
|
22
|
+
export class Address extends Persist.Model {
|
23
23
|
static {
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
```
|
@@ -4,7 +4,7 @@ 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 `
|
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
|
|
@@ -13,18 +13,18 @@ import Persist from "@acodeninja/persist";
|
|
13
13
|
|
14
14
|
export class Person extends Persist.Model {
|
15
15
|
static {
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
Person.name = Persist.Property.String.required;
|
17
|
+
Person.address = () => Address;
|
18
|
+
Person.indexedProperties = () => ['name', 'address.postcode'];
|
19
19
|
}
|
20
20
|
}
|
21
21
|
|
22
22
|
export class Address extends Persist.Model {
|
23
23
|
static {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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'];
|
28
28
|
}
|
29
29
|
}
|
30
30
|
```
|