@warp-drive-mirror/schema-record 0.0.1 → 0.0.3

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 (54) hide show
  1. package/LICENSE.md +19 -8
  2. package/README.md +231 -129
  3. package/dist/-private.js +1 -0
  4. package/dist/-private.js.map +1 -0
  5. package/dist/{record.js → index.js} +700 -99
  6. package/dist/index.js.map +1 -0
  7. package/dist/symbols-DqoS4ybV.js.map +1 -1
  8. package/logos/NCC-1701-a-gold.svg +4 -0
  9. package/logos/NCC-1701-a-gold_100.svg +1 -0
  10. package/logos/NCC-1701-a-gold_base-64.txt +1 -0
  11. package/logos/README.md +4 -0
  12. package/logos/docs-badge.svg +2 -0
  13. package/logos/ember-data-logo-dark.svg +12 -0
  14. package/logos/ember-data-logo-light.svg +12 -0
  15. package/logos/github-header.svg +444 -0
  16. package/logos/social1.png +0 -0
  17. package/logos/social2.png +0 -0
  18. package/logos/warp-drive-logo-dark.svg +4 -0
  19. package/logos/warp-drive-logo-gold.svg +4 -0
  20. package/package.json +32 -58
  21. package/unstable-preview-types/-private/{compute.d.ts → fields/compute.d.ts} +17 -15
  22. package/unstable-preview-types/-private/fields/compute.d.ts.map +1 -0
  23. package/unstable-preview-types/-private/{managed-array.d.ts → fields/managed-array.d.ts} +4 -4
  24. package/unstable-preview-types/-private/fields/managed-array.d.ts.map +1 -0
  25. package/unstable-preview-types/-private/{managed-object.d.ts → fields/managed-object.d.ts} +4 -4
  26. package/unstable-preview-types/-private/fields/managed-object.d.ts.map +1 -0
  27. package/unstable-preview-types/-private/fields/many-array-manager.d.ts +23 -0
  28. package/unstable-preview-types/-private/fields/many-array-manager.d.ts.map +1 -0
  29. package/unstable-preview-types/{hooks.d.ts → -private/hooks.d.ts} +3 -3
  30. package/unstable-preview-types/-private/hooks.d.ts.map +1 -0
  31. package/unstable-preview-types/{record.d.ts → -private/record.d.ts} +3 -3
  32. package/unstable-preview-types/-private/record.d.ts.map +1 -0
  33. package/unstable-preview-types/{schema.d.ts → -private/schema.d.ts} +61 -11
  34. package/unstable-preview-types/-private/schema.d.ts.map +1 -0
  35. package/unstable-preview-types/{symbols.d.ts → -private/symbols.d.ts} +1 -1
  36. package/unstable-preview-types/-private/symbols.d.ts.map +1 -0
  37. package/unstable-preview-types/-private.d.ts +4 -0
  38. package/unstable-preview-types/-private.d.ts.map +1 -0
  39. package/unstable-preview-types/index.d.ts +353 -7
  40. package/unstable-preview-types/index.d.ts.map +1 -0
  41. package/dist/hooks.js +0 -19
  42. package/dist/hooks.js.map +0 -1
  43. package/dist/record.js.map +0 -1
  44. package/dist/schema.js +0 -278
  45. package/dist/schema.js.map +0 -1
  46. package/unstable-preview-types/-private/compute.d.ts.map +0 -1
  47. package/unstable-preview-types/-private/managed-array.d.ts.map +0 -1
  48. package/unstable-preview-types/-private/managed-object.d.ts.map +0 -1
  49. package/unstable-preview-types/hooks.d.ts.map +0 -1
  50. package/unstable-preview-types/record.d.ts.map +0 -1
  51. package/unstable-preview-types/schema.d.ts.map +0 -1
  52. package/unstable-preview-types/symbols.d.ts.map +0 -1
  53. /package/{NCC-1701-a-blue.svg → logos/NCC-1701-a-blue.svg} +0 -0
  54. /package/{NCC-1701-a.svg → logos/NCC-1701-a.svg} +0 -0
package/LICENSE.md CHANGED
@@ -1,12 +1,23 @@
1
- The MIT License (MIT)
1
+ MIT License
2
2
 
3
- Copyright (C) 2023 EmberData and WarpDrive contributors
4
- Copyright (C) 2017-2022 Ember.js contributors
5
- Portions Copyright (C) 2011-2017 Tilde, Inc. and contributors.
6
- Portions Copyright (C) 2011 LivingSocial Inc.
3
+ Copyright (c) 2017-2025 Ember.js and contributors
4
+ Copyright (c) 2011-2017 Tilde, Inc. and contributors
5
+ Copyright (c) 2011 LivingSocial Inc.
7
6
 
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
9
13
 
