flint-orm 0.4.1 → 0.4.3
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.
- package/dist/config.d.ts +30 -0
- package/dist/drivers/better-sqlite3.d.ts +38 -0
- package/dist/drivers/bun-sqlite.d.ts +38 -0
- package/dist/drivers/libsql-web.d.ts +44 -0
- package/dist/drivers/libsql.d.ts +40 -0
- package/dist/drivers/turso-sync.d.ts +44 -0
- package/dist/drivers/turso.d.ts +38 -0
- package/dist/entries/better-sqlite3.d.ts +1 -0
- package/dist/entries/bun-sqlite.d.ts +1 -0
- package/dist/entries/config.d.ts +1 -0
- package/dist/entries/expressions.d.ts +5 -0
- package/dist/entries/libsql-web.d.ts +2 -0
- package/dist/entries/libsql.d.ts +2 -0
- package/dist/entries/table.d.ts +4 -0
- package/dist/entries/turso-sync.d.ts +2 -0
- package/dist/entries/turso.d.ts +1 -0
- package/dist/errors.d.ts +13 -0
- package/dist/executor.d.ts +16 -0
- package/dist/flint.d.ts +120 -0
- package/dist/index.d.ts +11 -0
- package/dist/migration/diff.d.ts +15 -0
- package/dist/migration/generate.d.ts +18 -0
- package/dist/migration/index.d.ts +11 -0
- package/dist/migration/migrate.d.ts +52 -0
- package/dist/migration/migration.d.ts +5 -0
- package/dist/migration/operations.d.ts +14 -0
- package/dist/migration/serialize.d.ts +3 -0
- package/dist/migration/sql.d.ts +2 -0
- package/dist/migration/types.d.ts +98 -0
- package/dist/query/aggregates.d.ts +47 -0
- package/dist/query/builder.d.ts +257 -0
- package/dist/query/conditions.d.ts +176 -0
- package/dist/schema/columns.d.ts +146 -0
- package/dist/schema/table.d.ts +74 -0
- package/dist/sqlite/introspect.d.ts +19 -0
- package/dist/src/cli.js +13818 -0
- package/dist/src/entries/better-sqlite3.js +2050 -0
- package/dist/src/entries/bun-sqlite.js +1284 -0
- package/dist/src/entries/config.js +55 -0
- package/dist/src/entries/expressions.js +1252 -0
- package/dist/src/entries/libsql-web.js +6065 -0
- package/dist/src/entries/libsql.js +7208 -0
- package/dist/src/entries/table.js +403 -0
- package/dist/src/entries/turso-sync.js +3504 -0
- package/dist/src/entries/turso.js +2379 -0
- package/dist/{index.js → src/index.js} +86 -22
- package/dist/src/migration/index.js +1066 -0
- package/package.json +1 -1
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/** Foreign key referential action. */
|
|
2
|
+
export type ForeignKeyAction = 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
|
|
3
|
+
/**
|
|
4
|
+
* A column definition with chainable modifiers.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const name = text("name").notNull().default("unnamed");
|
|
8
|
+
*/
|
|
9
|
+
export interface ColumnDef<T, S extends string = string> {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
primaryKey(): ColumnDef<T, S>;
|
|
12
|
+
notNull(): ColumnDef<T, S>;
|
|
13
|
+
unique(): ColumnDef<T, S>;
|
|
14
|
+
/** Set a static default value used when the value is omitted during insert. */
|
|
15
|
+
default(value: T): ColumnDef<T, S>;
|
|
16
|
+
/** Set a dynamic default function called when the value is omitted during insert. */
|
|
17
|
+
defaultFn(fn: () => T): ColumnDef<T, S>;
|
|
18
|
+
/**
|
|
19
|
+
* Define a foreign key reference to another table's column.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* text("userId").references(users.id)
|
|
23
|
+
*/
|
|
24
|
+
references(target: ColumnDef<any, any>): ColumnDef<T, S>;
|
|
25
|
+
/** Set ON DELETE action for the foreign key. Requires `.references()` to be called first. */
|
|
26
|
+
onDelete(action: ForeignKeyAction): ColumnDef<T, S>;
|
|
27
|
+
/** Set ON UPDATE action for the foreign key. Requires `.references()` to be called first. */
|
|
28
|
+
onUpdate(action: ForeignKeyAction): ColumnDef<T, S>;
|
|
29
|
+
readonly __internal: {
|
|
30
|
+
/** Phantom — exists only at the type level, never accessed at runtime. */
|
|
31
|
+
readonly _type: T;
|
|
32
|
+
/** SQLite storage class: "text" | "integer" | "real" | "blob". */
|
|
33
|
+
readonly sqlType: S;
|
|
34
|
+
/** Column constraint flags — set via modifiers, read by migration generator. */
|
|
35
|
+
readonly isPrimaryKey: boolean;
|
|
36
|
+
readonly isNotNull: boolean;
|
|
37
|
+
readonly isUnique: boolean;
|
|
38
|
+
readonly hasDefault: boolean;
|
|
39
|
+
readonly defaultValue: T | undefined;
|
|
40
|
+
readonly defaultFn: (() => T) | undefined;
|
|
41
|
+
readonly isAutoIncrement: boolean;
|
|
42
|
+
readonly hasDefaultNow: boolean;
|
|
43
|
+
readonly hasOnUpdate: boolean;
|
|
44
|
+
/** Foreign key reference — table and column names. */
|
|
45
|
+
readonly referencesTable: string | null;
|
|
46
|
+
readonly referencesColumn: string | null;
|
|
47
|
+
/** Foreign key referential actions. */
|
|
48
|
+
readonly onDelete: ForeignKeyAction | null;
|
|
49
|
+
readonly onUpdate: ForeignKeyAction | null;
|
|
50
|
+
/** Converts logical value → storage value. Called by the builder. */
|
|
51
|
+
readonly encode: (value: T) => unknown;
|
|
52
|
+
/** Converts storage value → logical value. Called by the builder. */
|
|
53
|
+
readonly decode: (value: unknown) => T;
|
|
54
|
+
/** Table name — set by table() when the column is attached. Used for join disambiguation. */
|
|
55
|
+
readonly tableName: string | null;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** An integer column definition with auto-increment support. */
|
|
59
|
+
export interface IntegerColumnDef<S extends string = 'integer'> extends ColumnDef<number, S> {
|
|
60
|
+
primaryKey(): IntegerColumnDef<S>;
|
|
61
|
+
notNull(): IntegerColumnDef<S>;
|
|
62
|
+
unique(): IntegerColumnDef<S>;
|
|
63
|
+
default(value: number): IntegerColumnDef<S>;
|
|
64
|
+
defaultFn(fn: () => number): IntegerColumnDef<S>;
|
|
65
|
+
/**
|
|
66
|
+
* Mark as auto-increment (SQLite ROWID alias).
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* const id = integer("id").primaryKey().autoIncrement();
|
|
70
|
+
*/
|
|
71
|
+
autoIncrement(): IntegerColumnDef<S>;
|
|
72
|
+
}
|
|
73
|
+
/** A date column that stores a unix timestamp (milliseconds) in SQLite. Nullable in results unless `.defaultNow()` is called. */
|
|
74
|
+
export interface DateColumnDef extends ColumnDef<Date, 'integer'> {
|
|
75
|
+
primaryKey(): DateColumnDef;
|
|
76
|
+
notNull(): DateColumnDef;
|
|
77
|
+
unique(): DateColumnDef;
|
|
78
|
+
default(value: Date): DateColumnDef;
|
|
79
|
+
defaultFn(fn: () => Date): DateColumnDef;
|
|
80
|
+
/**
|
|
81
|
+
* Use `Date.now()` as the default when the value is omitted during insert.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const createdAt = date("created_at").defaultNow();
|
|
85
|
+
*/
|
|
86
|
+
defaultNow(): DateColumnDefWithDefault;
|
|
87
|
+
/**
|
|
88
|
+
* Always set to `Date.now()` on update, regardless of the provided value.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* const updatedAt = date("updated_at").onUpdateTimestamp();
|
|
92
|
+
*/
|
|
93
|
+
onUpdateTimestamp(): DateColumnDef;
|
|
94
|
+
}
|
|
95
|
+
/** A date column with a guaranteed default value, making it non-nullable in query results. */
|
|
96
|
+
export interface DateColumnDefWithDefault extends DateColumnDef {
|
|
97
|
+
primaryKey(): DateColumnDefWithDefault;
|
|
98
|
+
notNull(): DateColumnDefWithDefault;
|
|
99
|
+
unique(): DateColumnDefWithDefault;
|
|
100
|
+
default(value: Date): DateColumnDefWithDefault;
|
|
101
|
+
defaultFn(fn: () => Date): DateColumnDefWithDefault;
|
|
102
|
+
defaultNow(): DateColumnDefWithDefault;
|
|
103
|
+
onUpdateTimestamp(): DateColumnDefWithDefault;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Create a text column.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* const name = text("name").notNull();
|
|
110
|
+
*/
|
|
111
|
+
export declare function text(name?: string): ColumnDef<string, 'text'>;
|
|
112
|
+
/**
|
|
113
|
+
* Create an integer column.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* const id = integer("id").primaryKey().autoIncrement();
|
|
117
|
+
*/
|
|
118
|
+
export declare function integer(name?: string): IntegerColumnDef<'integer'>;
|
|
119
|
+
/**
|
|
120
|
+
* Create a boolean column. Stores `0`/`1` in SQLite, exposed as `boolean` in TypeScript.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* const active = boolean("active").notNull().default(true);
|
|
124
|
+
*/
|
|
125
|
+
export declare function boolean(name?: string): ColumnDef<boolean, 'integer'>;
|
|
126
|
+
/**
|
|
127
|
+
* Create a JSON column. Stores JSON text in SQLite, exposed as `T` in TypeScript.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* const meta = json<Record<string, unknown>>("meta").default({});
|
|
131
|
+
*/
|
|
132
|
+
export declare function json<T>(name?: string): ColumnDef<T, 'text'>;
|
|
133
|
+
/**
|
|
134
|
+
* Create a date column. Stores a unix timestamp (milliseconds) in SQLite, exposed as `Date` in TypeScript.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* const createdAt = date("created_at").defaultNow().notNull();
|
|
138
|
+
*/
|
|
139
|
+
export declare function date(name?: string): DateColumnDef;
|
|
140
|
+
/**
|
|
141
|
+
* Create a real (floating-point) column.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* const price = real("price").notNull();
|
|
145
|
+
*/
|
|
146
|
+
export declare function real(name?: string): ColumnDef<number, 'real'>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { ColumnDef, DateColumnDef, DateColumnDefWithDefault } from './columns';
|
|
2
|
+
/** A table definition mapping column names to their `ColumnDef`s. */
|
|
3
|
+
export type TableDef<T> = T & {
|
|
4
|
+
readonly _: {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export declare function table<T extends Record<string, ColumnDef<any, any>>>(name: string, columns: T, indexFn?: (t: TableDef<T>) => IndexBuilder[]): TableDef<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Derive the row shape from a table's column definitions.
|
|
11
|
+
* DateColumnDef → Date | null (nullable unless defaultNow() was called)
|
|
12
|
+
* DateColumnDefWithDefault → Date (non-nullable, has a guaranteed default)
|
|
13
|
+
* All other columns → their _type as-is.
|
|
14
|
+
*
|
|
15
|
+
* Uses `infer C` to extract the column record from TableDef<T> so TypeScript
|
|
16
|
+
* evaluates the mapped type against the concrete columns, not the intersection.
|
|
17
|
+
* This produces cleaner hover info: { id: string; name: string } instead of
|
|
18
|
+
* Pick<InferRow<TableDef<{...}>>, ...>.
|
|
19
|
+
*/
|
|
20
|
+
export type InferRow<T extends TableDef<any>> = T extends TableDef<infer C> ? {
|
|
21
|
+
[K in keyof Omit<C, '_'>]: C[K] extends DateColumnDefWithDefault ? Date : C[K] extends DateColumnDef ? Date | null : C[K] extends ColumnDef<any, any> ? C[K]['__internal']['_type'] : never;
|
|
22
|
+
} : never;
|
|
23
|
+
/** The row type for inserts — columns with auto-defaults (integer, date) are optional. */
|
|
24
|
+
export type InsertRow<T extends TableDef<any>> = {
|
|
25
|
+
[K in keyof Omit<T, '_'> as HasAutoDefault<T[K]> extends true ? never : K]: T[K] extends ColumnDef<any, any> ? T[K]['__internal']['_type'] : never;
|
|
26
|
+
} & {
|
|
27
|
+
[K in keyof Omit<T, '_'> as HasAutoDefault<T[K]> extends true ? K : never]?: T[K] extends ColumnDef<any, any> ? T[K]['__internal']['_type'] : never;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Define a table with auto snake_case column names.
|
|
31
|
+
* Column names are inferred from object keys and converted to snake_case.
|
|
32
|
+
* Column constructors can be called without a name.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const users = snakeCase.table("users", {
|
|
36
|
+
* id: text().primaryKey(),
|
|
37
|
+
* firstName: text(),
|
|
38
|
+
* });
|
|
39
|
+
*/
|
|
40
|
+
export declare function snakeCaseTable<T extends Record<string, ColumnDef<any, any>>>(tableName: string, columns: T, indexFn?: (t: TableDef<T>) => IndexBuilder[]): TableDef<T>;
|
|
41
|
+
/** Provides `snakeCase.table()` for auto snake_case column naming. */
|
|
42
|
+
export declare const snakeCase: {
|
|
43
|
+
table: typeof snakeCaseTable;
|
|
44
|
+
};
|
|
45
|
+
/** An index definition — used internally by the migration system. */
|
|
46
|
+
export interface IndexDef {
|
|
47
|
+
name: string;
|
|
48
|
+
columns: string[];
|
|
49
|
+
unique: boolean;
|
|
50
|
+
}
|
|
51
|
+
/** Public builder returned by `index()` — chain `.on(columns)` then `.unique()`. */
|
|
52
|
+
export interface IndexBuilder {
|
|
53
|
+
/** Add one or more columns to the index. */
|
|
54
|
+
on(...columns: ColumnDef<any, any>[]): IndexBuilder;
|
|
55
|
+
/** Mark the index as unique. */
|
|
56
|
+
unique(): IndexBuilder;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create an index definition using a chainable API.
|
|
60
|
+
*
|
|
61
|
+
* @param name - The index name (will be used as-is in SQL)
|
|
62
|
+
* @returns An IndexBuilder — chain `.on(columns)` then `.unique()`
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* const users = table("users", {
|
|
66
|
+
* id: text("id").primaryKey(),
|
|
67
|
+
* email: text("email"),
|
|
68
|
+
* name: text("name"),
|
|
69
|
+
* }, (t) => [
|
|
70
|
+
* index("idx_users_email").on(t.email).unique(),
|
|
71
|
+
* index("idx_users_name").on(t.name),
|
|
72
|
+
* ]);
|
|
73
|
+
*/
|
|
74
|
+
export declare function index(name: string): IndexBuilderInternal;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import type { SchemaState } from '../migration/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Read the live database schema and return a SchemaState.
|
|
5
|
+
*
|
|
6
|
+
* This reads tables, columns, indexes, and foreign key references from
|
|
7
|
+
* the SQLite database using PRAGMAs.
|
|
8
|
+
*
|
|
9
|
+
* @param client - The bun:sqlite Database instance
|
|
10
|
+
* @returns SchemaState representing the live database schema
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* import { Database } from "bun:sqlite";
|
|
14
|
+
* import { introspect } from "flint-orm/sqlite/introspect";
|
|
15
|
+
*
|
|
16
|
+
* const client = new Database("app.db");
|
|
17
|
+
* const state = introspect(client);
|
|
18
|
+
*/
|
|
19
|
+
export declare function introspect(client: Database): SchemaState;
|