Open History to see the last commit
1import { DiffTriggerOperation } from '@powersync/common'
2import type {
3 BaseColumnType,
4 ExtractColumnValueType,
5 Table,
6} from '@powersync/common'
7
8/**
9 * All PowerSync table records include a UUID `id` column.
10 */
11export type PowerSyncRecord = {
12 id: string
13 [key: string]: unknown
14}
15
16/**
17 * Utility type: If T includes null, also allow undefined (to support optional fields in insert/update operations).
18 * PowerSync records are typically typed as `string | null`, where insert
19 * and update operations may also allow not specifying a value at all (optional).
20 */
21type WithUndefinedIfNull<T> = null extends T ? T | undefined : T
22type OptionalIfUndefined<T> = {
23 [K in keyof T as undefined extends T[K] ? K : never]?: T[K]
24} & {
25 [K in keyof T as undefined extends T[K] ? never : K]: T[K]
26}
27
28/**
29 * Provides the base column types for a table. This excludes the `id` column.
30 */
31export type ExtractedTableColumns<TTable extends Table> = {
32 [K in keyof TTable[`columnMap`]]: ExtractColumnValueType<
33 TTable[`columnMap`][K]
34 >
35}
36/**
37 * Utility type that extracts the typed structure of a table based on its column definitions.
38 * Maps each column to its corresponding TypeScript type using ExtractColumnValueType.
39 *
40 * @template TTable - The PowerSync table definition
41 * @example
42 * ```typescript
43 * const table = new Table({
44 * name: column.text,
45 * age: column.integer
46 * })
47 * type TableType = ExtractedTable<typeof table>
48 * // Results in: { id: string, name: string | null, age: number | null }
49 * ```
50 */
51export type ExtractedTable<TTable extends Table> =
52 ExtractedTableColumns<TTable> & {
53 id: string
54 }
55
56export type OptionalExtractedTable<TTable extends Table> = OptionalIfUndefined<{
57 [K in keyof TTable[`columnMap`]]: WithUndefinedIfNull<
58 ExtractColumnValueType<TTable[`columnMap`][K]>
59 >
60}> & {
61 id: string
62}
63
64/**
65 * Maps the schema of TTable to a type which
66 * requires the keys be equal, but the values can have any value type.
67 */
68export type AnyTableColumnType<TTable extends Table> = {
69 [K in keyof TTable[`columnMap`]]: any
70} & { id: string }
71
72export function asPowerSyncRecord(record: any): PowerSyncRecord {
73 if (typeof record.id !== `string`) {
74 throw new Error(`Record must have a string id field`)
75 }
76 return record as PowerSyncRecord
77}
78
79// Helper type to ensure the keys of TOutput match the Table columns
80export type MapBaseColumnType<TOutput> = {
81 [Key in keyof TOutput]: BaseColumnType<any>
82}
83
84/**
85 * Maps {@link DiffTriggerOperation} to TanstackDB operations
86 */
87export function mapOperation(operation: DiffTriggerOperation) {
88 switch (operation) {
89 case DiffTriggerOperation.INSERT:
90 return `insert`
91 case DiffTriggerOperation.UPDATE:
92 return `update`
93 case DiffTriggerOperation.DELETE:
94 return `delete`
95 }
96}
97
98/**
99 * Maps TanstackDB operations to {@link DiffTriggerOperation}
100 */
101export function mapOperationToPowerSync(operation: string) {
102 switch (operation) {
103 case `insert`:
104 return DiffTriggerOperation.INSERT
105 case `update`:
106 return DiffTriggerOperation.UPDATE
107 case `delete`:
108 return DiffTriggerOperation.DELETE
109 default:
110 throw new Error(`Unknown operation ${operation} received`)
111 }
112}