Commit 0666ccb
feat(tlschema): allow adding custom record types to the store (tldraw#8213)
Closes tldraw#7749
In order to allow SDK users to persist and synchronize domain-specific
data that doesn't fit into shapes, bindings, or assets, this PR adds
support for custom record types in the tldraw store.
Custom record types are registered via a new `records` option on
`createTLSchema` and `createTLStore`. Each custom record type specifies
a scope (document/session/presence), a validator, optional migrations,
and optional default properties. Built-in type names are guarded against
collision.
The PR also adds TypeScript module augmentation support via
`TLGlobalRecordPropsMap`, so custom record types can be included in the
`TLRecord` union.
Original branch: `ds300/custom-record-types`
Almost entirely authored by @ds300 (David Sheldrick)
https://github.com/user-attachments/assets/55d3e131-c7f1-4969-9932-1598461d7689
https://github.com/user-attachments/assets/dd531c84-6a4b-4a98-bc4d-90ea9a50711f
### Change type
- [x] `feature`
### Test plan
1. Open the "Custom records" example at localhost:5420
2. Click "+ Add marker" to create map-pin markers on the canvas
3. Verify markers render at correct positions and follow camera movement
4. Right-click a marker to remove it
- [ ] Unit tests
- [ ] End to end tests
### API changes
- Added `CustomRecordInfo` interface for configuring custom record types
- Added `createCustomRecordId()` for creating custom record IDs
- Added `createCustomRecordMigrationIds()` for creating versioned
migration IDs
- Added `createCustomRecordMigrationSequence()` for defining custom
record migrations
- Added `isCustomRecord()` type guard for checking record type
- Added `isCustomRecordId()` type guard for checking record IDs
- Added `TLGlobalRecordPropsMap` interface for module augmentation
- Added `TLDefaultRecord`, `TLCustomRecord`, `TLIndexedRecords` types
- Changed `TLRecord` to union of `TLDefaultRecord | TLCustomRecord`
- Changed `createTLSchema()` to accept `records` option
- Changed `TLStoreSchemaOptions` to accept `records` option
### Release notes
- Add support for custom record types in the tldraw store, allowing SDK
users to persist and sync domain-specific data alongside built-in
records.
### Code changes
| Section | LOC change |
| --------------- | ---------- |
| Core code | +444 / -21 |
| Automated files | +46 / -2 |
| Documentation | +244 / -0 |
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Adds new extensibility points to `createTLSchema`/`createTLStore` and
broadens the `TLRecord` type, which can affect validation/migrations and
any code assuming only built-in record types.
>
> **Overview**
> Adds support for registering **custom record types** in the tldraw
store via a new `records` option on `createTLSchema` and
`createTLStore`, including validation, scope, default properties, and
optional migrations (with a guard to prevent collisions with built-in
type names).
>
> Introduces a new `TLCustomRecord` API surface (`CustomRecordInfo`,
`createCustomRecordId`, migration helpers, and
`isCustomRecord`/`isCustomRecordId`) and updates `TLRecord` typing to
include custom records via `TLGlobalRecordPropsMap` module augmentation.
>
> Adds a new examples page (`Custom records`) demonstrating a
persisted/synced `marker` custom record rendered as a canvas overlay,
plus regenerated API reports.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
1e40a2b. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent efe342c commit 0666ccb
9 files changed
Lines changed: 735 additions & 23 deletions
File tree
- apps/examples/src/examples/data/assets/custom-records
- packages
- editor
- src/lib/config
- tlschema
- src
- records
Lines changed: 229 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
4472 | 4473 | | |
4473 | 4474 | | |
4474 | 4475 | | |
| 4476 | + | |
4475 | 4477 | | |
4476 | 4478 | | |
4477 | 4479 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| 46 | + | |
45 | 47 | | |
46 | 48 | | |
47 | 49 | | |
| |||
88 | 90 | | |
89 | 91 | | |
90 | 92 | | |
| 93 | + | |
91 | 94 | | |
92 | 95 | | |
93 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
157 | 158 | | |
158 | 159 | | |
159 | 160 | | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
160 | 172 | | |
161 | 173 | | |
162 | 174 | | |
| |||
179 | 191 | | |
180 | 192 | | |
181 | 193 | | |
182 | | - | |
| 194 | + | |
183 | 195 | | |
184 | 196 | | |
| 197 | + | |
185 | 198 | | |
186 | 199 | | |
187 | 200 | | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
188 | 209 | | |
189 | 210 | | |
190 | 211 | | |
| |||
419 | 440 | | |
420 | 441 | | |
421 | 442 | | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
422 | 449 | | |
423 | 450 | | |
424 | 451 | | |
| |||
930 | 957 | | |
931 | 958 | | |
932 | 959 | | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
933 | 963 | | |
934 | 964 | | |
935 | 965 | | |
| |||
988 | 1018 | | |
989 | 1019 | | |
990 | 1020 | | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
991 | 1024 | | |
992 | 1025 | | |
993 | 1026 | | |
| |||
1084 | 1117 | | |
1085 | 1118 | | |
1086 | 1119 | | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
1087 | 1124 | | |
1088 | 1125 | | |
1089 | 1126 | | |
| |||
1162 | 1199 | | |
1163 | 1200 | | |
1164 | 1201 | | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
1165 | 1207 | | |
1166 | 1208 | | |
1167 | 1209 | | |
| |||
1421 | 1463 | | |
1422 | 1464 | | |
1423 | 1465 | | |
1424 | | - | |
| 1466 | + | |
1425 | 1467 | | |
1426 | 1468 | | |
1427 | 1469 | | |
| |||
0 commit comments