@warp-drive/schema-record 0.0.0 → 0.0.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.
Files changed (54) hide show
  1. package/LICENSE.md +19 -8
  2. package/README.md +211 -123
  3. package/dist/-private.js +1 -0
  4. package/dist/-private.js.map +1 -0
  5. package/dist/{record.js → index.js} +646 -98
  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 +29 -55
  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} +14 -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 +16 -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,43 @@
1
1
  <p align="center">
2
2
  <img
3
3
  class="project-logo"
4
- src="./NCC-1701-a-blue.svg#gh-light-mode-only"
4
+ src="./logos/NCC-1701-a-blue.svg#gh-light-mode-only"
5
5
  alt="WarpDrive"
6
6
  width="120px"
7
7
  title="WarpDrive" />
8
8
  <img
9
9
  class="project-logo"
10
- src="./NCC-1701-a.svg#gh-dark-mode-only"
10
+ src="./logos/NCC-1701-a.svg#gh-dark-mode-only"
11
11
  alt="WarpDrive"
12
12
  width="120px"
13
13
  title="WarpDrive" />
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
19
  SchemaRecord is:
20
+
20
21
  - ⚡️ Fast
21
22
  - 📦 Tiny
22
23
  - ✨ Optimized
23
24
  - 🚀 Scalable
