forked from TanStack/db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.ts
More file actions
100 lines (91 loc) · 2.55 KB
/
schema.ts
File metadata and controls
100 lines (91 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { ColumnType } from "@powersync/common"
import type { Table } from "@powersync/common"
import type { StandardSchemaV1 } from "@standard-schema/spec"
import type { ExtractedTable } from "./helpers"
/**
* Converts a PowerSync Table instance to a StandardSchemaV1 schema.
* Creates a schema that validates the structure and types of table records
* according to the PowerSync table definition.
*
* @template TTable - The PowerSync schema-typed Table definition
* @param table - The PowerSync Table instance to convert
* @returns A StandardSchemaV1-compatible schema with proper type validation
*
* @example
* ```typescript
* const usersTable = new Table({
* name: column.text,
* age: column.integer
* })
* ```
*/
export function convertTableToSchema<TTable extends Table>(
table: TTable
): StandardSchemaV1<ExtractedTable<TTable>> {
type TExtracted = ExtractedTable<TTable>
// Create validate function that checks types according to column definitions
const validate = (
value: unknown
):
| StandardSchemaV1.SuccessResult<TExtracted>
| StandardSchemaV1.FailureResult => {
if (typeof value != `object` || value == null) {
return {
issues: [
{
message: `Value must be an object`,
},
],
}
}
const issues: Array<StandardSchemaV1.Issue> = []
// Check id field
if (!(`id` in value) || typeof (value as any).id != `string`) {
issues.push({
message: `id field must be a string`,
path: [`id`],
})
}
// Check each column
for (const column of table.columns) {
const val = (value as TExtracted)[column.name as keyof TExtracted]
if (val == null) {
continue
}
switch (column.type) {
case ColumnType.TEXT:
if (typeof val != `string`) {
issues.push({
message: `${column.name} must be a string or null`,
path: [column.name],
})
}
break
case ColumnType.INTEGER:
case ColumnType.REAL:
if (typeof val != `number`) {
issues.push({
message: `${column.name} must be a number or null`,
path: [column.name],
})
}
break
}
}
if (issues.length > 0) {
return { issues }
}
return { value: { ...value } as TExtracted }
}
return {
"~standard": {
version: 1,
vendor: `powersync`,
validate,
types: {
input: {} as TExtracted,
output: {} as TExtracted,
},
},
}
}