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

helpers.ts

104 lines3.0 KB

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