Skip to content

Commit 685af96

Browse files
kevin-dpsamwillis
andcommitted
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 <[email protected]>
1 parent 394e88e commit 685af96

File tree

91 files changed

+1158
-438
lines changed

Some content is hidden

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

91 files changed

+1158
-438
lines changed

.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/electric-db-collection/functions/electricCollectionOptions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ title: electricCollectionOptions
1111
function electricCollectionOptions<T>(config): CollectionConfig<InferSchemaOutput<T>, string | number, T, UtilsRecord> & object;
1212
```
1313

14-
Defined in: [packages/electric-db-collection/src/electric.ts:254](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L254)
14+
Defined in: [packages/electric-db-collection/src/electric.ts:277](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L277)
1515

1616
Creates Electric collection options for use with a standard Collection
1717

@@ -43,7 +43,7 @@ Collection options with utilities
4343
function electricCollectionOptions<T>(config): CollectionConfig<T, string | number, never, UtilsRecord> & object;
4444
```
4545

46-
Defined in: [packages/electric-db-collection/src/electric.ts:265](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L265)
46+
Defined in: [packages/electric-db-collection/src/electric.ts:288](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L288)
4747

4848
Creates Electric collection options for use with a standard Collection
4949

docs/reference/electric-db-collection/interfaces/ElectricCollectionConfig.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ title: ElectricCollectionConfig
55

66
# Interface: ElectricCollectionConfig\<T, TSchema\>
77

8-
Defined in: [packages/electric-db-collection/src/electric.ts:80](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L80)
8+
Defined in: [packages/electric-db-collection/src/electric.ts:102](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L102)
99

1010
Configuration interface for Electric collection options
1111

1212
## Extends
1313

14-
- `Omit`\<`BaseCollectionConfig`\<`T`, `string` \| `number`, `TSchema`, `UtilsRecord`, `any`\>, `"onInsert"` \| `"onUpdate"` \| `"onDelete"`\>
14+
- `Omit`\<`BaseCollectionConfig`\<`T`, `string` \| `number`, `TSchema`, `UtilsRecord`, `any`\>, `"onInsert"` \| `"onUpdate"` \| `"onDelete"` \| `"syncMode"`\>
1515

1616
## Type Parameters
1717

@@ -35,7 +35,7 @@ The schema type for validation
3535
optional onDelete: (params) => Promise<MatchingStrategy>;
3636
```
3737

38-
Defined in: [packages/electric-db-collection/src/electric.ts:185](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L185)
38+
Defined in: [packages/electric-db-collection/src/electric.ts:208](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L208)
3939

4040
Optional asynchronous handler function called before a delete operation
4141

@@ -87,7 +87,7 @@ onDelete: async ({ transaction, collection }) => {
8787
optional onInsert: (params) => Promise<MatchingStrategy>;
8888
```
8989

90-
Defined in: [packages/electric-db-collection/src/electric.ts:128](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L128)
90+
Defined in: [packages/electric-db-collection/src/electric.ts:151](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L151)
9191

9292
Optional asynchronous handler function called before an insert operation
9393

@@ -150,7 +150,7 @@ onInsert: async ({ transaction, collection }) => {
150150
optional onUpdate: (params) => Promise<MatchingStrategy>;
151151
```
152152

153-
Defined in: [packages/electric-db-collection/src/electric.ts:157](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L157)
153+
Defined in: [packages/electric-db-collection/src/electric.ts:180](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L180)
154154

155155
Optional asynchronous handler function called before an update operation
156156

@@ -203,6 +203,16 @@ onUpdate: async ({ transaction, collection }) => {
203203
shapeOptions: ShapeStreamOptions<GetExtensions<T>>;
204204
```
205205

206-
Defined in: [packages/electric-db-collection/src/electric.ts:90](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L90)
206+
Defined in: [packages/electric-db-collection/src/electric.ts:112](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L112)
207207

208208
Configuration options for the ElectricSQL ShapeStream
209+
210+
***
211+
212+
### syncMode?
213+
214+
```ts
215+
optional syncMode: ElectricSyncMode;
216+
```
217+
218+
Defined in: [packages/electric-db-collection/src/electric.ts:113](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L113)

docs/reference/electric-db-collection/interfaces/ElectricCollectionUtils.md

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

66
# Interface: ElectricCollectionUtils\<T\>
77

8-
Defined in: [packages/electric-db-collection/src/electric.ts:237](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L237)
8+
Defined in: [packages/electric-db-collection/src/electric.ts:260](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L260)
99

1010
Electric collection utilities type
1111

@@ -33,7 +33,7 @@ Electric collection utilities type
3333
awaitMatch: AwaitMatchFn<T>;
3434
```
3535

36-
Defined in: [packages/electric-db-collection/src/electric.ts:240](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L240)
36+
Defined in: [packages/electric-db-collection/src/electric.ts:263](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L263)
3737

3838
***
3939

@@ -43,4 +43,4 @@ Defined in: [packages/electric-db-collection/src/electric.ts:240](https://github
4343
awaitTxId: AwaitTxIdFn;
4444
```
4545

46-
Defined in: [packages/electric-db-collection/src/electric.ts:239](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L239)
46+
Defined in: [packages/electric-db-collection/src/electric.ts:262](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L262)

docs/reference/electric-db-collection/type-aliases/AwaitTxIdFn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ title: AwaitTxIdFn
99
type AwaitTxIdFn = (txId, timeout?) => Promise<boolean>;
1010
```
1111

12-
Defined in: [packages/electric-db-collection/src/electric.ts:224](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L224)
12+
Defined in: [packages/electric-db-collection/src/electric.ts:247](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L247)
1313

1414
Type for the awaitTxId utility function
1515

docs/reference/electric-db-collection/type-aliases/Txid.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ title: Txid
99
type Txid = number;
1010
```
1111

12-
Defined in: [packages/electric-db-collection/src/electric.ts:42](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L42)
12+
Defined in: [packages/electric-db-collection/src/electric.ts:46](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L46)
1313

1414
Type representing a transaction ID in ElectricSQL

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)

0 commit comments

Comments
 (0)