@@ -4,104 +4,110 @@ import { asPowerSyncRecord, mapOperation } from "./helpers"
44import { PendingOperationStore } from "./PendingOperationStore"
55import { PowerSyncTransactor } from "./PowerSyncTransactor"
66import { convertTableToSchema } from "./schema"
7- import type { ExtractedTable } from "./helpers"
8- import type { PendingOperation } from "./PendingOperationStore"
7+ import type { Table , TriggerDiffRecord } from "@powersync/common"
8+ import type { StandardSchemaV1 } from "@standard-schema/spec"
9+ import type { CollectionConfig , SyncConfig } from "@tanstack/db"
910import type {
1011 EnhancedPowerSyncCollectionConfig ,
1112 PowerSyncCollectionConfig ,
1213 PowerSyncCollectionUtils ,
1314} from "./definitions"
14- import type { CollectionConfig , SyncConfig } from "@tanstack/db"
15- import type { StandardSchemaV1 } from "@standard-schema/spec"
16- import type { ColumnsType , Table , TriggerDiffRecord } from "@powersync/common"
15+ import type { ExtractedTable } from "./helpers"
16+ import type { PendingOperation } from "./PendingOperationStore"
1717
1818/**
1919 * Creates PowerSync collection options for use with a standard Collection
2020 *
21- * @template TExplicit - The explicit type of items in the collection (highest priority)
22- * @template TSchema - The schema type for validation and type inference (second priority)
21+ * @template TTable - The SQLite based typing
22+ * @template TSchema - The schema type for validation - optionally supports a custom input type
2323 * @param config - Configuration options for the PowerSync collection
2424 * @returns Collection options with utilities
2525 */
2626
27- // Overload for when schema is provided
2827/**
29- * Creates a PowerSync collection configuration with schema validation.
28+ * Creates a PowerSync collection configuration with basic default validation.
3029 *
3130 * @example
3231 * ```typescript
33- * // With schema validation
3432 * const APP_SCHEMA = new Schema({
3533 * documents: new Table({
3634 * name: column.text,
3735 * }),
3836 * })
3937 *
38+ * type Document = (typeof APP_SCHEMA)["types"]["documents"]
39+ *
40+ * const db = new PowerSyncDatabase({
41+ * database: {
42+ * dbFilename: "test.sqlite",
43+ * },
44+ * schema: APP_SCHEMA,
45+ * })
46+ *
4047 * const collection = createCollection(
4148 * powerSyncCollectionOptions({
4249 * database: db,
43- * table: APP_SCHEMA.props.documents,
44- * schema: TODO
50+ * table: APP_SCHEMA.props.documents
4551 * })
4652 * )
4753 * ```
4854 */
49- // TODO!!!
50- // export function powerSyncCollectionOptions<T extends StandardSchemaV1>(
51- // config: PowerSyncCollectionConfig<InferSchemaOutput<T>, T>
52- // ): CollectionConfig<InferSchemaOutput<T>, string, T> & {
53- // schema: T
54- // utils: PowerSyncCollectionUtils
55- // }
55+ export function powerSyncCollectionOptions < TTable extends Table = Table > (
56+ config : PowerSyncCollectionConfig < TTable , never >
57+ ) : CollectionConfig < ExtractedTable < TTable > , string , never > & {
58+ utils : PowerSyncCollectionUtils
59+ }
5660
61+ // Overload for when schema is provided
5762/**
58- * Creates a PowerSync collection configuration without schema validation.
63+ * Creates a PowerSync collection configuration with schema validation.
5964 *
6065 * @example
6166 * ```typescript
67+ * import { z } from "zod"
68+ *
69+ * // The PowerSync SQLite Schema
6270 * const APP_SCHEMA = new Schema({
6371 * documents: new Table({
6472 * name: column.text,
6573 * }),
6674 * })
6775 *
68- * type Document = (typeof APP_SCHEMA)["types"]["documents"]
69- *
70- * const db = new PowerSyncDatabase({
71- * database: {
72- * dbFilename: "test.sqlite",
73- * },
74- * schema: APP_SCHEMA,
76+ * // Advanced Zod validations. The output type of this schema
77+ * // is constrained to the SQLite schema of APP_SCHEMA
78+ * const schema = z.object({
79+ * id: z.string(),
80+ * name: z.string().min(3, { message: "Should be at least 3 characters" }).nullable(),
7581 * })
7682 *
7783 * const collection = createCollection(
7884 * powerSyncCollectionOptions({
7985 * database: db,
80- * table: APP_SCHEMA.props.documents
86+ * table: APP_SCHEMA.props.documents,
87+ * schema
8188 * })
8289 * )
8390 * ```
8491 */
8592export function powerSyncCollectionOptions <
86- TableType extends Table < ColumnsType > = Table < ColumnsType > ,
93+ TTable extends Table ,
94+ TSchema extends StandardSchemaV1 < ExtractedTable < TTable > , any > ,
8795> (
88- config : PowerSyncCollectionConfig < TableType > & {
89- schema ?: never
90- }
91- ) : CollectionConfig < ExtractedTable < TableType [ `columnMap`] > , string > & {
92- schema ?: never
96+ config : PowerSyncCollectionConfig < TTable , TSchema >
97+ ) : CollectionConfig < ExtractedTable < TTable > , string , TSchema > & {
9398 utils : PowerSyncCollectionUtils
99+ schema : TSchema
94100}
95101
96102/**
97103 * Implementation of powerSyncCollectionOptions that handles both schema and non-schema configurations.
98104 */
99105export function powerSyncCollectionOptions <
100- TableType extends Table < ColumnsType > = Table < ColumnsType > ,
106+ TTable extends Table = Table ,
101107 TSchema extends StandardSchemaV1 = never ,
102108> (
103- config : PowerSyncCollectionConfig < TableType , TSchema >
104- ) : EnhancedPowerSyncCollectionConfig < TableType , TSchema > {
109+ config : PowerSyncCollectionConfig < TTable , TSchema >
110+ ) : EnhancedPowerSyncCollectionConfig < TTable , TSchema > {
105111 const {
106112 database,
107113 table,
@@ -110,7 +116,7 @@ export function powerSyncCollectionOptions<
110116 ...restConfig
111117 } = config
112118
113- type RecordType = ExtractedTable < TableType [ `columnMap` ] >
119+ type RecordType = ExtractedTable < TTable >
114120 const { viewName } = table
115121
116122 // We can do basic runtime validations for columns if not explicit schema has been provided
@@ -277,7 +283,7 @@ export function powerSyncCollectionOptions<
277283
278284 const getKey = ( record : RecordType ) => asPowerSyncRecord ( record ) . id
279285
280- const outputConfig : EnhancedPowerSyncCollectionConfig < TableType , TSchema > = {
286+ const outputConfig : EnhancedPowerSyncCollectionConfig < TTable , TSchema > = {
281287 ...restConfig ,
282288 schema,
283289 getKey,
@@ -286,7 +292,6 @@ export function powerSyncCollectionOptions<
286292 sync,
287293 onInsert : async ( params ) => {
288294 // The transaction here should only ever contain a single insert mutation
289- params . transaction
290295 return await transactor . applyTransaction ( params . transaction )
291296 } ,
292297 onUpdate : async ( params ) => {
0 commit comments