Open History to see the last commit
1---
2id: powerSyncCollectionOptions
3title: powerSyncCollectionOptions
4---
5
6# Function: powerSyncCollectionOptions()
7
8Implementation of powerSyncCollectionOptions that handles both schema and non-schema configurations.
9
10## Call Signature
11
12```ts
13function powerSyncCollectionOptions<TTable>(config): EnhancedPowerSyncCollectionConfig<TTable, OptionalExtractedTable<TTable>, never>;
14```
15
16Defined in: [powersync.ts:78](https://github.com/TanStack/db/blob/main/packages/powersync-db-collection/src/powersync.ts#L78)
17
18Creates a PowerSync collection configuration with basic default validation.
19Input and Output types are the SQLite column types.
20
21### Type Parameters
22
23#### TTable
24
25`TTable` *extends* `Table`\<`ColumnsType`\> = `Table`\<`ColumnsType`\>
26
27### Parameters
28
29#### config
30
31[`BasePowerSyncCollectionConfig`](../type-aliases/BasePowerSyncCollectionConfig.md)\<`TTable`, `never`\> & [`ConfigWithSQLiteTypes`](../type-aliases/ConfigWithSQLiteTypes.md)
32
33### Returns
34
35[`EnhancedPowerSyncCollectionConfig`](../type-aliases/EnhancedPowerSyncCollectionConfig.md)\<`TTable`, `OptionalExtractedTable`\<`TTable`\>, `never`\>
36
37### Example
38
39```typescript
40const APP_SCHEMA = new Schema({
41 documents: new Table({
42 name: column.text,
43 }),
44})
45
46type Document = (typeof APP_SCHEMA)["types"]["documents"]
47
48const db = new PowerSyncDatabase({
49 database: {
50 dbFilename: "test.sqlite",
51 },
52 schema: APP_SCHEMA,
53})
54
55const collection = createCollection(
56 powerSyncCollectionOptions({
57 database: db,
58 table: APP_SCHEMA.props.documents
59 })
60)
61```
62
63## Call Signature
64
65```ts
66function powerSyncCollectionOptions<TTable, TSchema>(config): CollectionConfig<InferPowerSyncOutputType<TTable, TSchema>, string, TSchema, PowerSyncCollectionUtils<TTable>> & object & object;
67```
68
69Defined in: [powersync.ts:135](https://github.com/TanStack/db/blob/main/packages/powersync-db-collection/src/powersync.ts#L135)
70
71Creates a PowerSync collection configuration with schema validation.
72
73The input types satisfy the SQLite column types.
74
75The output types are defined by the provided schema. This schema can enforce additional
76validation or type transforms.
77Arbitrary output typed mutations are encoded to SQLite for persistence. We provide a basic standard
78serialization implementation to serialize column values. Custom or advanced types require providing additional
79serializer specifications. Partial column overrides can be supplied to `serializer`.
80
81### Type Parameters
82
83#### TTable
84
85`TTable` *extends* `Table`\<`ColumnsType`\>
86
87#### TSchema
88
89`TSchema` *extends* `StandardSchemaV1`\<`OptionalExtractedTable`\<`TTable`\>, `AnyTableColumnType`\<`TTable`\>\>
90
91### Parameters
92
93#### config
94
95[`BasePowerSyncCollectionConfig`](../type-aliases/BasePowerSyncCollectionConfig.md)\<`TTable`, `TSchema`\> & [`SerializerConfig`](../type-aliases/SerializerConfig.md)\<`InferOutput`\<`TSchema`\>, `ExtractedTable`\<`TTable`\>\> & `object`
96
97### Returns
98
99`CollectionConfig`\<[`InferPowerSyncOutputType`](../type-aliases/InferPowerSyncOutputType.md)\<`TTable`, `TSchema`\>, `string`, `TSchema`, [`PowerSyncCollectionUtils`](../type-aliases/PowerSyncCollectionUtils.md)\<`TTable`\>\> & `object` & `object`
100
101### Example
102
103```typescript
104import { z } from "zod"
105
106// The PowerSync SQLite schema
107const APP_SCHEMA = new Schema({
108 documents: new Table({
109 name: column.text,
110 // Dates are stored as ISO date strings in SQLite
111 created_at: column.text
112 }),
113})
114
115// Advanced Zod validations. The output type of this schema
116// is constrained to the SQLite schema of APP_SCHEMA
117const schema = z.object({
118 id: z.string(),
119 // Notice that `name` is not nullable (is required) here and it has additional validation
120 name: z.string().min(3, { message: "Should be at least 3 characters" }).nullable(),
121 // The input type is still the SQLite string type. While collections will output smart Date instances.
122 created_at: z.string().transform(val => new Date(val))
123})
124
125const collection = createCollection(
126 powerSyncCollectionOptions({
127 database: db,
128 table: APP_SCHEMA.props.documents,
129 schema,
130 serializer: {
131 // The default is toISOString, this is just to demonstrate custom overrides
132 created_at: (outputValue) => outputValue.toISOString(),
133 },
134 })
135)
136```
137
138## Call Signature
139
140```ts
141function powerSyncCollectionOptions<TTable, TSchema>(config): CollectionConfig<InferPowerSyncOutputType<TTable, TSchema>, string, TSchema, PowerSyncCollectionUtils<TTable>> & object & object;
142```
143
144Defined in: [powersync.ts:203](https://github.com/TanStack/db/blob/main/packages/powersync-db-collection/src/powersync.ts#L203)
145
146Creates a PowerSync collection configuration with schema validation.
147
148The input types are not linked to the internal SQLite table types. This can
149give greater flexibility, e.g. by accepting rich types as input for `insert` or `update` operations.
150An additional `deserializationSchema` is required in order to process incoming SQLite updates to the output type.
151
152The output types are defined by the provided schema. This schema can enforce additional
153validation or type transforms.
154Arbitrary output typed mutations are encoded to SQLite for persistence. We provide a basic standard
155serialization implementation to serialize column values. Custom or advanced types require providing additional
156serializer specifications. Partial column overrides can be supplied to `serializer`.
157
158### Type Parameters
159
160#### TTable
161
162`TTable` *extends* `Table`\<`ColumnsType`\>
163
164#### TSchema
165
166`TSchema` *extends* `StandardSchemaV1`\<`AnyTableColumnType`\<`TTable`\>, `AnyTableColumnType`\<`TTable`\>\>
167
168### Parameters
169
170#### config
171
172[`BasePowerSyncCollectionConfig`](../type-aliases/BasePowerSyncCollectionConfig.md)\<`TTable`, `TSchema`\> & [`SerializerConfig`](../type-aliases/SerializerConfig.md)\<`InferOutput`\<`TSchema`\>, `ExtractedTable`\<`TTable`\>\> & `object`
173
174### Returns
175
176`CollectionConfig`\<[`InferPowerSyncOutputType`](../type-aliases/InferPowerSyncOutputType.md)\<`TTable`, `TSchema`\>, `string`, `TSchema`, [`PowerSyncCollectionUtils`](../type-aliases/PowerSyncCollectionUtils.md)\<`TTable`\>\> & `object` & `object`
177
178### Example
179
180```typescript
181import { z } from "zod"
182
183// The PowerSync SQLite schema
184const APP_SCHEMA = new Schema({
185 documents: new Table({
186 name: column.text,
187 // Booleans are represented as integers in SQLite
188 is_active: column.integer
189 }),
190})
191
192// Advanced Zod validations.
193// We accept boolean values as input for operations and expose Booleans in query results
194const schema = z.object({
195 id: z.string(),
196 isActive: z.boolean(), // TInput and TOutput are boolean
197})
198
199// The deserializationSchema converts the SQLite synced INTEGER (0/1) values to booleans.
200const deserializationSchema = z.object({
201 id: z.string(),
202 isActive: z.number().nullable().transform((val) => val == null ? true : val > 0),
203})
204
205const collection = createCollection(
206 powerSyncCollectionOptions({
207 database: db,
208 table: APP_SCHEMA.props.documents,
209 schema,
210 deserializationSchema,
211 })
212)
213```