Repository navigation
Expand file tree
/
Copy pathsqlite.ts
More file actions
109 lines (103 loc) · 4.74 KB
/
Copy pathsqlite.ts
File metadata and controls
109 lines (103 loc) · 4.74 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
101
102
103
104
105
106
107
108
109
// @maple-dev/effect-orm/sqlite
//
// SQLite and Cloudflare D1 from one import: column types, tables with their
// DDL, SQLite functions, the query builder and a `compile` that defaults to
// the SQLite dialect. Migrations stay with the caller for now. See
// docs/sqlite.md.
import {
compileCHRaw,
compileUnion as compileUnionCH,
compileUnionUnsafe as compileUnionUnsafeCH,
type Compile,
type CompileUnion,
type CompileUnionUnsafe,
type CompileUnsafe,
} from "./ch/compile"
import { activeDialect, type Dialect } from "./ch/dialect"
import { compiled, compiledUnsafe } from "./ch/failure"
import { table as queryTable, type Table, type TableOptions } from "./ch/table"
import type { ColumnDefs } from "./ch/types"
import {
externalSqliteTable,
isTableDefinition,
sqliteTable,
type ColumnInput,
type ColumnsOf,
type DefaultedColumnsOf,
type ExternalTableDefinition,
type InsertRequirementsOf,
type SqliteSchemaTable,
type TableDefinition,
type UpdateRequirementsOf,
} from "./schema/sqlite-define"
import { sqliteDialect } from "./sqlite/dialect"
import { Effect } from "effect"
export * from "./core"
export * from "./sqlite/types"
export * from "./sqlite/functions"
export * from "./sqlite/table-functions"
export type { TableOptions }
export {
column,
index,
uniqueIndex,
type ColumnInput,
type ColumnOptions,
type ColumnSpec,
type ColumnsOf,
type DefaultedColumnsOf,
type ExternalTableDefinition,
type IndexSpec,
type SqliteSchemaTable,
type TableDdl,
type TableDefinition,
} from "./schema/sqlite-define"
export { SchemaDefinitionError, type DefinitionProblem, type DdlExpr, type DdlKey } from "./schema/define"
export { type Casing } from "./schema/casing"
/**
* A SQLite table. With `{ columns, primaryKey, indexes }` it carries its DDL
* (`renderSqliteSchema` from `/schema` writes it). With `{ external: true, columns }`,
* or a bare record of columns and query options, it is a query schema only.
*/
export function table<const Name extends string, const Columns extends Record<string, ColumnInput>>(
name: Name,
definition: ExternalTableDefinition<Columns>,
): Table<Name, ColumnsOf<Columns>, DefaultedColumnsOf<Columns>, never, InsertRequirementsOf<Columns>, UpdateRequirementsOf<Columns>>
export function table<const Name extends string, const Columns extends Record<string, ColumnInput>>(
name: Name,
definition: TableDefinition<Columns>,
): SqliteSchemaTable<Name, ColumnsOf<Columns>, DefaultedColumnsOf<Columns>, InsertRequirementsOf<Columns>, UpdateRequirementsOf<Columns>>
export function table<
const Name extends string,
const Columns extends ColumnDefs,
const Defaulted extends keyof Columns & string = never,
const Computed extends keyof Columns & string = never,
>(name: Name, columns: Columns, options?: TableOptions<Columns, Defaulted, Computed>): Table<Name, Columns, Defaulted, Computed>
export function table(name: string, definition: any, options?: TableOptions<any, any, any>): Table<string, any, any, any> {
if (options !== undefined || !isTableDefinition(definition)) return queryTable(name, definition, options)
if ("external" in definition && definition.external === true) return externalSqliteTable(name, definition as ExternalTableDefinition<any>)
return sqliteTable(name, definition as TableDefinition<any>)
}
export { sqliteDialect, d1Dialect } from "./sqlite/dialect"
export { runSync, type RunSyncOptions, type SqlStorageLike, type SyncDefaults } from "./sqlite/run-sync"
/** The dialect a `/sqlite` compile writes for: `options.dialect`, else `CurrentDialect`, else SQLite. */
const sqliteOptions = <O extends { readonly dialect?: Dialect | undefined } | undefined>(options: O) => ({
...options,
dialect: options?.dialect ?? activeDialect() ?? sqliteDialect,
})
/**
* The shared compiler, for SQLite unless `options.dialect` or `CurrentDialect`
* says otherwise. Pass (or provide) d1Dialect for D1's limits. The dialect is
* read when the Effect runs, from the fiber that runs it. Its type checks a
* query's functions and clauses against SQLite (see `DialectSatisfied`).
*/
// Typed through `any` and cast, as in `/postgres`: `Compile` is overloaded.
export const compile = ((query: any, params?: any, options?: any) =>
compiled(() => compileCHRaw(query, params, sqliteOptions(options)))) as Compile<"sqlite">
/** Compile immediately, throwing on a failed compile. */
export const compileUnsafe = ((query: any, params?: any, options?: any) =>
compiledUnsafe(() => compileCHRaw(query, params, sqliteOptions(options)))) as CompileUnsafe<"sqlite">
export const compileUnion: CompileUnion<"sqlite"> = (query, params, options) =>
Effect.suspend(() => compileUnionCH(query, params, sqliteOptions(options)))
export const compileUnionUnsafe: CompileUnionUnsafe<"sqlite"> = (query, params, options) =>
compileUnionUnsafeCH(query, params, sqliteOptions(options))