Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

createcollection.md

119 lines3.5 KB

1---
2id: createCollection
3title: createCollection
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8# Function: createCollection()
9​
10```ts
11function createCollection<TExplicit, TKey, TUtils, TSchema, TFallback>(options): Collection<ResolveType<TExplicit, TSchema, TFallback>, TKey, TUtils, TSchema, ResolveInsertInput<TExplicit, TSchema, TFallback>>
12```
13​
14Defined in: [packages/db/src/collection.ts:160](https://github.com/TanStack/db/blob/main/packages/db/src/collection.ts#L160)
15​
16Creates a new Collection instance with the given configuration
17​
18## Type Parameters
19​
20• **TExplicit** = `unknown`
21​
22The explicit type of items in the collection (highest priority)
23​
24• **TKey** *extends* `string` \| `number` = `string` \| `number`
25​
26The type of the key for the collection
27​
28• **TUtils** *extends* [`UtilsRecord`](../../type-aliases/utilsrecord.md) = \{\}
29​
30The utilities record type
31​
32• **TSchema** *extends* `StandardSchemaV1`\<`unknown`, `unknown`\> = `StandardSchemaV1`\<`unknown`, `unknown`\>
33​
34The schema type for validation and type inference (second priority)
35​
36• **TFallback** *extends* `object` = `Record`\<`string`, `unknown`\>
37​
38The fallback type if no explicit or schema type is provided
39​
40## Parameters
41​
42### options
43​
44[`CollectionConfig`](../../interfaces/collectionconfig.md)\<[`ResolveType`](../../type-aliases/resolvetype.md)\<`TExplicit`, `TSchema`, `TFallback`\>, `TKey`, `TSchema`, [`ResolveInsertInput`](../../type-aliases/resolveinsertinput.md)\<`TExplicit`, `TSchema`, `TFallback`\>\> & `object`
45​
46Collection options with optional utilities
47​
48## Returns
49​
50[`Collection`](../../interfaces/collection.md)\<[`ResolveType`](../../type-aliases/resolvetype.md)\<`TExplicit`, `TSchema`, `TFallback`\>, `TKey`, `TUtils`, `TSchema`, [`ResolveInsertInput`](../../type-aliases/resolveinsertinput.md)\<`TExplicit`, `TSchema`, `TFallback`\>\>
51​
52A new Collection with utilities exposed both at top level and under .utils
53​
54## Examples
55​
56```ts
57// Pattern 1: With operation handlers (direct collection calls)
58const todos = createCollection({
59 id: "todos",
60 getKey: (todo) => todo.id,
61 schema,
62 onInsert: async ({ transaction, collection }) => {
63 // Send to API
64 await api.createTodo(transaction.mutations[0].modified)
65 },
66 onUpdate: async ({ transaction, collection }) => {
67 await api.updateTodo(transaction.mutations[0].modified)
68 },
69 onDelete: async ({ transaction, collection }) => {
70 await api.deleteTodo(transaction.mutations[0].key)
71 },
72 sync: { sync: () => {} }
73})
74​
75// Direct usage (handlers manage transactions)
76const tx = todos.insert({ id: "1", text: "Buy milk", completed: false })
77await tx.isPersisted.promise
78```
79​
80```ts
81// Pattern 2: Manual transaction management
82const todos = createCollection({
83 getKey: (todo) => todo.id,
84 schema: todoSchema,
85 sync: { sync: () => {} }
86})
87​
88// Explicit transaction usage
89const tx = createTransaction({
90 mutationFn: async ({ transaction }) => {
91 // Handle all mutations in transaction
92 await api.saveChanges(transaction.mutations)
93 }
94})
95​
96tx.mutate(() => {
97 todos.insert({ id: "1", text: "Buy milk" })
98 todos.update("2", draft => { draft.completed = true })
99})
100​
101await tx.isPersisted.promise
102```
103​
104```ts
105// Using schema for type inference (preferred as it also gives you client side validation)
106const todoSchema = z.object({
107 id: z.string(),
108 title: z.string(),
109 completed: z.boolean()
110})
111​
112const todos = createCollection({
113 schema: todoSchema,
114 getKey: (todo) => todo.id,
115 sync: { sync: () => {} }
116})
117​
118// Note: You must provide either an explicit type or a schema, but not both.
119```