@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 CHANGED
@@ -41,10 +41,10 @@ import Persist from '@acodeninja/persist';
41
41
 
42
42
  class Person extends Persist.Model {
43
43
  static {
44
- this.name = Persist.Property.String.required;
45
- this.dateOfBirth = Persist.Property.Date.required;
46
- this.height = Persist.Property.Number.required;
47
- this.isStudent = Persist.Property.Boolean.required;
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
  ```
@@ -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.Type.Model {
17
+ export class Person extends Persist.Model {
18
18
  static {
19
- this.withName('Person');
20
- this.name = Persist.Type.String.required;
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.Type.Model {
40
+ export class Person extends Persist.Model {
41
41
  static {
42
- this.address = () => Address;
42
+ Person.address = () => Address;
43
43
  }
44
44
  }
45
45
 
46
- export class Address extends Persist.Type.Model {
46
+ export class Address extends Persist.Model {
47
47
  static {
48
- this.person = () => Person;
49
- this.address = Persist.Type.String.required;
50
- this.postcode = Persist.Type.String.required;
48
+ Address.person = () => Person;
49
+ Address.address = Persist.Property.String.required;
50
+ Address.postcode = Persist.Property.String.required;
51
51
  }
52
52
  }
53
53
  ```
@@ -28,10 +28,10 @@ import Persist from '@acodeninja/persist';
28
28
 
29
29
  class Person extends Persist.Model {
30
30
  static {
31
- this.name = Persist.Property.String.required;
32
- this.dateOfBirth = Persist.Property.Date.required;
33
- this.height = Persist.Property.Number.required;
34
- this.isStudent = Persist.Property.Boolean.required;
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
- this.address = Persist.Property.String.required;
49
- this.postcode = Persist.Property.String.required;
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
- this.name = Persist.Property.String.required;
56
- this.address = Address;
55
+ Person.name = Persist.Property.String.required;
56
+ Person.address = Address;
57
57
  }
58
58
  }
59
59
  ```
@@ -13,8 +13,8 @@ import Persist from '@acodeninja/persist';
13
13
 
14
14
  class Person extends Persist.Model {
15
15
  static {
16
- this.firstName = Persist.Property.String;
17
- this.lastName = Persist.Property.String;
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
- this.firstName = Persist.Property.String;
34
- this.lastName = Persist.Property.String.required;
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
- this.markettingEmailsActive = Persist.Property.Boolean;
49
- this.accountActive = Persist.Property.Boolean.required;
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
- this.loginToken = Persist.Property.Number;
64
- this.accountId = Persist.Property.Number.required;
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
- this.lastLogin = Persist.Property.Date;
81
- this.createdAt = Persist.Property.Date.required;
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
- this.failedLoginAttempts = Persist.Property.Array.of(Persist.Property.Date);
98
- this.fullName = Persist.Property.Array.of(Persist.Property.String).required;
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
- this.address = Persist.Property.Custom.of({
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
- this.title = Persist.Property.String;
144
- this.slug = Persist.Property.Resolved.Slug.of('title');
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
- this.requiredString = Type.String.required;
166
- this.requiredNumber = Type.Number.required;
167
- this.requiredBoolean = Type.Boolean.required;
168
- this.requiredDate = Type.Date.required;
169
- this.requiredArrayOfString = Type.Array.of(Type.String).required;
170
- this.requiredArrayOfNumber = Type.Array.of(Type.Number).required;
171
- this.requiredArrayOfBoolean = Type.Array.of(Type.Boolean).required;
172
- this.requiredArrayOfDate = Type.Array.of(Type.Date).required;
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
- this.name = Persist.Property.String.required;
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
- this.address = Persist.Property.String.required;
19
- this.postcode = Persist.Property.String.required;
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
- this.name = Persist.Property.String.required;
34
- this.address = () => Address;
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
- this.address = Persist.Property.String.required;
41
- this.postcode = Persist.Property.String.required;
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
- this.name = Persist.Property.String.required;
61
- this.address = () => Address;
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
- this.person = () => Person;
68
- this.address = Persist.Property.String.required;
69
- this.postcode = Persist.Property.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
  ```
@@ -80,16 +80,16 @@ import Persist from "@acodeninja/persist";
80
80
 
81
81
  export class Person extends Persist.Model {
82
82
  static {
83
- this.name = Persist.Property.String.required;
84
- this.addresses = () => Persist.Property.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
88
  export class Address extends Persist.Model {
89
89
  static {
90
- this.person = () => Person;
91
- this.address = Persist.Property.String.required;
92
- this.postcode = Persist.Property.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
  ```
@@ -105,16 +105,16 @@ import Persist from "@acodeninja/persist";
105
105
 
106
106
  export class Person extends Persist.Model {
107
107
  static {
108
- this.name = Persist.Property.String.required;
109
- this.addresses = () => Persist.Property.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
113
  export class Address extends Persist.Model {
114
114
  static {
115
- this.people = () => Persist.Property.Array.of(Person);
116
- this.address = Persist.Property.String.required;
117
- this.postcode = Persist.Property.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
  ```
@@ -130,24 +130,24 @@ import Persist from "@acodeninja/persist";
130
130
 
131
131
  export class Person extends Persist.Model {
132
132
  static {
133
- this.name = Persist.Property.String.required;
134
- this.addresses = () => Persist.Property.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
138
  export class Abode extends Persist.Model {
139
139
  static {
140
- this.moveInDate = Persist.Property.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
146
  export class Address extends Persist.Model {
147
147
  static {
148
- this.people = () => Persist.Property.Array.of(Person);
149
- this.address = Persist.Property.String.required;
150
- this.postcode = Persist.Property.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
  ```
@@ -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 `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
 
@@ -13,18 +13,18 @@ import Persist from "@acodeninja/persist";
13
13
 
14
14
  export class Person extends Persist.Model {
15
15
  static {
16
- this.name = Persist.Property.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
22
  export class Address extends Persist.Model {
23
23
  static {
24
- this.address = Persist.Property.String.required;
25
- this.postcode = Persist.Property.String.required;
26
- this.people = () => Persist.Property.Array.of(Person)
27
- this.indexProperties = () => ['postcode', 'people.[*].name'];
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
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acodeninja/persist",
3
- "version": "3.0.0-next.26",
3
+ "version": "3.0.0-next.28",
4
4
  "description": "A JSON based data modelling and persistence module with alternate storage mechanisms.",
5
5
  "type": "module",
6
6
  "scripts": {