@tldraw/store 5.4.0-canary.2cc059276813 → 5.4.0-canary.2fdc29a83f08
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/DOCS.md +50 -50
- package/dist-cjs/index.js +1 -1
- package/dist-esm/index.mjs +1 -1
- package/package.json +3 -3
package/DOCS.md
CHANGED
|
@@ -17,9 +17,9 @@ The store provides:
|
|
|
17
17
|
- **Side effects** - Lifecycle hooks for implementing business logic
|
|
18
18
|
- **Queries** - Reactive indexes and filtering for efficient data access
|
|
19
19
|
|
|
20
|
-
## 2. Core
|
|
20
|
+
## 2. Core concepts
|
|
21
21
|
|
|
22
|
-
### Records:
|
|
22
|
+
### Records: the foundation
|
|
23
23
|
|
|
24
24
|
**Records** are immutable data objects that extend the `BaseRecord` interface. Every record has an `id` and a `typeName` that identifies its type:
|
|
25
25
|
|
|
@@ -34,7 +34,7 @@ interface Book extends BaseRecord<'book', RecordId<Book>> {
|
|
|
34
34
|
}
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
### Record
|
|
37
|
+
### Record types: factories for records
|
|
38
38
|
|
|
39
39
|
A **RecordType** is a factory that creates and manages records of a specific type. You define how records are created, validated, and what their default properties are:
|
|
40
40
|
|
|
@@ -52,9 +52,9 @@ const Book = createRecordType<Book>('book', {
|
|
|
52
52
|
|
|
53
53
|
// Create a new book
|
|
54
54
|
|
|
55
|
-
## 2. Core
|
|
55
|
+
## 2. Core concepts
|
|
56
56
|
|
|
57
|
-
### Records:
|
|
57
|
+
### Records: the foundation
|
|
58
58
|
|
|
59
59
|
**Records** are immutable data objects that extend the `BaseRecord` interface. Every record has an `id` and a `typeName` that identifies its type:
|
|
60
60
|
|
|
@@ -73,7 +73,7 @@ interface Book extends BaseRecord<'book', RecordId<Book>> {
|
|
|
73
73
|
}
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
### Record
|
|
76
|
+
### Record types: factories for records
|
|
77
77
|
|
|
78
78
|
A **RecordType** is a factory that creates and manages records of a specific type. You define how records are created, validated, and what their default properties are:
|
|
79
79
|
|
|
@@ -100,9 +100,9 @@ const book = Book.create({
|
|
|
100
100
|
// Results in: { id: 'book:abc123', typeName: 'book', title: '1984', authorId: 'author:xyz789', publishedYear: 2025, inStock: true }
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
-
## 3. Basic
|
|
103
|
+
## 3. Basic usage
|
|
104
104
|
|
|
105
|
-
### Creating and
|
|
105
|
+
### Creating and storing records
|
|
106
106
|
|
|
107
107
|
You add records to the store using the `put` method:
|
|
108
108
|
|
|
@@ -123,7 +123,7 @@ store.put([orwell, huxley, ...books])
|
|
|
123
123
|
|
|
124
124
|
> Tip: The `put` method handles both creating new records and updating existing ones. If a record with the same ID already exists, it will be updated.
|
|
125
125
|
|
|
126
|
-
### Reading
|
|
126
|
+
### Reading records
|
|
127
127
|
|
|
128
128
|
You can read individual records or access all records of a type:
|
|
129
129
|
|
|
@@ -139,7 +139,7 @@ const allRecords = store.allRecords()
|
|
|
139
139
|
const hasBook = store.has(books[0].id) // true
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
-
### Updating
|
|
142
|
+
### Updating records
|
|
143
143
|
|
|
144
144
|
Use the `update` method to modify existing records:
|
|
145
145
|
|
|
@@ -153,7 +153,7 @@ store.update(book.id, (currentBook) => ({
|
|
|
153
153
|
|
|
154
154
|
The update function receives the current record and returns a new record with your changes applied.
|
|
155
155
|
|
|
156
|
-
### Removing
|
|
156
|
+
### Removing records
|
|
157
157
|
|
|
158
158
|
Remove records using their IDs:
|
|
159
159
|
|
|
@@ -165,9 +165,9 @@ store.remove([book.id])
|
|
|
165
165
|
store.remove([book1.id, book2.id, book3.id])
|
|
166
166
|
```
|
|
167
167
|
|
|
168
|
-
## 4. Reactive
|
|
168
|
+
## 4. Reactive queries and indexing
|
|
169
169
|
|
|
170
|
-
### Understanding
|
|
170
|
+
### Understanding reactive indexes
|
|
171
171
|
|
|
172
172
|
The store automatically maintains **reactive indexes** that allow efficient querying of your data. These indexes update automatically when records change:
|
|
173
173
|
|
|
@@ -183,7 +183,7 @@ console.log(orwellBooks) // Set<RecordId<Book>>
|
|
|
183
183
|
|
|
184
184
|
The index returns a `Map` where keys are property values and values are `Set`s of record IDs that have that property value.
|
|
185
185
|
|
|
186
|
-
### Reactive
|
|
186
|
+
### Reactive queries with computed values
|
|
187
187
|
|
|
188
188
|
Combine indexes with computed values to create reactive queries:
|
|
189
189
|
|
|
@@ -210,7 +210,7 @@ const inStockBooksByAuthor = computed('inStockBooksByAuthor', () => {
|
|
|
210
210
|
console.log(inStockBooksByAuthor.get()) // Map<RecordId<Author>, Book[]>
|
|
211
211
|
```
|
|
212
212
|
|
|
213
|
-
### The
|
|
213
|
+
### The store: your reactive database
|
|
214
214
|
|
|
215
215
|
The **Store** is the central container that manages all your records. It provides reactive access to data and automatically tracks changes:
|
|
216
216
|
|
|
@@ -230,9 +230,9 @@ const store = new Store({
|
|
|
230
230
|
})
|
|
231
231
|
```
|
|
232
232
|
|
|
233
|
-
## 3. Basic
|
|
233
|
+
## 3. Basic usage
|
|
234
234
|
|
|
235
|
-
### Creating and
|
|
235
|
+
### Creating and storing records
|
|
236
236
|
|
|
237
237
|
You add records to the store using the `put` method:
|
|
238
238
|
|
|
@@ -250,7 +250,7 @@ store.put(books)
|
|
|
250
250
|
|
|
251
251
|
> Tip: The `put` method handles both creating new records and updating existing ones. If a record with the same ID already exists, it will be updated.
|
|
252
252
|
|
|
253
|
-
### Reading
|
|
253
|
+
### Reading records
|
|
254
254
|
|
|
255
255
|
You can read individual records or access all records of a type:
|
|
256
256
|
|
|
@@ -266,7 +266,7 @@ const allRecords = store.allRecords()
|
|
|
266
266
|
const hasBook = store.has(books[0].id) // true
|
|
267
267
|
```
|
|
268
268
|
|
|
269
|
-
### Updating
|
|
269
|
+
### Updating records
|
|
270
270
|
|
|
271
271
|
Use the `update` method to modify existing records:
|
|
272
272
|
|
|
@@ -280,7 +280,7 @@ store.update(book.id, (currentBook) => ({
|
|
|
280
280
|
|
|
281
281
|
The update function receives the current record and returns a new record with your changes applied.
|
|
282
282
|
|
|
283
|
-
### Removing
|
|
283
|
+
### Removing records
|
|
284
284
|
|
|
285
285
|
Remove records using their IDs:
|
|
286
286
|
|
|
@@ -292,9 +292,9 @@ store.remove([book.id])
|
|
|
292
292
|
store.remove([book1.id, book2.id, book3.id])
|
|
293
293
|
```
|
|
294
294
|
|
|
295
|
-
## 4. Reactive
|
|
295
|
+
## 4. Reactive queries and indexing
|
|
296
296
|
|
|
297
|
-
### Understanding
|
|
297
|
+
### Understanding reactive indexes
|
|
298
298
|
|
|
299
299
|
The store automatically maintains **reactive indexes** that allow efficient querying of your data. These indexes update automatically when records change:
|
|
300
300
|
|
|
@@ -309,7 +309,7 @@ console.log(orwellBooks) // Set<RecordId<Book>>
|
|
|
309
309
|
|
|
310
310
|
The index returns a `Map` where keys are property values and values are `Set`s of record IDs that have that property value.
|
|
311
311
|
|
|
312
|
-
### Reactive
|
|
312
|
+
### Reactive queries with computed values
|
|
313
313
|
|
|
314
314
|
Combine indexes with computed values to create reactive queries:
|
|
315
315
|
|
|
@@ -336,7 +336,7 @@ const inStockBooksByAuthor = computed('inStockBooksByAuthor', () => {
|
|
|
336
336
|
console.log(inStockBooksByAuthor.get()) // Map<string, Book[]>
|
|
337
337
|
```
|
|
338
338
|
|
|
339
|
-
### Filtering
|
|
339
|
+
### Filtering history for specific record types
|
|
340
340
|
|
|
341
341
|
You can create reactive computations that track changes to specific record types:
|
|
342
342
|
|
|
@@ -358,9 +358,9 @@ const dispose = react('book-changes', () => {
|
|
|
358
358
|
|
|
359
359
|
> Tip: The `filterHistory` method returns a `Computed` that tracks changes to records of a specific type. Use it with `react()` from `@tldraw/state` to respond to changes.
|
|
360
360
|
|
|
361
|
-
## 5. Record
|
|
361
|
+
## 5. Record scopes and persistence
|
|
362
362
|
|
|
363
|
-
### Understanding
|
|
363
|
+
### Understanding record scopes
|
|
364
364
|
|
|
365
365
|
Records have different **scopes** that determine how they're persisted and synchronized:
|
|
366
366
|
|
|
@@ -382,7 +382,7 @@ const PresenceRecord = createRecordType<PresenceData>('presence', {
|
|
|
382
382
|
- **`session`** - Per-instance data that might be saved locally
|
|
383
383
|
- **`presence`** - Temporary data that's shared but not saved
|
|
384
384
|
|
|
385
|
-
### Serialization and
|
|
385
|
+
### Serialization and snapshots
|
|
386
386
|
|
|
387
387
|
You can serialize the store's data for persistence:
|
|
388
388
|
|
|
@@ -400,9 +400,9 @@ store.loadStoreSnapshot(saved)
|
|
|
400
400
|
|
|
401
401
|
> Note: The store automatically handles migrations when loading snapshots from older versions of your schema.
|
|
402
402
|
|
|
403
|
-
## 6. Side
|
|
403
|
+
## 6. Side effects and business logic
|
|
404
404
|
|
|
405
|
-
### Understanding
|
|
405
|
+
### Understanding side effects
|
|
406
406
|
|
|
407
407
|
**Side effects** are hooks that let you implement business logic in response to record changes. They run automatically when records are created, updated, or deleted:
|
|
408
408
|
|
|
@@ -431,7 +431,7 @@ store.sideEffects.registerAfterDeleteHandler('book', (book, source) => {
|
|
|
431
431
|
})
|
|
432
432
|
```
|
|
433
433
|
|
|
434
|
-
### Change
|
|
434
|
+
### Change sources
|
|
435
435
|
|
|
436
436
|
Side effects receive a `source` parameter that tells you where the change originated:
|
|
437
437
|
|
|
@@ -447,7 +447,7 @@ store.sideEffects.registerAfterCreateHandler('book', (book, source) => {
|
|
|
447
447
|
})
|
|
448
448
|
```
|
|
449
449
|
|
|
450
|
-
### Before
|
|
450
|
+
### Before handlers: validation and transformation
|
|
451
451
|
|
|
452
452
|
**Before handlers** run before changes are applied and can validate or transform the data:
|
|
453
453
|
|
|
@@ -470,9 +470,9 @@ store.sideEffects.registerBeforeCreateHandler('book', (book, source) => {
|
|
|
470
470
|
})
|
|
471
471
|
```
|
|
472
472
|
|
|
473
|
-
## 7. Schema
|
|
473
|
+
## 7. Schema evolution with migrations
|
|
474
474
|
|
|
475
|
-
### Creating
|
|
475
|
+
### Creating migration sequences
|
|
476
476
|
|
|
477
477
|
As your application evolves, you'll need to update your data structure. **Migrations** handle this automatically:
|
|
478
478
|
|
|
@@ -527,7 +527,7 @@ const schema = StoreSchema.create(
|
|
|
527
527
|
)
|
|
528
528
|
```
|
|
529
529
|
|
|
530
|
-
### Migration
|
|
530
|
+
### Migration safety
|
|
531
531
|
|
|
532
532
|
The store automatically applies migrations when loading data from older versions:
|
|
533
533
|
|
|
@@ -542,13 +542,13 @@ store.loadStoreSnapshot(oldSnapshot)
|
|
|
542
542
|
|
|
543
543
|
> Tip: Always test your migrations thoroughly with real data before deploying to production.
|
|
544
544
|
|
|
545
|
-
## 8. Advanced
|
|
545
|
+
## 8. Advanced topics
|
|
546
546
|
|
|
547
|
-
### Transactional
|
|
547
|
+
### Transactional operations
|
|
548
548
|
|
|
549
549
|
The store automatically ensures that related operations happen atomically. When you perform multiple operations in response to user actions or side effects, they are automatically grouped together for consistency and performance.
|
|
550
550
|
|
|
551
|
-
### Custom
|
|
551
|
+
### Custom computed caches
|
|
552
552
|
|
|
553
553
|
Create **computed caches** for expensive derivations that should be memoized per record:
|
|
554
554
|
|
|
@@ -564,7 +564,7 @@ const analysis = expensiveBookData.get(book.id)
|
|
|
564
564
|
|
|
565
565
|
The cache automatically updates when the underlying record changes and cleans up when records are deleted.
|
|
566
566
|
|
|
567
|
-
### Extracting
|
|
567
|
+
### Extracting changes
|
|
568
568
|
|
|
569
569
|
You can capture changes that occur within a function:
|
|
570
570
|
|
|
@@ -585,7 +585,7 @@ This is useful for implementing undo/redo systems or understanding what changed
|
|
|
585
585
|
|
|
586
586
|
The store provides several tools for understanding what's happening in your application.
|
|
587
587
|
|
|
588
|
-
### Store
|
|
588
|
+
### Store listeners
|
|
589
589
|
|
|
590
590
|
Add listeners to react to changes in your store:
|
|
591
591
|
|
|
@@ -608,7 +608,7 @@ const removeDocumentListener = store.listen(
|
|
|
608
608
|
)
|
|
609
609
|
```
|
|
610
610
|
|
|
611
|
-
### Understanding
|
|
611
|
+
### Understanding record validation
|
|
612
612
|
|
|
613
613
|
The store validates all records when they're created or updated. Validation errors provide detailed information:
|
|
614
614
|
|
|
@@ -625,7 +625,7 @@ try {
|
|
|
625
625
|
|
|
626
626
|
## 10. Integration
|
|
627
627
|
|
|
628
|
-
### Framework
|
|
628
|
+
### Framework integration
|
|
629
629
|
|
|
630
630
|
The store is framework-agnostic but integrates well with React through `@tldraw/state-react`:
|
|
631
631
|
|
|
@@ -647,7 +647,7 @@ const BookList = track(() => {
|
|
|
647
647
|
|
|
648
648
|
The `track` function automatically subscribes the component to relevant store changes.
|
|
649
649
|
|
|
650
|
-
### Persistence
|
|
650
|
+
### Persistence strategies
|
|
651
651
|
|
|
652
652
|
Implement different persistence strategies based on your needs:
|
|
653
653
|
|
|
@@ -685,9 +685,9 @@ store.mergeRemoteChanges(() => {
|
|
|
685
685
|
|
|
686
686
|
Changes merged this way are marked with `source: 'remote'` so your side effects can handle them appropriately.
|
|
687
687
|
|
|
688
|
-
## 11. Performance
|
|
688
|
+
## 11. Performance considerations
|
|
689
689
|
|
|
690
|
-
### Memory
|
|
690
|
+
### Memory management
|
|
691
691
|
|
|
692
692
|
The store uses several strategies to maintain good performance:
|
|
693
693
|
|
|
@@ -696,7 +696,7 @@ The store uses several strategies to maintain good performance:
|
|
|
696
696
|
- **Efficient indexes** - Reactive indexes use incremental updates rather than full rebuilds
|
|
697
697
|
- **Change batching** - Multiple changes are batched together before notifying listeners
|
|
698
698
|
|
|
699
|
-
### Query
|
|
699
|
+
### Query optimization
|
|
700
700
|
|
|
701
701
|
For best performance with large datasets:
|
|
702
702
|
|
|
@@ -712,7 +712,7 @@ const book = store.unsafeGetWithoutCapture(bookId) // No reactive subscription
|
|
|
712
712
|
const expensiveData = store.createComputedCache('expensive', computeExpensiveData)
|
|
713
713
|
```
|
|
714
714
|
|
|
715
|
-
### Side
|
|
715
|
+
### Side effect performance
|
|
716
716
|
|
|
717
717
|
Keep side effects fast since they run synchronously:
|
|
718
718
|
|
|
@@ -728,9 +728,9 @@ store.sideEffects.registerAfterCreateHandler('book', (book) => {
|
|
|
728
728
|
})
|
|
729
729
|
```
|
|
730
730
|
|
|
731
|
-
## 12. Common
|
|
731
|
+
## 12. Common patterns
|
|
732
732
|
|
|
733
|
-
### Repository
|
|
733
|
+
### Repository pattern
|
|
734
734
|
|
|
735
735
|
Create typed repositories for cleaner APIs:
|
|
736
736
|
|
|
@@ -754,7 +754,7 @@ class BookRepository {
|
|
|
754
754
|
}
|
|
755
755
|
```
|
|
756
756
|
|
|
757
|
-
### State
|
|
757
|
+
### State machines with records
|
|
758
758
|
|
|
759
759
|
Use records to represent state machine states:
|
|
760
760
|
|
|
@@ -775,7 +775,7 @@ store.sideEffects.registerAfterChangeHandler('orderState', (prev, next) => {
|
|
|
775
775
|
})
|
|
776
776
|
```
|
|
777
777
|
|
|
778
|
-
### Computed
|
|
778
|
+
### Computed relationships
|
|
779
779
|
|
|
780
780
|
Model relationships between records using efficient queries:
|
|
781
781
|
|
package/dist-cjs/index.js
CHANGED
|
@@ -56,7 +56,7 @@ var import_StoreSchema = require("./lib/StoreSchema");
|
|
|
56
56
|
var import_StoreSideEffects = require("./lib/StoreSideEffects");
|
|
57
57
|
(0, import_utils.registerTldrawLibraryVersion)(
|
|
58
58
|
"@tldraw/store",
|
|
59
|
-
"5.4.0-canary.
|
|
59
|
+
"5.4.0-canary.2fdc29a83f08",
|
|
60
60
|
"cjs"
|
|
61
61
|
);
|
|
62
62
|
//# sourceMappingURL=index.js.map
|
package/dist-esm/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tldraw/store",
|
|
3
3
|
"description": "tldraw infinite canvas SDK (store).",
|
|
4
|
-
"version": "5.4.0-canary.
|
|
4
|
+
"version": "5.4.0-canary.2fdc29a83f08",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "tldraw Inc.",
|
|
7
7
|
"email": "hello@tldraw.com"
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"lint": "yarn run -T tsx ../../internal/scripts/lint.ts"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@tldraw/state": "5.4.0-canary.
|
|
54
|
-
"@tldraw/utils": "5.4.0-canary.
|
|
53
|
+
"@tldraw/state": "5.4.0-canary.2fdc29a83f08",
|
|
54
|
+
"@tldraw/utils": "5.4.0-canary.2fdc29a83f08"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
57
|
"react": "^18.2.0 || ^19.2.1"
|