24
- - :electron: Universal
25
-
26
- Never write a Model again.
25
+ - ⚛️ Universal
26
+ - ☢️ Reactive
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
+ This package provides reactive capabilities for your resource data.
29
+ It works together with a [*Warp***Drive**](https://github.com/emberjs/data/)
30
+ [Cache](https://github.com/emberjs/data/blob/main/packages/core-types/src/cache.ts)
31
+ and associated Schemas to simplify the most complex parts of your
32
+ app's state management.
29
33
 
30
34
  ## Installation
31
35
 
36
+ Install using your javascript package manager of choice. For instance
37
+ with [pnpm](https://pnpm.io/)
38
+
32
39
  ```cli
33
- pnpm install @warp-drive/schema-record
40
+ pnpm add @warp-drive/schema-record
34
41
  ```
35
42
 
36
43
  **Tagged Releases**
@@ -42,166 +49,247 @@ pnpm install @warp-drive/schema-record
42
49
  - ![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
50
 
44
51
 
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/schema-record
50
- ```
51
-
52
52
  ## Getting Started
53
53
 
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)
54
+ If this package is how you are first learning about WarpDrive/EmberData, we
55
+ recommend starting with learning about [Requests](https://github.com/emberjs/data/blob/main/packages/request/README.md)
56
+ and the [Store](https://github.com/emberjs/data/blob/main/packages/store/README.md).
55
57
 
56
58
  ## 🚀 Setup
57
59
 
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`.
60
+ SchemaRecord integrates with WarpDrive via the Store's resource lifecycle hooks.
61
+ When WarpDrive needs to create a new record instance to give reactive access to
62
+ a resource in the cache, it calls `instantiateRecord`. When it no longer needs
63
+ that instance, it will call `teardownRecord`.
62
64
 
63
- ```ts
65
+ ```diff
64
66
  import Store from '@ember-data/store';
65
- import SchemaRecord from '@warp-drive/schema-record';
66
- import Cache from '@ember-data/json-api';
67
+ +import { instantiateRecord, teardownRecord, registerDerivations, SchemaService } from '@warp-drive/schema-record';
67
68
 
68
- const DestroyHook = Symbol.for('destroy');
69
+ class AppStore extends Store {
69
70
 
70
- export default class extends Store {
71
- instantiateRecord(identifier) {
72
- return new SchemaRecord(this, identifier);
73
- }
71
+ + createSchemaService() {
72
+ + const schema = new SchemaService();
73
+ + registerDerivations(schema);
74
+ + return schema;
75
+ + }
74
76
 
75
- teardownRecord(record: SchemaRecord): void {
76
- record[DestroyHook]();
77
- }
77
+ + instantiateRecord(identifier, createArgs) {
78
+ + return instantiateRecord(this, identifier, createArgs);
79
+ + }
80
+
81
+ + teardownRecord(record) {
82
+ + return teardownRecord(record);
83
+ + }
78
84
  }
79
85
  ```
80
86
 
81
- ## Start Using
82
-
83
- Any Store method that returns records will use SchemaRecord once configured as above.
87
+ Any Store API that returns a record instance will use the `instantiateRecord`
88
+ hook configured above to instantiate a SchemaRecord once this is in place.
84
89
  After that, its up to you what SchemaRecord can do.
85
90
 
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
91
+ ## Start Using
103
92
 
104
- SchemaRecord is Immutable. This means by design you cannot mutate a SchemaRecord instance.
93
+ ### About
105
94
 
106
- How then do you make edits and preserve changes?
95
+ SchemaRecord is a reactive object that transforms raw data from an associated
96
+ cache into reactive data backed by Signals.
107
97
 
108
- ### Mutation Workflows
98
+ The shape of the object and the transformation of raw cache data into its
99
+ reactive form is controlled by a resource schema.
109
100
 
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.
101
+ For instance, lets say your API is a [JSON:API](https://jsonapi.org) and your store is using the
102
+ JSONAPICache, and a request returns the following raw data:
113
103
 
114
104
  ```ts
115
- const fork = await store.fork();
105
+ {
106
+ data: {
107
+ type: 'user',
108
+ id: '1',
109
+ attributes: { firstName: 'Chris', lastName: 'Thoburn' },
110
+ relationships: { pets: { data: [{ type: 'dog', id: '1' }] }}
111
+ },
112
+ included: [
113
+ {
114
+ type: 'dog',
115
+ id: '1',
116
+ attributes: { name: 'Rey' },
117
+ relationships: { owner: { data: { type: 'user', id: '1' }}}
118
+ }
119
+ ]
120
+ }
116
121
  ```
117
122
 
118
- Forks are not themselves editable, they are just a pre-requisite.
119
- There are three ways to get an editable SchemaRecord instance.
120
-
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.
123
+ We could describe the `'user'` and `'dog'` resources in the above payload
124
+ with the following schemas:
127
125
 
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.
126
+ ```ts
127
+ store.registerSchemas([
128
+ {
129
+ type: 'user',
130
+ identity: { type: '@id', name: 'id' },
131
+ fields: [
132
+ {
133
+ type: '@identity',
134
+ name: '$type',
135
+ kind: 'derived',
136
+ options: { key: 'type' },
137
+ },
138
+ { kind: 'field', name: 'firstName' },
139
+ { kind: 'field', name: 'lastName' },
140
+ {
141
+ kind: 'derived',
142
+ name: 'name',
143
+ type: 'concat',
144
+ options: { fields: ['firstName', 'lastName'], separator: ' ' }
145
+ },
146
+ {
147
+ kind: 'hasMany',
148
+ name: 'pets',
149
+ type: 'pet',
150
+ options: {
151
+ async: false,
152
+ inverse: 'owner',
153
+ polymorphic: true
154
+ }
155
+ }
156
+ ]
157
+ },
158
+ {
159
+ type: 'dog',
160
+ identity: { type: '@id', name: 'id' },
161
+ fields: [
162
+ {
163
+ type: '@identity',
164
+ name: '$type',
165
+ kind: 'derived',
166
+ options: { key: 'type' },
167
+ },
168
+ { kind: 'field', name: 'name' },
169
+ {
170
+ kind: 'belongsTo',
171
+ name: 'owner',
172
+ type: 'user',
173
+ options: {
174
+ async: false,
175
+ inverse: 'pets',
176
+ as: 'pet',
177
+ }
178
+ }
179
+ ]
180
+ }
181
+ ]);
182
+ ```
131
183
 
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.
184
+ With these schemas in place, the reactive objects that the store would
185
+ provide us whenever we encountered a `'user'` or a `'dog'` would be:
136
186
 
137
187
  ```ts
138
- // get a fork for editing
139
- const fork = await store.fork();
140
188
 
141
- // create a new record
142
- const user = fork.createRecord('user', { name: 'Chris' });
143
-
144
- // save the record
145
- await fork.request(createRecord(user));
189
+ interface Pet {
190
+ readonly id: string;
191
+ readonly owner: User;
192
+ }
146
193
 
147
- // reflect the changes back to the original store
148
- await store.merge(fork);
149
- ```
194
+ interface Dog extends Pet {
195
+ readonly $type: 'dog';
196
+ readonly name: string;
197
+ }
150
198
 
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.
199
+ interface EditableUser {
200
+ readonly $type: 'user';
201
+ readonly id: string;
202
+ firstName: string;
203
+ lastName: string;
204
+ readonly name: string;
205
+ pets: Array<Dog | Pet>;
206
+ }
155
207
 
156
- ### Optimistic UX
208
+ interface User {
209
+ readonly $type: 'user';
210
+ readonly id: string;
211
+ readonly firstName: string;
212
+ readonly lastName: string;
213
+ readonly name: string;
214
+ readonly pets: Readonly<Array<Dog | Pet>>;
215
+ [Checkout]: Promise<EditableUser>
216
+ }>
217
+ ```
157
218
 
158
- ```ts
159
- // get a fork for editing
160
- const fork = await store.fork();
219
+ Note how based on the schema the reactive object we receive is able to produce
220
+ `name` on user (despite no name field being in the cache), provide `$type`
221
+ pulled from the identity of the resource, and flatten the individual attributes
222
+ and relationships onto the record for easier use.
161
223
 
162
- // create a new record
163
- const user = fork.createRecord('user', { name: 'Chris' });
224
+ Notice also how we typed this object with `readonly`. This is because while
225
+ SchemaRecord instances are ***deeply reactive***, they are also ***immutable***.
164
226
 
165
- // reflect the (dirty) changes back to the original store
166
- await store.merge(fork);
227
+ We can mutate a SchemaRecord only be explicitly asking permission to do so, and
228
+ in the process gaining access to an editable copy. The immutable version will
229
+ not show any in-process edits made to this editable copy.
167
230
 
168
- // save the record
169
- await fork.request(createRecord(user));
231
+ ```ts
232
+ import { Checkout } from '@warp-drive/schema-record';
170
233
 
171
- // reflect the (clean) changes back to the original store
172
- await store.merge(fork);
234
+ const editable = await user[Checkout]();
173
235
  ```
174
236
 
175
- ## Schema Format
237
+ ### Utilities
176
238
 
177
- The schema format is the array representation of a Map structure. From which
178
- we will populate or append to a Map!
239
+ SchemaRecord provides a schema builder that simplifies setting up a couple of
240
+ conventional fields like identity and `$type`. We can rewrite the schema
241
+ definition above using this utility like so:
179
242
 
180
243
  ```ts
181
- [
182
- [ 'user', <user-schema> ],
183
- [ 'company', <company-schema> ],
184
- ]
244
+ import { withDefaults } from '@warp-drive/schema-record';
245
+
246
+ store.registerSchemas([
247
+ withDefaults({
248
+ type: 'user',
249
+ fields: [
250
+ { kind: 'field', name: 'firstName' },
251
+ { kind: 'field', name: 'lastName' },
252
+ {
253
+ kind: 'derived',
254
+ name: 'name',
255
+ type: 'concat',
256
+ options: { fields: ['firstName', 'lastName'], separator: ' ' }
257
+ },
258
+ {
259
+ kind: 'hasMany',
260
+ name: 'pets',
261
+ type: 'pet',
262
+ options: {
263
+ async: false,
264
+ inverse: 'owner',
265
+ polymorphic: true
266
+ }
267
+ }
268
+ ]
269
+ }),
270
+ withDefaults({
271
+ type: 'dog',
272
+ fields: [
273
+ { kind: 'field', name: 'name' },
274
+ {
275
+ kind: 'belongsTo',
276
+ name: 'owner',
277
+ type: 'user',
278
+ options: {
279
+ async: false,
280
+ inverse: 'pets',
281
+ as: 'pet',
282
+ }
283
+ }
284
+ ]
285
+ })
286
+ ]);
185
287
  ```
186
288
 
187
- It follows this signature:
188
289
 
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
- ```
290
+ ### Field Schemas
202
291
 
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.
292
+ For the full range of available schema capabilities, see [Field Schemas](../core-types/src/schema/fields.ts)
205
293
 
206
294
 
207
295
  ### ♥️ 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":""}