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