@acodeninja/persist 2.3.0 → 2.3.2

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
@@ -1,243 +1,29 @@
1
1
  # @acodeninja/persist
2
2
 
3
- A JSON based data modelling and persistence library with alternate storage mechanisms.
3
+ A JSON based data modelling and persistence library with alternate storage mechanisms, designed with static site generation in mind.
4
4
 
5
- ## Models
5
+ ![NPM Version](https://img.shields.io/npm/v/%40acodeninja%2Fpersist)
6
+ ![NPM Unpacked Size](https://img.shields.io/npm/unpacked-size/%40acodeninja%2Fpersist)
7
+ ![GitHub top language](https://img.shields.io/github/languages/top/acodeninja/persist)
8
+ ![NPM Downloads](https://img.shields.io/npm/dw/%40acodeninja%2Fpersist)
6
9
 
7
- The `Model` and `Type` classes allow creating representations of data objects
10
+ [![DeepSource](https://app.deepsource.com/gh/acodeninja/persist.svg/?label=active+issues&show_trend=true&token=Vd8_PJuRwwoq4_uBJ0_ymc06)](https://app.deepsource.com/gh/acodeninja/persist/)
11
+ [![DeepSource](https://app.deepsource.com/gh/acodeninja/persist.svg/?label=code+coverage&show_trend=true&token=Vd8_PJuRwwoq4_uBJ0_ymc06)](https://app.deepsource.com/gh/acodeninja/persist/)
8
12
 
9
- ### Defining Models
13
+ ## Features
10
14
 
11
- ##### A model using all available basic types
15
+ - Data modelling with relationships
16
+ - Data validation
17
+ - Data querying
18
+ - Fuzzy search
19
+ - Storage with: S3, HTTP and Filesystem
12
20
 
13
- ```javascript
14
- import Persist from "@acodeninja/persist";
21
+ ## Find out more
15
22
 
16
- export class SimpleModel extends Persist.Type.Model {
17
- static boolean = Persist.Type.Boolean;
18
- static string = Persist.Type.String;
19
- static number = Persist.Type.Number;
20
- static date = Persist.Type.Date;
21
- }
22
- ```
23
-
24
- ##### A simple model using required types
25
-
26
- ```javascript
27
- import Persist from "@acodeninja/persist";
28
-
29
- export class SimpleModel extends Persist.Type.Model {
30
- static requiredBoolean = Persist.Type.Boolean.required;
31
- static requiredString = Persist.Type.String.required;
32
- static requiredNumber = Persist.Type.Number.required;
33
- static requiredDate = Persist.Type.Date.required;
34
- }
35
- ```
36
-
37
- ##### A simple model using arrays of basic types
38
-
39
- ```javascript
40
- import Persist from "@acodeninja/persist";
41
-
42
- export class SimpleModel extends Persist.Type.Model {
43
- static arrayOfBooleans = Persist.Type.Array.of(Type.Boolean);
44
- static arrayOfStrings = Persist.Type.Array.of(Type.String);
45
- static arrayOfNumbers = Persist.Type.Array.of(Type.Number);
46
- static arrayOfDates = Persist.Type.Array.of(Type.Date);
47
- static requiredArrayOfBooleans = Persist.Type.Array.of(Type.Boolean).required;
48
- static requiredArrayOfStrings = Persist.Type.Array.of(Type.String).required;
49
- static requiredArrayOfNumbers = Persist.Type.Array.of(Type.Number).required;
50
- static requiredArrayOfDates = Persist.Type.Array.of(Type.Date).required;
51
- }
52
- ```
53
-
54
- <details>
55
- <summary>Complex relationships are also supported</summary>
56
-
57
- #### One-to-One Relationships
58
-
59
- ##### A one-to-one relationship
60
-
61
- ```javascript
62
- import Persist from "@acodeninja/persist";
63
-
64
- export class ModelB extends Persist.Type.Model {
65
- }
66
-
67
- export class ModelA extends Persist.Type.Model {
68
- static linked = ModelB;
69
- }
70
- ```
71
-
72
- ##### A circular one-to-one relationship
73
-
74
- ```javascript
75
- import Persist from "@acodeninja/persist";
76
-
77
- export class ModelA extends Persist.Type.Model {
78
- static linked = () => ModelB;
79
- }
80
-
81
- export class ModelB extends Persist.Type.Model {
82
- static linked = ModelA;
83
- }
84
- ```
85
-
86
- #### One-to-Many Relationships
87
-
88
- ##### A one-to-many relationship
89
-
90
- ```javascript
91
- import Persist from "@acodeninja/persist";
92
-
93
- export class ModelB extends Persist.Type.Model {
94
- }
95
-
96
- export class ModelA extends Persist.Type.Model {
97
- static linked = Persist.Type.Array.of(ModelB);
98
- }
99
- ```
100
-
101
- ##### A circular one-to-many relationship
102
-
103
- ```javascript
104
- import Persist from "@acodeninja/persist";
105
-
106
- export class ModelA extends Persist.Type.Model {
107
- static linked = () => Type.Array.of(ModelB);
108
- }
109
-
110
- export class ModelB extends Persist.Type.Model {
111
- static linked = ModelA;
112
- }
113
- ```
114
-
115
- #### Many-to-Many Relationships
116
-
117
- ##### A many-to-many relationship
118
-
119
- ```javascript
120
- import Persist from "@acodeninja/persist";
121
-
122
- export class ModelA extends Persist.Type.Model {
123
- static linked = Persist.Type.Array.of(ModelB);
124
- }
125
-
126
- export class ModelB extends Persist.Type.Model {
127
- static linked = Persist.Type.Array.of(ModelA);
128
- }
129
- ```
130
- </details>
131
-
132
- ## Find and Search
133
-
134
- Models may expose a `searchProperties()` and `indexProperties()` static method to indicate which
135
- fields should be indexed for storage engine `find()` and `search()` methods.
136
-
137
- Use `find()` for a low usage exact string match on any indexed attribute of a model.
138
-
139
- Use `search()` for a medium usage fuzzy string match on any search indexed attribute of a model.
140
-
141
- ```javascript
142
- import Persist from "@acodeninja/persist";
143
- import FileEngine from "@acodeninja/persist/engine/file";
144
-
145
- export class Tag extends Persist.Type.Model {
146
- static tag = Persist.Type.String.required;
147
- static description = Persist.Type.String;
148
- static searchProperties = () => ['tag', 'description'];
149
- static indexProperties = () => ['tag'];
150
- }
151
-
152
- const tag = new Tag({tag: 'documentation', description: 'How to use the persist library'});
153
-
154
- await FileEngine.find(Tag, {tag: 'documentation'});
155
- // [Tag {tag: 'documentation', description: 'How to use the persist library'}]
156
-
157
- await FileEngine.search(Tag, 'how to');
158
- // [Tag {tag: 'documentation', description: 'How to use the persist library'}]
159
- ```
160
-
161
- ## Storage
162
-
163
- ### Filesystem Storage Engine
164
-
165
- To store models using the local file system, use the `File` storage engine.
166
-
167
- ```javascript
168
- import Persist from "@acodeninja/persist";
169
- import FileEngine from "@acodeninja/persist/engine/file";
170
-
171
- Persist.addEngine('local', FileEngine, {
172
- path: '/app/storage',
173
- });
174
-
175
- export class Tag extends Persist.Type.Model {
176
- static tag = Persist.Type.String.required;
177
- }
178
-
179
- await Persist.getEngine('local', FileEngine).put(new Tag({tag: 'documentation'}));
180
- ```
181
-
182
- ### HTTP Storage Engine
183
-
184
- To store models using an S3 Bucket, use the `S3` storage engine.
185
-
186
- ```javascript
187
- import Persist from "@acodeninja/persist";
188
- import HTTPEngine from "@acodeninja/persist/engine/http";
189
-
190
- Persist.addEngine('remote', HTTPEngine, {
191
- host: 'https://api.example.com',
192
- });
193
-
194
- export class Tag extends Persist.Type.Model {
195
- static tag = Persist.Type.String.required;
196
- }
197
-
198
- await Persist.getEngine('remote', HTTPEngine).put(new Tag({tag: 'documentation'}));
199
- ```
200
-
201
- ### S3 Storage Engine
202
-
203
- To store models using an S3 Bucket, use the `S3` storage engine.
204
-
205
- ```javascript
206
- import Persist from "@acodeninja/persist";
207
- import S3Engine from "@acodeninja/persist/engine/s3";
208
-
209
- Persist.addEngine('remote', S3Engine, {
210
- bucket: 'test-bucket',
211
- client: new S3Client(),
212
- });
213
-
214
- export class Tag extends Persist.Type.Model {
215
- static tag = Persist.Type.String.required;
216
- }
217
-
218
- await Persist.getEngine('remote', S3Engine).put(new Tag({tag: 'documentation'}));
219
- ```
220
-
221
- ## Transactions
222
-
223
- Create transactions to automatically roll back on failure to update.
224
-
225
- ```javascript
226
- import Persist from "@acodeninja/persist";
227
- import S3Engine from "@acodeninja/persist/engine/s3";
228
-
229
- Persist.addEngine('remote', S3Engine, {
230
- bucket: 'test-bucket',
231
- client: new S3Client(),
232
- transactions: true,
233
- });
234
-
235
- export class Tag extends Persist.Type.Model {
236
- static tag = Persist.Type.String.required;
237
- }
238
-
239
- const transaction = Persist.getEngine('remote', S3Engine).start();
240
-
241
- await transaction.put(new Tag({tag: 'documentation'}));
242
- await transaction.commit();
243
- ```
23
+ - [Model Property Types](./docs/model-property-types.md)
24
+ - [Models as Properties](./docs/models-as-properties.md)
25
+ - [Structured Queries](./docs/structured-queries.md)
26
+ - [Search Queries](./docs/search-queries.md)
27
+ - [Storage Engines](./docs/storage-engines.md)
28
+ - [Transactions](./docs/transactions.md)
29
+ - [Quirks](./docs/code-quirks.md)
@@ -0,0 +1,71 @@
1
+ # Code Quirks
2
+
3
+ When using Persist in a minified or bundled codebase, it's important to be aware of two key quirks: handling class names during minification and managing reference errors when working with model relationships.
4
+
5
+ ## Class Names and Minification
6
+
7
+ When you bundle or minify JavaScript code for production, class names are often altered, which can cause issues. Specifically, models may lose their original class names, which we rely on for storing data in the correct namespace.
8
+
9
+ To avoid this problem, you have two options:
10
+
11
+ 1. Disable class name mangling in your minifier.
12
+ 2. Use `this.setMinifiedName(name)` to manually specify the model's name.
13
+
14
+ ```javascript
15
+ import Persist from "@acodeninja/persist";
16
+
17
+ export class Person extends Persist.Type.Model {
18
+ static {
19
+ this.setMinifiedName('Person');
20
+ this.name = Persist.Type.String.required;
21
+ }
22
+ }
23
+ ```
24
+
25
+ If you don't set the minified name, the wrong namespace may be used when saving models, leading to unexpected behavior.
26
+
27
+ ## Reference Errors
28
+
29
+ When defining relationships between models, especially circular relationships (e.g., `Person` references `Address`, and `Address` references `Person`), the order of declarations matters. If the models are referenced before they are initialized, you'll encounter `ReferenceError` messages, like:
30
+
31
+ ```console
32
+ ReferenceError: Cannot access 'Address' before initialization
33
+ ```
34
+
35
+ To avoid these errors, always define model relationships using arrow functions. For example:
36
+
37
+ ```javascript
38
+ import Persist from "@acodeninja/persist";
39
+
40
+ export class Person extends Persist.Type.Model {
41
+ static {
42
+ this.address = () => Address;
43
+ }
44
+ }
45
+
46
+ export class Address extends Persist.Type.Model {
47
+ static {
48
+ this.person = () => Person;
49
+ this.address = Persist.Type.String.required;
50
+ this.postcode = Persist.Type.String.required;
51
+ }
52
+ }
53
+ ```
54
+
55
+ By doing this, you ensure that model references are evaluated lazily, after all models have been initialized, preventing `ReferenceError` issues.
56
+
57
+ ## Using `HTTP` Engine in Browser
58
+
59
+ When implementing thee `HTTP` engine for code that runs in the web browser, you must pass `fetch` into the engine configuration and bind it to the `window` object.
60
+
61
+ ```javascript
62
+ import Persist from "@acodeninja/persist";
63
+ import HTTPEngine from "@acodeninja/persist/engine/http";
64
+
65
+ Persist.addEngine('remote', HTTPEngine, {
66
+ host: 'https://api.example.com',
67
+ fetch: fetch.bind(window),
68
+ });
69
+ ```
70
+
71
+ This will ensure that `fetch` can access the window context which is required for it to function.
@@ -0,0 +1,173 @@
1
+ # Model Property Types
2
+
3
+ Persist uses a type definition for the properties of each model, this allows for validation and type coercion when saving and retrieving data.
4
+
5
+ Model properties can be assigned a `Type`, or another `Model`. For more information on see [Models as Properties](./models-as-properties.md).
6
+
7
+ ## Defining Model Properties
8
+
9
+ Properties can be defined on a model by setting static properties to the value of a type on the class that describes the model.
10
+
11
+ ```javascript
12
+ import Persist from '@acodeninja/persist';
13
+
14
+ class Person extends Persist.Type.Model {
15
+ static {
16
+ this.firstName = Persist.Type.String;
17
+ this.lastName = Persist.Type.String;
18
+ }
19
+ }
20
+ ```
21
+
22
+ ## Simple Types
23
+
24
+ ### `Persist.Type.String`
25
+
26
+ Use the `String` type for model properties that should store a [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String). The `String` type also supports the `.required` modifier to ensure that when the model is persisted a value must exist for it.
27
+
28
+ ```javascript
29
+ import Persist from '@acodeninja/persist';
30
+
31
+ class Person extends Persist.Type.Model {
32
+ static {
33
+ this.firstName = Persist.Type.String;
34
+ this.lastName = Persist.Type.String.required;
35
+ }
36
+ }
37
+ ```
38
+
39
+ ### `Persist.Type.Boolean`
40
+
41
+ Use the `Boolean` type for model properties that should store a [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean). The `Boolean` type also supports the `.required` modifier to ensure that when the model is persisted a value must exist for it.
42
+
43
+ ```javascript
44
+ import Persist from '@acodeninja/persist';
45
+
46
+ class Person extends Persist.Type.Model {
47
+ static {
48
+ this.markettingEmailsActive = Persist.Type.Boolean;
49
+ this.accountActive = Persist.Type.Boolean.required;
50
+ }
51
+ }
52
+ ```
53
+
54
+ ### `Persist.Type.Number`
55
+
56
+ Use the `Number` type for model properties that should store a [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number). The `Number` type also supports the `.required` modifier to ensure that when the model is persisted a value must exist for it.
57
+
58
+ ```javascript
59
+ import Persist from '@acodeninja/persist';
60
+
61
+ class Person extends Persist.Type.Model {
62
+ static {
63
+ this.loginToken = Persist.Type.Number;
64
+ this.accountId = Persist.Type.Number.required;
65
+ }
66
+ }
67
+ ```
68
+
69
+ ### `Persist.Type.Date`
70
+
71
+ Use the `Date` type for model properties that should store a [date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). The `Date` type also supports the `.required` modifier to ensure that when the model is persisted a value must exist for it.
72
+
73
+ ```javascript
74
+ import Persist from '@acodeninja/persist';
75
+
76
+ class Person extends Persist.Type.Model {
77
+ static {
78
+ this.lastLogin = Persist.Type.Date;
79
+ this.createdAt = Persist.Type.Date.required;
80
+ }
81
+ }
82
+ ```
83
+
84
+ ## Complex Types
85
+
86
+ ### `Persist.Type.Array.of(type)`
87
+
88
+ Use the `Array` type for model properties that should store an array of another type or model. The `Array` type also supports the `.required` modifier to ensure that when the model is persisted a value must exist for it.
89
+
90
+ ```javascript
91
+ import Persist from '@acodeninja/persist';
92
+
93
+ class Person extends Persist.Type.Model {
94
+ static {
95
+ this.failedLoginAttempts = Persist.Type.Array.of(Persist.Type.Date);
96
+ this.fullName = Persist.Type.Array.of(Persist.Type.String).required;
97
+ }
98
+ }
99
+ ```
100
+
101
+ ### `Persist.Type.Custom.of(schema)`
102
+
103
+ Use the `Custom` type for model properties that should store a custom [json-schema draft-07](https://json-schema.org/draft-07/json-schema-hypermedia) object. You can also use any formats defined by the [`avj-formats`](https://ajv.js.org/packages/ajv-formats.html) library. The `Custom` type also supports the `.required` modifier to ensure that when the model is persisted a value must exist for it.
104
+
105
+ ```javascript
106
+ import Persist from '@acodeninja/persist';
107
+
108
+ class Person extends Persist.Type.Model {
109
+ static {
110
+ this.address = Persist.Type.Custom.of({
111
+ type: 'object',
112
+ additionalProperties: false,
113
+ required: ['line1', 'city', 'postcode'],
114
+ properties: {
115
+ line1: {type: 'string'},
116
+ line2: {type: 'string'},
117
+ city: {type: 'string'},
118
+ postcode: {
119
+ type: 'string',
120
+ pattern: "^[A-Z]+[0-9]+\s[A-Z]+[0-9]+$",
121
+ },
122
+ },
123
+ }).required;
124
+ }
125
+ }
126
+ ```
127
+
128
+ ## Resolved Types
129
+
130
+ Resolved types are different from other types in that they do not directly store data themselves, rather they perform an action on another property of the model.
131
+
132
+ ### `Persist.Type.Resolved.Slug.of(property)`
133
+
134
+ Use the `Slug` type for model properties that should have a slug version of another properties value. The `Custom` type also supports the `.required` modifier to ensure that when the model is persisted a value must exist for it.
135
+
136
+ ```javascript
137
+ import Persist from '@acodeninja/persist';
138
+
139
+ class Page extends Persist.Type.Model {
140
+ static {
141
+ this.title = Persist.Type.String;
142
+ this.slug = Persist.Type.Resolved.Slug.of('title');
143
+ }
144
+ }
145
+
146
+ const page = new Page({title: 'A really important article!'});
147
+ const {slug} = page.toData();
148
+
149
+ console.log(slug); // a-really-important-article
150
+ ```
151
+
152
+ ## Modifiers
153
+
154
+ Models and most types support a modifier, this will alter the validation and persistence process based on the type of modifier used.
155
+
156
+ ### `.required`
157
+
158
+ Most types support the `.required` modifier, which will alter validation to enforce the presence of the property when saving data.
159
+
160
+ ```javascript
161
+ class RequiredStringModel extends Persist.Type.Model {
162
+ static {
163
+ this.requiredString = Type.String.required;
164
+ this.requiredNumber = Type.Number.required;
165
+ this.requiredBoolean = Type.Boolean.required;
166
+ this.requiredDate = Type.Date.required;
167
+ this.requiredArrayOfString = Type.Array.of(Type.String).required;
168
+ this.requiredArrayOfNumber = Type.Array.of(Type.Number).required;
169
+ this.requiredArrayOfBoolean = Type.Array.of(Type.Boolean).required;
170
+ this.requiredArrayOfDate = Type.Array.of(Type.Date).required;
171
+ }
172
+ }
173
+ ```
@@ -0,0 +1,159 @@
1
+ # Models as Properties
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).
4
+
5
+ We’ll explore different types of relationships between models using examples of `Person` and `Address` models, evolving the definition step by step.
6
+
7
+ ```javascript
8
+ import Persist from "@acodeninja/persist";
9
+
10
+ export class Person extends Persist.Type.Model {
11
+ static {
12
+ this.name = Persist.Type.String.required;
13
+ }
14
+ }
15
+
16
+ export class Address extends Persist.Type.Model {
17
+ static {
18
+ this.address = Persist.Type.String.required;
19
+ this.postcode = Persist.Type.String.required;
20
+ }
21
+ }
22
+ ```
23
+
24
+ ## One-to-One Relationships
25
+
26
+ To define a **one-to-one** relationship between two models, set a static property in one model as a function that returns the other model. This ensures that the models can be defined in any order, avoiding issues with initialization.
27
+
28
+ ```javascript
29
+ import Persist from "@acodeninja/persist";
30
+
31
+ export class Person extends Persist.Type.Model {
32
+ static {
33
+ this.name = Persist.Type.String.required;
34
+ this.address = () => Address;
35
+ }
36
+ }
37
+
38
+ export class Address extends Persist.Type.Model {
39
+ static {
40
+ this.address = Persist.Type.String.required;
41
+ this.postcode = Persist.Type.String.required;
42
+ }
43
+ }
44
+ ```
45
+
46
+ > [!IMPORTANT]
47
+ > **Why Use an Arrow Function?**
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`.
50
+
51
+ ### Circular One-to-One Relationships
52
+
53
+ You can extend the previous example by allowing both models to reference each other. This is useful for circular relationships, where querying one model (e.g., `Address`) should also allow access to the related model (e.g., `Person`).
54
+
55
+ ```javascript
56
+ import Persist from "@acodeninja/persist";
57
+
58
+ export class Person extends Persist.Type.Model {
59
+ static {
60
+ this.name = Persist.Type.String.required;
61
+ this.address = () => Address;
62
+ }
63
+ }
64
+
65
+ export class Address extends Persist.Type.Model {
66
+ static {
67
+ this.person = () => Person;
68
+ this.address = Persist.Type.String.required;
69
+ this.postcode = Persist.Type.String.required;
70
+ }
71
+ }
72
+ ```
73
+
74
+ ## One-to-Many Relationships
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:
77
+
78
+ ```javascript
79
+ import Persist from "@acodeninja/persist";
80
+
81
+ export class Person extends Persist.Type.Model {
82
+ static {
83
+ this.name = Persist.Type.String.required;
84
+ this.addresses = () => Persist.Type.Array.of(Address);
85
+ }
86
+ }
87
+
88
+ export class Address extends Persist.Type.Model {
89
+ static {
90
+ this.person = () => Person;
91
+ this.address = Persist.Type.String.required;
92
+ this.postcode = Persist.Type.String.required;
93
+ }
94
+ }
95
+ ```
96
+
97
+ This structure allows for querying both the Person and their multiple Address records, while maintaining the ability to retrieve the related person from any address.
98
+
99
+ ## Many-to-Many Relationships
100
+
101
+ In some cases, you may want to model a many-to-many relationship. For example, if multiple people can live at the same address, this type of relationship is ideal.
102
+
103
+ ```javascript
104
+ import Persist from "@acodeninja/persist";
105
+
106
+ export class Person extends Persist.Type.Model {
107
+ static {
108
+ this.name = Persist.Type.String.required;
109
+ this.addresses = () => Persist.Type.Array.of(Address);
110
+ }
111
+ }
112
+
113
+ export class Address extends Persist.Type.Model {
114
+ static {
115
+ this.people = () => Persist.Type.Array.of(Person);
116
+ this.address = Persist.Type.String.required;
117
+ this.postcode = Persist.Type.String.required;
118
+ }
119
+ }
120
+ ```
121
+
122
+ This allows both `Person` and `Address` models to reference each other as arrays, establishing a many-to-many relationship.
123
+
124
+ ## Combining Relationships
125
+
126
+ In more complex scenarios, you may want to capture additional information about the relationship itself. For example, when tracking when a person moved to a particular address, you can create an intermediary model (e.g., `Abode`) to store this information.
127
+
128
+ ```javascript
129
+ import Persist from "@acodeninja/persist";
130
+
131
+ export class Person extends Persist.Type.Model {
132
+ static {
133
+ this.name = Persist.Type.String.required;
134
+ this.addresses = () => Persist.Type.Array.of(Abode);
135
+ }
136
+ }
137
+
138
+ export class Abode extends Persist.Type.Model {
139
+ static {
140
+ this.moveInDate = Persist.Type.Date.required;
141
+ this.address = () => Address;
142
+ this.person = () => Person;
143
+ }
144
+ }
145
+
146
+ export class Address extends Persist.Type.Model {
147
+ static {
148
+ this.people = () => Persist.Type.Array.of(Person);
149
+ this.address = Persist.Type.String.required;
150
+ this.postcode = Persist.Type.String.required;
151
+ }
152
+ }
153
+ ```
154
+
155
+ In this setup:
156
+
157
+ - A `Person` can have multiple `Abode` entries (i.e., where they lived and when they moved in).
158
+ - Each `Abode` links a `Person` to an `Address`, while also recording the move-in date.
159
+ - An `Address` can still reference multiple people, making this a flexible and more complex relationship model.