10
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
11
16
 
12
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md CHANGED
@@ -1,36 +1,45 @@
1
1
  <p align="center">
2
2
  <img
3
3
  class="project-logo"
4
- src="./NCC-1701-a-blue.svg#gh-light-mode-only"
5
- alt="WarpDrive"
6
- width="120px"
7
- title="WarpDrive" />
4
+ src="./logos/github-header.svg#gh-light-mode-only"
5
+ alt="WarpDrive | Boldly go where no app has gone before"
6
+ title="WarpDrive | Boldly go where no app has gone before"
7
+ />
8
8
  <img
9
9
  class="project-logo"
10
- src="./NCC-1701-a.svg#gh-dark-mode-only"
11
- alt="WarpDrive"
12
- width="120px"
13
- title="WarpDrive" />
10
+ src="./logos/github-header.svg#gh-dark-mode-only"
11
+ alt="WarpDrive | Boldly go where no app has gone before"
12
+ title="WarpDrive | Boldly go where no app has gone before"
13
+ />
14
14
  </p>
15
15
 
16
- <h3 align="center">Your data, managed.</h3>
16
+ <h3 align="center">Your Data, Managed.</h3>
17
17
  <p align="center">🌲 Get back to Nature 🐿️ Or shipping 💚</p>
18
18
 
19
- SchemaRecord is:
20
19
  - ⚡️ Fast
21
20
  - 📦 Tiny
22
21
  - ✨ Optimized
23
22
  - 🚀 Scalable
24
- - :electron: Universal
23
+ - ⚛️ Universal
24
+ - ☢️ Reactive
25
25
 
