@livestore/common 0.4.0-dev.0 → 0.4.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,290 @@
1
+ import { shouldNeverHappen, type Writeable } from '@livestore/utils'
2
+ import { Option, Schema, SchemaAST } from '@livestore/utils/effect'
3
+
4
+ import { AutoIncrement, ColumnType, Default, PrimaryKeyId, Unique } from './column-annotations.ts'
5
+ import { SqliteDsl } from './db-schema/mod.ts'
6
+
7
+ /**
8
+ * Maps a schema to a SQLite column definition, respecting column annotations.
9
+ */
10
+ export const getColumnDefForSchema = (
11
+ schema: Schema.Schema.AnyNoContext,
12
+ propertySignature?: SchemaAST.PropertySignature,
13
+ ): SqliteDsl.ColumnDefinition.Any => {
14
+ const ast = schema.ast
15
+
16
+ // 1. Extract annotations
17
+ const getAnnotation = <T>(annotationId: symbol): Option.Option<T> =>
18
+ propertySignature
19
+ ? hasPropertyAnnotation<T>(propertySignature, annotationId)
20
+ : SchemaAST.getAnnotation<T>(annotationId)(ast)
21
+
22
+ const annotations = {
23
+ primaryKey: getAnnotation<boolean>(PrimaryKeyId).pipe(Option.getOrElse(() => false)),
24
+ autoIncrement: getAnnotation<boolean>(AutoIncrement).pipe(Option.getOrElse(() => false)),
25
+ defaultValue: getAnnotation<unknown>(Default),
26
+ columnType: SchemaAST.getAnnotation<SqliteDsl.FieldColumnType>(ColumnType)(ast),
27
+ }
28
+
29
+ // 2. Resolve the core type and nullable info
30
+ const typeInfo = resolveType(ast)
31
+
32
+ // 3. Create column definition based on resolved type
33
+ let columnDef: SqliteDsl.ColumnDefinition.Any
34
+
35
+ // Custom column type overrides everything
36
+ if (Option.isSome(annotations.columnType)) {
37
+ columnDef = createColumnFromType(annotations.columnType.value, typeInfo.coreType)
38
+ }
39
+ // Lossy case: both null and undefined need JSON
40
+ else if (typeInfo.hasNull && typeInfo.hasUndefined) {
41
+ columnDef = {
42
+ ...SqliteDsl.text(),
43
+ nullable: true,
44
+ schema: Schema.parseJson(schema),
45
+ }
46
+ }
47
+ // Regular nullable/optional case
48
+ else if (typeInfo.hasNull || typeInfo.hasUndefined) {
49
+ const baseColumnDef = createColumnFromAST(typeInfo.coreType, Schema.make(typeInfo.coreType))
50
+ const isComplexOptional = typeInfo.hasUndefined && !isPrimitiveAST(typeInfo.coreType)
51
+
52
+ columnDef = {
53
+ ...baseColumnDef,
54
+ nullable: true,
55
+ schema: isComplexOptional ? Schema.parseJson(schema) : schema,
56
+ }
57
+ }
58
+ // Non-nullable type
59
+ else {
60
+ columnDef = createColumnFromAST(ast, schema)
61
+ }
62
+
63
+ // 4. Apply annotations
64
+ const result = { ...columnDef }
65
+ if (annotations.primaryKey) result.primaryKey = true
66
+ if (annotations.autoIncrement) result.autoIncrement = true
67
+ if (Option.isSome(annotations.defaultValue)) {
68
+ result.default = Option.some(annotations.defaultValue.value)
69
+ }
70
+
71
+ return result
72
+ }
73
+
74
+ /**
75
+ * Checks if a property signature has a specific annotation, checking both
76
+ * the property signature itself and its type AST.
77
+ */
78
+ const hasPropertyAnnotation = <T>(
79
+ propertySignature: SchemaAST.PropertySignature,
80
+ annotationId: symbol,
81
+ ): Option.Option<T> => {
82
+ // When using Schema.optional(Schema.String).pipe(withPrimaryKey) in a struct,
83
+ // the annotation ends up on a PropertySignatureDeclaration, not the Union type
84
+ // Check if this is a PropertySignatureDeclaration with annotations
85
+ if ('annotations' in propertySignature && propertySignature.annotations) {
86
+ const annotation = SchemaAST.getAnnotation<T>(annotationId)(propertySignature as any)
87
+ if (Option.isSome(annotation)) {
88
+ return annotation
89
+ }
90
+ }
91
+
92
+ // Otherwise check the type AST
93
+ return SchemaAST.getAnnotation<T>(annotationId)(propertySignature.type)
94
+ }
95
+
96
+ /**
97
+ * Maps schema property signatures to SQLite column definitions.
98
+ * Returns both columns and unique column names for index creation.
99
+ */
100
+ export const schemaFieldsToColumns = (
101
+ propertySignatures: ReadonlyArray<SchemaAST.PropertySignature>,
102
+ ): { columns: SqliteDsl.Columns; uniqueColumns: string[] } => {
103
+ const columns: SqliteDsl.Columns = {}
104
+ const uniqueColumns: string[] = []
105
+
106
+ for (const prop of propertySignatures) {
107
+ if (typeof prop.name === 'string') {
108
+ // Create a schema from the AST
109
+ const fieldSchema = Schema.make(prop.type)
110
+ // Check if property has primary key annotation
111
+ const hasPrimaryKey = hasPropertyAnnotation<boolean>(prop, PrimaryKeyId).pipe(Option.getOrElse(() => false))
112
+ // Check if property has unique annotation
113
+ const hasUnique = hasPropertyAnnotation<boolean>(prop, Unique).pipe(Option.getOrElse(() => false))
114
+
115
+ columns[prop.name] = schemaFieldToColumn(fieldSchema, prop, hasPrimaryKey)
116
+
117
+ if (hasUnique) {
118
+ uniqueColumns.push(prop.name)
119
+ }
120
+ }
121
+ }
122
+
123
+ return { columns, uniqueColumns }
124
+ }
125
+
126
+ /**
127
+ * Converts a schema field and its property signature to a SQLite column definition.
128
+ */
129
+ const schemaFieldToColumn = (
130
+ fieldSchema: Schema.Schema.AnyNoContext,
131
+ propertySignature: SchemaAST.PropertySignature,
132
+ forceHasPrimaryKey?: boolean,
133
+ ): SqliteDsl.ColumnDefinition.Any => {
134
+ // Determine column type based on schema type
135
+ const columnDef = getColumnDefForSchema(fieldSchema, propertySignature)
136
+
137
+ // Create a new object with appropriate properties
138
+ const result: Writeable<SqliteDsl.ColumnDefinition.Any> = {
139
+ columnType: columnDef.columnType,
140
+ schema: columnDef.schema,
141
+ default: columnDef.default,
142
+ nullable: columnDef.nullable,
143
+ primaryKey: columnDef.primaryKey,
144
+ autoIncrement: columnDef.autoIncrement,
145
+ }
146
+
147
+ // Set primaryKey property explicitly
148
+ if (forceHasPrimaryKey || columnDef.primaryKey) {
149
+ result.primaryKey = true
150
+ } else {
151
+ result.primaryKey = false
152
+ }
153
+
154
+ // Check for invalid primary key + nullable combination
155
+ if (result.primaryKey && (propertySignature.isOptional || columnDef.nullable)) {
156
+ return shouldNeverHappen(
157
+ `Primary key columns cannot be nullable. Found nullable primary key for column. ` +
158
+ `Either remove the primary key annotation or use a non-nullable schema.`,
159
+ )
160
+ }
161
+
162
+ // Set nullable property explicitly
163
+ if (propertySignature.isOptional) {
164
+ result.nullable = true
165
+ } else if (columnDef.nullable) {
166
+ result.nullable = true
167
+ } else {
168
+ result.nullable = false
169
+ }
170
+
171
+ // Only add autoIncrement if it's true
172
+ if (columnDef.autoIncrement) {
173
+ result.autoIncrement = true
174
+ }
175
+
176
+ return result as SqliteDsl.ColumnDefinition.Any
177
+ }
178
+
179
+ /**
180
+ * Resolves type information from an AST, unwrapping unions and tracking nullability.
181
+ */
182
+ const resolveType = (
183
+ ast: SchemaAST.AST,
184
+ ): {
185
+ coreType: SchemaAST.AST
186
+ hasNull: boolean
187
+ hasUndefined: boolean
188
+ } => {
189
+ if (!SchemaAST.isUnion(ast)) {
190
+ return { coreType: ast, hasNull: false, hasUndefined: false }
191
+ }
192
+
193
+ let hasNull = false
194
+ let hasUndefined = false
195
+ let coreType: SchemaAST.AST | undefined
196
+
197
+ const visit = (type: SchemaAST.AST): void => {
198
+ if (SchemaAST.isUndefinedKeyword(type)) {
199
+ hasUndefined = true
200
+ } else if (SchemaAST.isLiteral(type) && type.literal === null) {
201
+ hasNull = true
202
+ } else if (SchemaAST.isUnion(type)) {
203
+ type.types.forEach(visit)
204
+ } else if (!coreType) {
205
+ coreType = type
206
+ }
207
+ }
208
+
209
+ ast.types.forEach(visit)
210
+ return { coreType: coreType || ast, hasNull, hasUndefined }
211
+ }
212
+
213
+ /**
214
+ * Creates a column definition from an AST node.
215
+ */
216
+ const createColumnFromAST = (
217
+ ast: SchemaAST.AST,
218
+ schema: Schema.Schema.AnyNoContext,
219
+ ): SqliteDsl.ColumnDefinition.Any => {
220
+ // Follow refinements and transformations to their core type
221
+ if (SchemaAST.isRefinement(ast)) {
222
+ // Special case for Schema.Int
223
+ const identifier = SchemaAST.getIdentifierAnnotation(ast).pipe(Option.getOrElse(() => ''))
224
+ if (identifier === 'Int') return SqliteDsl.integer()
225
+ return createColumnFromAST(ast.from, Schema.make(ast.from))
226
+ }
227
+
228
+ if (SchemaAST.isTransformation(ast)) {
229
+ return createColumnFromAST(ast.to, Schema.make(ast.to))
230
+ }
231
+
232
+ // Primitive types
233
+ if (SchemaAST.isStringKeyword(ast)) return SqliteDsl.text()
234
+ if (SchemaAST.isNumberKeyword(ast)) return SqliteDsl.real()
235
+ if (SchemaAST.isBooleanKeyword(ast)) return SqliteDsl.boolean()
236
+
237
+ // Literals
238
+ if (SchemaAST.isLiteral(ast)) {
239
+ const value = ast.literal
240
+ if (typeof value === 'string') return SqliteDsl.text()
241
+ if (typeof value === 'number') return SqliteDsl.real()
242
+ if (typeof value === 'boolean') return SqliteDsl.boolean()
243
+ }
244
+
245
+ // Everything else is complex
246
+ return SqliteDsl.json({ schema })
247
+ }
248
+
249
+ /**
250
+ * Creates a column from a specific column type string.
251
+ */
252
+ const createColumnFromType = (columnType: string, ast: SchemaAST.AST): SqliteDsl.ColumnDefinition.Any => {
253
+ switch (columnType) {
254
+ case 'text':
255
+ return SqliteDsl.text()
256
+ case 'integer':
257
+ // Preserve boolean transformation
258
+ return SchemaAST.isBooleanKeyword(ast) ? SqliteDsl.boolean() : SqliteDsl.integer()
259
+ case 'real':
260
+ return SqliteDsl.real()
261
+ case 'blob':
262
+ return SqliteDsl.blob()
263
+ default:
264
+ return shouldNeverHappen(`Unsupported column type: ${columnType}`)
265
+ }
266
+ }
267
+
268
+ /**
269
+ * Checks if an AST represents a primitive (non-complex) type.
270
+ */
271
+ const isPrimitiveAST = (ast: SchemaAST.AST): boolean => {
272
+ if (
273
+ SchemaAST.isStringKeyword(ast) ||
274
+ SchemaAST.isNumberKeyword(ast) ||
275
+ SchemaAST.isBooleanKeyword(ast) ||
276
+ SchemaAST.isLiteral(ast)
277
+ ) {
278
+ return true
279
+ }
280
+
281
+ if (SchemaAST.isRefinement(ast)) {
282
+ return isPrimitiveAST(ast.from)
283
+ }
284
+
285
+ if (SchemaAST.isTransformation(ast)) {
286
+ return isPrimitiveAST(ast.to)
287
+ }
288
+
289
+ return false
290
+ }