Skip to content

Commit 4e77602

Browse files
kevin-dpsamwillis
andauthored
Add optional compareOptions to the Collection Config which act as col… (#762)
* Add optional compareOptions to the Collection Config which act as collection-wide config and allow queries to override those. * Fix import * changeset * Ran prettier * update docs * Live queries inherit compare options from collection in the FROM clause * Explicit compare options for live queries * Rename compareOptions to defaultStringCollation in the collection config and live query collection config * Docs update --------- Co-authored-by: Sam Willis <sam.willis@gmail.com>
1 parent 0ac7270 commit 4e77602

86 files changed

Lines changed: 1135 additions & 425 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/all-meals-follow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/db": patch
3+
---
4+
5+
Add optional compareOptions to collection configuration.

docs/reference/classes/CollectionImpl.md

Lines changed: 61 additions & 45 deletions
Large diffs are not rendered by default.

docs/reference/functions/createCollection.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ title: createCollection
1111
function createCollection<T, TKey, TUtils>(options): Collection<InferSchemaOutput<T>, TKey, TUtils, T, InferSchemaInput<T>> & NonSingleResult;
1212
```
1313

14-
Defined in: [packages/db/src/collection/index.ts:130](https://github.com/TanStack/db/blob/main/packages/db/src/collection/index.ts#L130)
14+
Defined in: [packages/db/src/collection/index.ts:131](https://github.com/TanStack/db/blob/main/packages/db/src/collection/index.ts#L131)
1515

1616
Creates a new Collection instance with the given configuration
1717

@@ -120,7 +120,7 @@ const todos = createCollection({
120120
function createCollection<T, TKey, TUtils>(options): Collection<InferSchemaOutput<T>, TKey, TUtils, T, InferSchemaInput<T>> & SingleResult;
121121
```
122122

123-
Defined in: [packages/db/src/collection/index.ts:143](https://github.com/TanStack/db/blob/main/packages/db/src/collection/index.ts#L143)
123+
Defined in: [packages/db/src/collection/index.ts:144](https://github.com/TanStack/db/blob/main/packages/db/src/collection/index.ts#L144)
124124

125125
Creates a new Collection instance with the given configuration
126126

@@ -229,7 +229,7 @@ const todos = createCollection({
229229
function createCollection<T, TKey, TUtils>(options): Collection<T, TKey, TUtils, never, T> & NonSingleResult;
230230
```
231231

232-
Defined in: [packages/db/src/collection/index.ts:157](https://github.com/TanStack/db/blob/main/packages/db/src/collection/index.ts#L157)
232+
Defined in: [packages/db/src/collection/index.ts:158](https://github.com/TanStack/db/blob/main/packages/db/src/collection/index.ts#L158)
233233

234234
Creates a new Collection instance with the given configuration
235235

@@ -338,7 +338,7 @@ const todos = createCollection({
338338
function createCollection<T, TKey, TUtils>(options): Collection<T, TKey, TUtils, never, T> & SingleResult;
339339
```
340340

341-
Defined in: [packages/db/src/collection/index.ts:170](https://github.com/TanStack/db/blob/main/packages/db/src/collection/index.ts#L170)
341+
Defined in: [packages/db/src/collection/index.ts:171](https://github.com/TanStack/db/blob/main/packages/db/src/collection/index.ts#L171)
342342

343343
Creates a new Collection instance with the given configuration
344344

docs/reference/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ title: "@tanstack/db"
110110
- [ChangeMessage](../interfaces/ChangeMessage.md)
111111
- [Collection](../interfaces/Collection.md)
112112
- [CollectionConfig](../interfaces/CollectionConfig.md)
113+
- [CollectionLike](../interfaces/CollectionLike.md)
113114
- [Context](../interfaces/Context.md)
114115
- [CreateOptimisticActionsOptions](../interfaces/CreateOptimisticActionsOptions.md)
115116
- [CurrentStateAsChangesOptions](../interfaces/CurrentStateAsChangesOptions.md)
@@ -193,6 +194,7 @@ title: "@tanstack/db"
193194
- [StorageEventApi](../type-aliases/StorageEventApi.md)
194195
- [Strategy](../type-aliases/Strategy.md)
195196
- [StrategyOptions](../type-aliases/StrategyOptions.md)
197+
- [StringCollationConfig](../type-aliases/StringCollationConfig.md)
196198
- [SubscriptionEvents](../type-aliases/SubscriptionEvents.md)
197199
- [SubscriptionStatus](../type-aliases/SubscriptionStatus.md)
198200
- [SyncConfigRes](../type-aliases/SyncConfigRes.md)

docs/reference/interfaces/BaseCollectionConfig.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: BaseCollectionConfig
55

66
# Interface: BaseCollectionConfig\<T, TKey, TSchema, TUtils, TReturn\>
77

8-
Defined in: [packages/db/src/types.ts:385](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L385)
8+
Defined in: [packages/db/src/types.ts:416](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L416)
99

1010
## Extended by
1111

@@ -42,7 +42,7 @@ Defined in: [packages/db/src/types.ts:385](https://github.com/TanStack/db/blob/m
4242
optional autoIndex: "eager" | "off";
4343
```
4444

45-
Defined in: [packages/db/src/types.ts:434](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L434)
45+
Defined in: [packages/db/src/types.ts:465](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L465)
4646

4747
Auto-indexing mode for the collection.
4848
When enabled, indexes will be automatically created for simple where expressions.
@@ -66,7 +66,7 @@ When enabled, indexes will be automatically created for simple where expressions
6666
optional compare: (x, y) => number;
6767
```
6868

69-
Defined in: [packages/db/src/types.ts:445](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L445)
69+
Defined in: [packages/db/src/types.ts:476](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L476)
7070

7171
Optional function to compare two items.
7272
This is used to order the items in the collection.
@@ -100,13 +100,28 @@ compare: (x, y) => x.createdAt.getTime() - y.createdAt.getTime()
100100

101101
***
102102

103+
### defaultStringCollation?
104+
105+
```ts
106+
optional defaultStringCollation: StringCollationConfig;
107+
```
108+
109+
Defined in: [packages/db/src/types.ts:622](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L622)
110+
111+
Specifies how to compare data in the collection.
112+
This should be configured to match data ordering on the backend.
113+
E.g., when using the Electric DB collection these options
114+
should match the database's collation settings.
115+
116+
***
117+
103118
### gcTime?
104119

105120
```ts
106121
optional gcTime: number;
107122
```
108123

109-
Defined in: [packages/db/src/types.ts:414](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L414)
124+
Defined in: [packages/db/src/types.ts:445](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L445)
110125

111126
Time in milliseconds after which the collection will be garbage collected
112127
when it has no active subscribers. Defaults to 5 minutes (300000ms).
@@ -119,7 +134,7 @@ when it has no active subscribers. Defaults to 5 minutes (300000ms).
119134
getKey: (item) => TKey;
120135
```
121136

122-
Defined in: [packages/db/src/types.ts:409](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L409)
137+
Defined in: [packages/db/src/types.ts:440](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L440)
123138

124139
Function to extract the ID from an object
125140
This is required for update/delete operations which now only accept IDs
@@ -153,7 +168,7 @@ getKey: (item) => item.uuid
153168
optional id: string;
154169
```
155170

156-
Defined in: [packages/db/src/types.ts:398](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L398)
171+
Defined in: [packages/db/src/types.ts:429](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L429)
157172

158173
***
159174

@@ -163,7 +178,7 @@ Defined in: [packages/db/src/types.ts:398](https://github.com/TanStack/db/blob/m
163178
optional onDelete: DeleteMutationFn<T, TKey, TUtils, TReturn>;
164179
```
165180

166-
Defined in: [packages/db/src/types.ts:583](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L583)
181+
Defined in: [packages/db/src/types.ts:614](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L614)
167182

168183
Optional asynchronous handler function called before a delete operation
169184

@@ -227,7 +242,7 @@ onDelete: async ({ transaction, collection }) => {
227242
optional onInsert: InsertMutationFn<T, TKey, TUtils, TReturn>;
228243
```
229244

230-
Defined in: [packages/db/src/types.ts:496](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L496)
245+
Defined in: [packages/db/src/types.ts:527](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L527)
231246

232247
Optional asynchronous handler function called before an insert operation
233248

@@ -290,7 +305,7 @@ onInsert: async ({ transaction, collection }) => {
290305
optional onUpdate: UpdateMutationFn<T, TKey, TUtils, TReturn>;
291306
```
292307

293-
Defined in: [packages/db/src/types.ts:540](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L540)
308+
Defined in: [packages/db/src/types.ts:571](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L571)
294309

295310
Optional asynchronous handler function called before an update operation
296311

@@ -354,7 +369,7 @@ onUpdate: async ({ transaction, collection }) => {
354369
optional schema: TSchema;
355370
```
356371

357-
Defined in: [packages/db/src/types.ts:399](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L399)
372+
Defined in: [packages/db/src/types.ts:430](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L430)
358373

359374
***
360375

@@ -364,7 +379,7 @@ Defined in: [packages/db/src/types.ts:399](https://github.com/TanStack/db/blob/m
364379
optional startSync: boolean;
365380
```
366381

367-
Defined in: [packages/db/src/types.ts:425](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L425)
382+
Defined in: [packages/db/src/types.ts:456](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L456)
368383

369384
Whether to eagerly start syncing on collection creation.
370385
When true, syncing begins immediately. When false, syncing starts when the first subscriber attaches.
@@ -387,7 +402,7 @@ false
387402
optional syncMode: SyncMode;
388403
```
389404

390-
Defined in: [packages/db/src/types.ts:454](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L454)
405+
Defined in: [packages/db/src/types.ts:485](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L485)
391406

392407
The mode of sync to use for the collection.
393408

@@ -409,4 +424,4 @@ The exact implementation of the sync mode is up to the sync implementation.
409424
optional utils: TUtils;
410425
```
411426

412-
Defined in: [packages/db/src/types.ts:585](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L585)
427+
Defined in: [packages/db/src/types.ts:624](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L624)

docs/reference/interfaces/ChangeMessage.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: ChangeMessage
55

66
# Interface: ChangeMessage\<T, TKey\>
77

8-
Defined in: [packages/db/src/types.ts:261](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L261)
8+
Defined in: [packages/db/src/types.ts:292](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L292)
99

1010
## Extended by
1111

@@ -29,7 +29,7 @@ Defined in: [packages/db/src/types.ts:261](https://github.com/TanStack/db/blob/m
2929
key: TKey;
3030
```
3131

32-
Defined in: [packages/db/src/types.ts:265](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L265)
32+
Defined in: [packages/db/src/types.ts:296](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L296)
3333

3434
***
3535

@@ -39,7 +39,7 @@ Defined in: [packages/db/src/types.ts:265](https://github.com/TanStack/db/blob/m
3939
optional metadata: Record<string, unknown>;
4040
```
4141

42-
Defined in: [packages/db/src/types.ts:269](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L269)
42+
Defined in: [packages/db/src/types.ts:300](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L300)
4343

4444
***
4545

@@ -49,7 +49,7 @@ Defined in: [packages/db/src/types.ts:269](https://github.com/TanStack/db/blob/m
4949
optional previousValue: T;
5050
```
5151

52-
Defined in: [packages/db/src/types.ts:267](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L267)
52+
Defined in: [packages/db/src/types.ts:298](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L298)
5353

5454
***
5555

@@ -59,7 +59,7 @@ Defined in: [packages/db/src/types.ts:267](https://github.com/TanStack/db/blob/m
5959
type: OperationType;
6060
```
6161

62-
Defined in: [packages/db/src/types.ts:268](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L268)
62+
Defined in: [packages/db/src/types.ts:299](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L299)
6363

6464
***
6565

@@ -69,4 +69,4 @@ Defined in: [packages/db/src/types.ts:268](https://github.com/TanStack/db/blob/m
6969
value: T;
7070
```
7171

72-
Defined in: [packages/db/src/types.ts:266](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L266)
72+
Defined in: [packages/db/src/types.ts:297](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L297)

0 commit comments

Comments
 (0)