26
- Never write a Model again.
26
+ SchemaRecord is a reactive object that transforms raw data from an [associated cache](https://github.com/emberjs/data/blob/main/packages/core-types/src/cache.ts) into reactive data backed by Signals.
27
27
 
28
- This package provides presentation capabilities for your resource data. It works together with an [*Ember***Data**](https://github.com/emberjs/data/) [Cache](https://github.com/emberjs/data/blob/main/ember-data-types/cache/cache.ts) and associated Schemas to simplify the most complex parts of your state management.
28
+ The shape of the object and the transformation of raw cache data into its
29
+ reactive form is controlled by a resource schema.
30
+
31
+ Resource schemas are simple JSON, allowing them to be defined and delivered from anywhere.
32
+
33
+ The capabilities that SchemaRecord brings to [*Warp***Drive**](https://github.com/emberjs/data/)
34
+ will simplify even the most complex parts of your app's state management.
29
35
 
30
36
  ## Installation
31
37
 
38
+ Install using your javascript package manager of choice. For instance
39
+ with [pnpm](https://pnpm.io/)
40
+
32
41
  ```cli
33
- pnpm install @warp-drive-mirror/schema-record
42
+ pnpm add @warp-drive-mirror/schema-record
34
43
  ```
35
44
 
36
45
  **Tagged Releases**
@@ -42,166 +51,259 @@ pnpm install @warp-drive-mirror/schema-record
42
51
  - ![NPM LTS 4.12 Version](https://img.shields.io/npm/v/%40warp-drive/schema-record/lts-4-12?label=%40lts-4-12&color=bbbbbb)
43
52
 
44
53
 
45
- #### 🔜 Soon
46
- Install using your javascript package manager of choice. For instance with [pnpm](https://pnpm.io/)
47
-
48
- ```no-highlight
49
- pnpm add @warp-drive-mirror/schema-record
50
- ```
51
-
52
54
  ## Getting Started
53
55
 
54
- If this package is how you are first learning about EmberData, we recommend starting with learning about the [Store](https://github.com/emberjs/data/blob/main/packages/store/README.md) and [Requests](https://github.com/emberjs/data/blob/main/packages/request/README.md)
56
+ If this package is how you are first learning about WarpDrive/EmberData, we
57
+ recommend starting with learning about [Requests](https://github.com/emberjs/data/blob/main/packages/request/README.md)
58
+ and the [Store](https://github.com/emberjs/data/blob/main/packages/store/README.md).
55
59
 
56
60
  ## 🚀 Setup
57
61
 
58
- SchemaRecord integrates with EmberData via the Store's resource lifecycle hooks.
59
- When EmberData needs to create a new presentation class to pair with some resource
60
- data, it calls `instantiateRecord`. When it no longer needs that class, it will call
61
- `teardownRecord`.
62
+ SchemaRecord integrates with WarpDrive via the Store's resource lifecycle hooks.
63
+ When WarpDrive needs to create a new record instance to give reactive access to
64
+ a resource in the cache, it calls `instantiateRecord`. When it no longer needs
65
+ that instance, it will call `teardownRecord`.
62
66
 
63
- ```ts
67
+ ```diff
64
68
  import Store from '@ember-data-mirror/store';
65
- import SchemaRecord from '@warp-drive-mirror/schema-record';
66
- import Cache from '@ember-data-mirror/json-api';
69
+ +import { instantiateRecord, teardownRecord, registerDerivations, SchemaService } from '@warp-drive-mirror/schema-record';
67
70
 
68
- const DestroyHook = Symbol.for('destroy');
71
+ class AppStore extends Store {
69
72
 
70
- export default class extends Store {
71
- instantiateRecord(identifier) {
72
- return new SchemaRecord(this, identifier);
73
- }
73
+ + createSchemaService() {
74
+ + const schema = new SchemaService();
75
+ + registerDerivations(schema);
76
+ + return schema;
77
+ + }
74
78
 
75
- teardownRecord(record: SchemaRecord): void {
76
- record[DestroyHook]();
77
- }
79
+ + instantiateRecord(identifier, createArgs) {
80
+ + return instantiateRecord(this, identifier, createArgs);
81
+ + }
82
+
83
+ + teardownRecord(record) {
84
+ + return teardownRecord(record);
85
+ + }
78
86
  }
79
87
  ```
80
88
 
81
- ## Start Using
82
-
83
- Any Store method that returns records will use SchemaRecord once configured as above.
89
+ Any Store API that returns a record instance will use the `instantiateRecord`
90
+ hook configured above to instantiate a SchemaRecord once this is in place.
84
91
  After that, its up to you what SchemaRecord can do.
85
92
 
86
- SchemaRecord's behavior is driven by the Schemas you register with the Store's Schema
87
- Service. Schemas are simple json objects that follow a pattern.
88
-
89
- You could manually construct schemas, though that would be laborious. We recommend
90
- compiling schemas from another available source such as your API's types. If you don't
91
- have a source from which to compile schemas, consider using `@warp-drive/schema-dsl`.
92
-
93
- The Schema DSL allows authoring rich, expressive schemas using familiar Typescript and
94
- Decorators, which compile at build into json schemas you can deliver to your app either
95
- in your asset bundle, via a separate fetch, or from your API.
96
-
97
- The Schema DSL will also compile and register types for your schemas that give you robust
98
- typescript support.
99
-
100
- ## Main Paradigms
101
-
102
- ### Immutability
93
+ ## Start Using
103
94
 
104
- SchemaRecord is Immutable. This means by design you cannot mutate a SchemaRecord instance.
95
+ ### About
105
96
 
106
- How then do you make edits and preserve changes?
97
+ SchemaRecord is a reactive object that transforms raw data from an associated
98
+ cache into reactive data backed by Signals.
107
99
 
108
- ### Mutation Workflows
100
+ The shape of the object and the transformation of raw cache data into its
101
+ reactive form is controlled by a resource schema.
109
102
 
110
- Edits are performed in mutation workflows. A workflow is begun by forking the store.
111
- Forks are cheap copy-on-write scopes that allow you to make changes in isolation without
112
- affecting the global state of the application (until you want to). You can even fork forks, though its probably not that useful to do so in the common case.
103
+ For instance, lets say your API is a [JSON:API](https://jsonapi.org) and your store is using the
104
+ JSONAPICache, and a request returns the following raw data:
113
105
 
114
106
  ```ts
115
- const fork = await store.fork();
107
+ {
108
+ data: {
109
+ type: 'user',
110
+ id: '1',
111
+ attributes: { firstName: 'Chris', lastName: 'Thoburn' },
112
+ relationships: { pets: { data: [{ type: 'dog', id: '1' }] }}
113
+ },
114
+ included: [
115
+ {
116
+ type: 'dog',
117
+ id: '1',
118
+ attributes: { name: 'Rey' },
119
+ relationships: { owner: { data: { type: 'user', id: '1' }}}
120
+ }
121
+ ]
122
+ }
116
123
  ```
117
124
 
118
- Forks are not themselves editable, they are just a pre-requisite.
119
- There are three ways to get an editable SchemaRecord instance.
125
+ We could describe the `'user'` and `'dog'` resources in the above payload
126
+ with the following schemas:
120
127
 
121
- 1. Create a new record with `const editable = fork.createRecord(<type>, data)`
122
- 2. Checkout an existing record in edit mode: `const editable = fork.checkoutRecord(record)`
123
- 3. Access a related record on a record already in edit mode: `const editableFriend = editable.bestFriend`
124
-
125
- If you decide you want to discard your changes, there's no need to rollback. Simply
126
- dereferencing the fork and any records you've received from it will cause it to GC.
127
-
128
- However, explicitly calling `fork.deref()` will ensure that if you did forget to dereference
129
- any records and left them around somewhere as a variable, they'll blow up with a useful
130
- error if used again.
128
+ ```ts
129
+ store.schema.registerResources([
130
+ {
131
+ type: 'user',
132
+ identity: { type: '@id', name: 'id' },
133
+ fields: [
134
+ {
135
+ type: '@identity',
136
+ name: '$type',
137
+ kind: 'derived',
138
+ options: { key: 'type' },
139
+ },
140
+ { kind: 'field', name: 'firstName' },
141
+ { kind: 'field', name: 'lastName' },
142
+ {
143
+ kind: 'derived',
144
+ name: 'name',
145
+ type: 'concat',
146
+ options: { fields: ['firstName', 'lastName'], separator: ' ' }
147
+ },
148
+ {
149
+ kind: 'hasMany',
150
+ name: 'pets',
151
+ type: 'pet',
152
+ options: {
153
+ async: false,
154
+ inverse: 'owner',
155
+ polymorphic: true,
156
+ linksMode: true,
157
+ }
158
+ }
159
+ ]
160
+ },
161
+ {
162
+ type: 'dog',
163
+ identity: { type: '@id', name: 'id' },
164
+ fields: [
165
+ {
166
+ type: '@identity',
167
+ name: '$type',
168
+ kind: 'derived',
169
+ options: { key: 'type' },
170
+ },
171
+ { kind: 'field', name: 'name' },
172
+ {
173
+ kind: 'belongsTo',
174
+ name: 'owner',
175
+ type: 'user',
176
+ options: {
177
+ async: false,
178
+ inverse: 'pets',
179
+ as: 'pet',
180
+ linksMode: true,
181
+ }
182
+ }
183
+ ]
184
+ }
185
+ ]);
186
+ ```
131
187
 
132
- To save changes, call `fork.request(saveRecord(editable))`. Saving changes will only commit
133
- the changes to the fork, it won't commit them upstream. To reflect the changes upstream, call
134
- `await fork.merge(store)`. In most cases, `store` should be the store you forked from, though
135
- it is allowed to attempt to merge into a parent `store` as well.
188
+ With these schemas in place, the reactive objects that the store would
189
+ provide us whenever we encountered a `'user'` or a `'dog'` would be:
136
190
 
137
191
  ```ts
138
- // get a fork for editing
139
- const fork = await store.fork();
140
-
141
- // create a new record
142
- const user = fork.createRecord('user', { name: 'Chris' });
143
192
 
144
- // save the record
145
- await fork.request(createRecord(user));
193
+ interface Pet {
194
+ readonly id: string;
195
+ readonly owner: User;
196
+ }
146
197
 
147
- // reflect the changes back to the original store
148
- await store.merge(fork);
149
- ```
198
+ interface Dog extends Pet {
199
+ readonly $type: 'dog';
200
+ readonly name: string;
201
+ }
150
202
 
151
- > Note: merging behavior is determined by the Cache implementation. The implementations
152
- > maintained by the EmberData team will merge both persisted and unpersisted changes back
153
- > to the upstream (preserving them as remote and local state respectively). This approach
154
- > allows developers to choose to optimistically vs pessimistically update the global state.
203
+ interface EditableUser {
204
+ readonly $type: 'user';
205
+ readonly id: string;
206
+ firstName: string;
207
+ lastName: string;
208
+ readonly name: string;
209
+ pets: Array<Dog | Pet>;
210
+ }
155
211
 
156
- ### Optimistic UX
212
+ interface User {
213
+ readonly $type: 'user';
214
+ readonly id: string;
215
+ readonly firstName: string;
216
+ readonly lastName: string;
217
+ readonly name: string;
218
+ readonly pets: Readonly<Array<Dog | Pet>>;
219
+ [Checkout]: Promise<EditableUser>
220
+ }>
221
+ ```
157
222
 
158
- ```ts
159
- // get a fork for editing
160
- const fork = await store.fork();
223
+ Note how based on the schema the reactive object we receive is able to produce
224
+ `name` on user (despite no name field being in the cache), provide `$type`
225
+ pulled from the identity of the resource, and flatten the individual attributes
226
+ and relationships onto the record for easier use.
161
227
 
162
- // create a new record
163
- const user = fork.createRecord('user', { name: 'Chris' });
228
+ Notice also how we typed this object with `readonly`. This is because while
229
+ SchemaRecord instances are ***deeply reactive***, they are also ***immutable***.
164
230
 
165
- // reflect the (dirty) changes back to the original store
166
- await store.merge(fork);
231
+ We can mutate a SchemaRecord only be explicitly asking permission to do so, and
232
+ in the process gaining access to an editable copy. The immutable version will
233
+ not show any in-process edits made to this editable copy.
167
234
 
168
- // save the record
169
- await fork.request(createRecord(user));
235
+ ```ts
236
+ import { Checkout } from '@warp-drive-mirror/schema-record';
170
237
 
171
- // reflect the (clean) changes back to the original store
172
- await store.merge(fork);
238
+ const editable = await user[Checkout]();
173
239
  ```
174
240
 
175
- ## Schema Format
241
+ ### Utilities
176
242
 
177
- The schema format is the array representation of a Map structure. From which
178
- we will populate or append to a Map!
243
+ SchemaRecord provides a schema builder that simplifies setting up a couple of
244
+ conventional fields like identity and `$type`. We can rewrite the schema
245
+ definition above using this utility like so:
179
246
 
180
247
  ```ts
181
- [
182
- [ 'user', <user-schema> ],
183
- [ 'company', <company-schema> ],
184
- ]
248
+ import { withDefaults } from '@warp-drive-mirror/schema-record';
249
+
250
+ store.schema.registerResources([
251
+ withDefaults({
252
+ type: 'user',
253
+ fields: [
254
+ { kind: 'field', name: 'firstName' },
255
+ { kind: 'field', name: 'lastName' },
256
+ {
257
+ kind: 'derived',
258
+ name: 'name',
259
+ type: 'concat',
260
+ options: { fields: ['firstName', 'lastName'], separator: ' ' }
261
+ },
262
+ {
263
+ kind: 'hasMany',
264
+ name: 'pets',
265
+ type: 'pet',
266
+ options: {
267
+ async: false,
268
+ inverse: 'owner',
269
+ polymorphic: true
270
+ }
271
+ }
272
+ ]
273
+ }),
274
+ withDefaults({
275
+ type: 'dog',
276
+ fields: [
277
+ { kind: 'field', name: 'name' },
278
+ {
279
+ kind: 'belongsTo',
280
+ name: 'owner',
281
+ type: 'user',
282
+ options: {
283
+ async: false,
284
+ inverse: 'pets',
285
+ as: 'pet',
286
+ }
287
+ }
288
+ ]
289
+ })
290
+ ]);
185
291
  ```
186
292
 
187
- It follows this signature:
293
+ Additionally, `@warp-drive-mirror/core-types` provides several utilities for type-checking and narrowing schemas.
188
294
 
189
- ```ts
190
- type ResourceType = string; // 'user'
191
- type FieldName = string; // 'name'
192
- type FieldDef = {
193
- name: string;
194
- type: string | null;
195
- kind: 'resource' | 'collection' | 'attribute' | 'derivation' | 'object' | 'array';
196
- options: Record<string, unknown>;
197
- };
198
-
199
- type ResourceSchema = Array<[FieldName, FieldDef]>
200
- type Schemas = Array<[ResourceType, ResourceSchema]>
201
- ```
295
+ - (type) [PolarisResourceSchema]()
296
+ - (type) [LegacyResourceSchema]()
297
+ - (type) [ObjectSchema]()
298
+ - [resourceSchema]()
299
+ - [objectSchema]()
300
+ - [isResourceSchema]()
301
+ - [isLegacyResourceSchema]()
302
+
303
+
304
+ ### Field Schemas
202
305
 
203
- You'll find this syntax is capable of describing most conceivable behaviors, including
204
- some emergent ones we're sure we haven't thought of yet.
306
+ For the full range of available schema capabilities, see [Field Schemas](../core-types/src/schema/fields.ts)
205
307
 
206
308
 
207
309
  ### ♥️ Credits
@@ -0,0 +1 @@
1
+ export { E as Editable, L as Legacy } from "./symbols-DqoS4ybV.js";
@@ -0,0 +1 @@
1
+ {"version":3,"file":"-private.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}