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,98 @@
|
|
|
1
|
+
/** Serialized column definition — what lives in state.json. */
|
|
2
|
+
export interface SerializedColumn {
|
|
3
|
+
name: string;
|
|
4
|
+
sqlType: 'text' | 'integer' | 'real' | 'blob';
|
|
5
|
+
isPrimaryKey: boolean;
|
|
6
|
+
isNotNull: boolean;
|
|
7
|
+
isUnique: boolean;
|
|
8
|
+
hasDefault: boolean;
|
|
9
|
+
defaultValue?: unknown;
|
|
10
|
+
referencesTable?: string;
|
|
11
|
+
referencesColumn?: string;
|
|
12
|
+
onDelete?: string;
|
|
13
|
+
onUpdate?: string;
|
|
14
|
+
}
|
|
15
|
+
/** Serialized table definition — what lives in state.json. */
|
|
16
|
+
export interface SerializedTable {
|
|
17
|
+
name: string;
|
|
18
|
+
columns: SerializedColumn[];
|
|
19
|
+
indexes: SerializedIndex[];
|
|
20
|
+
}
|
|
21
|
+
/** Serialized index definition. */
|
|
22
|
+
export interface SerializedIndex {
|
|
23
|
+
name: string;
|
|
24
|
+
columns: string[];
|
|
25
|
+
unique: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** Full schema state — the content of state.json. */
|
|
28
|
+
export interface SchemaState {
|
|
29
|
+
version: number;
|
|
30
|
+
tables: Record<string, SerializedTable>;
|
|
31
|
+
}
|
|
32
|
+
export interface AddTableOp {
|
|
33
|
+
type: 'addTable';
|
|
34
|
+
table: SerializedTable;
|
|
35
|
+
}
|
|
36
|
+
export interface DropTableOp {
|
|
37
|
+
type: 'dropTable';
|
|
38
|
+
tableName: string;
|
|
39
|
+
columns?: SerializedColumn[];
|
|
40
|
+
}
|
|
41
|
+
export interface RenameTableOp {
|
|
42
|
+
type: 'renameTable';
|
|
43
|
+
from: string;
|
|
44
|
+
to: string;
|
|
45
|
+
}
|
|
46
|
+
export interface AddColumnOp {
|
|
47
|
+
type: 'addColumn';
|
|
48
|
+
tableName: string;
|
|
49
|
+
column: SerializedColumn;
|
|
50
|
+
}
|
|
51
|
+
export interface DropColumnOp {
|
|
52
|
+
type: 'dropColumn';
|
|
53
|
+
tableName: string;
|
|
54
|
+
columnName: string;
|
|
55
|
+
}
|
|
56
|
+
export interface RenameColumnOp {
|
|
57
|
+
type: 'renameColumn';
|
|
58
|
+
tableName: string;
|
|
59
|
+
from: string;
|
|
60
|
+
to: string;
|
|
61
|
+
}
|
|
62
|
+
export interface CreateIndexOp {
|
|
63
|
+
type: 'createIndex';
|
|
64
|
+
tableName: string;
|
|
65
|
+
index: SerializedIndex;
|
|
66
|
+
}
|
|
67
|
+
export interface DropIndexOp {
|
|
68
|
+
type: 'dropIndex';
|
|
69
|
+
indexName: string;
|
|
70
|
+
}
|
|
71
|
+
export interface ModifyColumnOp {
|
|
72
|
+
type: 'modifyColumn';
|
|
73
|
+
tableName: string;
|
|
74
|
+
columnName: string;
|
|
75
|
+
changes: {
|
|
76
|
+
isNotNull?: boolean;
|
|
77
|
+
hasDefault?: boolean;
|
|
78
|
+
defaultValue?: unknown;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export interface ModifyIndexOp {
|
|
82
|
+
type: 'modifyIndex';
|
|
83
|
+
tableName: string;
|
|
84
|
+
indexName: string;
|
|
85
|
+
from: SerializedIndex;
|
|
86
|
+
to: SerializedIndex;
|
|
87
|
+
}
|
|
88
|
+
export interface RebuildTableOp {
|
|
89
|
+
type: 'rebuildTable';
|
|
90
|
+
tableName: string;
|
|
91
|
+
oldTable: SerializedTable;
|
|
92
|
+
newTable: SerializedTable;
|
|
93
|
+
}
|
|
94
|
+
export type MigrationOperation = AddTableOp | DropTableOp | RenameTableOp | AddColumnOp | DropColumnOp | RenameColumnOp | CreateIndexOp | DropIndexOp | ModifyColumnOp | ModifyIndexOp | RebuildTableOp;
|
|
95
|
+
export interface MigrationFile {
|
|
96
|
+
name: string;
|
|
97
|
+
operations: MigrationOperation[];
|
|
98
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Executor } from '../executor';
|
|
2
|
+
import type { ColumnDef } from '../schema/columns';
|
|
3
|
+
import type { AnyTable } from '../schema/table';
|
|
4
|
+
import type { Condition } from './conditions';
|
|
5
|
+
/**
|
|
6
|
+
* Count all rows in a table, optionally filtered by a condition.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const total = await db.count(users);
|
|
10
|
+
* const active = await db.count(users, eq(users.active, true));
|
|
11
|
+
*/
|
|
12
|
+
export declare function count<T extends AnyTable>(executor: Executor, table: T, condition?: Condition): Promise<number>;
|
|
13
|
+
/**
|
|
14
|
+
* Count non-null values of a column, optionally filtered by a condition.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const count = await db.countColumn(users, users.email);
|
|
18
|
+
*/
|
|
19
|
+
export declare function countColumn<T extends AnyTable, C extends ColumnDef<any, any>>(executor: Executor, table: T, column: C, condition?: Condition): Promise<number>;
|
|
20
|
+
/**
|
|
21
|
+
* Sum non-null values of a column, optionally filtered by a condition.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const total = await db.sum(orders, orders.amount);
|
|
25
|
+
*/
|
|
26
|
+
export declare function sum<T extends AnyTable, C extends ColumnDef<any, any>>(executor: Executor, table: T, column: C, condition?: Condition): Promise<number | null>;
|
|
27
|
+
/**
|
|
28
|
+
* Average non-null values of a column, optionally filtered by a condition.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const avgAge = await db.avg(users, users.age);
|
|
32
|
+
*/
|
|
33
|
+
export declare function avg<T extends AnyTable, C extends ColumnDef<any, any>>(executor: Executor, table: T, column: C, condition?: Condition): Promise<number | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Find the minimum non-null value of a column, optionally filtered by a condition.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const minAge = await db.min(users, users.age);
|
|
39
|
+
*/
|
|
40
|
+
export declare function min<T extends AnyTable, C extends ColumnDef<any, any>>(executor: Executor, table: T, column: C, condition?: Condition): Promise<number | null>;
|
|
41
|
+
/**
|
|
42
|
+
* Find the maximum non-null value of a column, optionally filtered by a condition.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const maxAge = await db.max(users, users.age);
|
|
46
|
+
*/
|
|
47
|
+
export declare function max<T extends AnyTable, C extends ColumnDef<any, any>>(executor: Executor, table: T, column: C, condition?: Condition): Promise<number | null>;
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import type { ColumnDef } from '../schema/columns';
|
|
2
|
+
import type { Condition } from './conditions';
|
|
3
|
+
import type { AnyTable, InferRow, InsertRow } from '../schema/table';
|
|
4
|
+
import type { Executor } from '../executor';
|
|
5
|
+
/**
|
|
6
|
+
* Narrow a row type to specific columns using a mapped type.
|
|
7
|
+
* Unlike Pick, TypeScript eagerly evaluates { [K in C]: T[K] } to a concrete
|
|
8
|
+
* shape in hover info: { id: string; name: string } instead of Pick<...>.
|
|
9
|
+
*/
|
|
10
|
+
export type NarrowRow<T, C extends keyof T> = {
|
|
11
|
+
[K in C]: T[K];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Force TypeScript to expand intersection/mapped types into a flat object
|
|
15
|
+
* in hover info: Prettify<{ id: string } & { name: string }> → { id: string; name: string }.
|
|
16
|
+
*/
|
|
17
|
+
export type Prettify<T> = {
|
|
18
|
+
[K in keyof T]: T[K];
|
|
19
|
+
} & {};
|
|
20
|
+
/** Anything that can produce SQL — used by `db.batch()`. */
|
|
21
|
+
export interface Executable {
|
|
22
|
+
toSQL(): {
|
|
23
|
+
sql: string;
|
|
24
|
+
params: unknown[];
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** Phase 1 of a SELECT — only `.from()` is available. */
|
|
28
|
+
export interface SelectStage1 {
|
|
29
|
+
from<U extends AnyTable>(table: U): SelectBuilder<U>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Full SELECT builder — available after `.from()`.
|
|
33
|
+
* Returns `InferRow<T>[]` (all columns) from `execute()`.
|
|
34
|
+
* Call `.columns()` to narrow — returns a `NarrowedSelectBuilder`.
|
|
35
|
+
*/
|
|
36
|
+
export declare class SelectBuilder<T extends AnyTable> implements Executable {
|
|
37
|
+
#private;
|
|
38
|
+
constructor(executor: Executor, tableName: string, table: T, conditions?: Condition[], selectedColumns?: (keyof InferRow<T>)[] | null, orderByClauses?: {
|
|
39
|
+
column: ColumnDef<any, any>;
|
|
40
|
+
direction: 'asc' | 'desc';
|
|
41
|
+
}[], limitValue?: number | null, offsetValue?: number | null, distinct?: boolean);
|
|
42
|
+
/** Add a WHERE condition. Multiple calls stack. */
|
|
43
|
+
where(condition: Condition): SelectBuilder<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Narrow which columns appear in the result.
|
|
46
|
+
* Returns a `NarrowedSelectBuilder` with a clean `{ id: string; name: string }` return type.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* db.select().from(users).columns(["id", "name"]).execute()
|
|
50
|
+
* // ^? { id: string; name: string }[]
|
|
51
|
+
*/
|
|
52
|
+
columns<K extends keyof InferRow<T>>(keys: K[]): NarrowedSelectBuilder<T, K>;
|
|
53
|
+
/** Return a single row or null instead of an array. Adds `LIMIT 1` to the SQL. */
|
|
54
|
+
single(): SingleSelectBuilder<T>;
|
|
55
|
+
/** Return only distinct (unique) rows. */
|
|
56
|
+
distinct(): SelectBuilder<T>;
|
|
57
|
+
/** Add an ORDER BY clause. Multiple calls stack. */
|
|
58
|
+
orderBy<K extends keyof InferRow<T>>(key: K, direction?: 'asc' | 'desc'): SelectBuilder<T>;
|
|
59
|
+
/** Limit the number of results. */
|
|
60
|
+
limit(n: number): SelectBuilder<T>;
|
|
61
|
+
/** Skip N rows before returning results. */
|
|
62
|
+
offset(n: number): SelectBuilder<T>;
|
|
63
|
+
toSQL(): {
|
|
64
|
+
sql: string;
|
|
65
|
+
params: unknown[];
|
|
66
|
+
};
|
|
67
|
+
/** Execute the query and return all matching rows. */
|
|
68
|
+
execute(): Promise<InferRow<T>[]>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Narrowed SELECT builder — after `.columns()` has been called.
|
|
72
|
+
* Returns `NarrowRow<InferRow<T>, C>[]` from `execute()` — a clean
|
|
73
|
+
* `{ id: string; name: string }` shape, no Pick wrapper.
|
|
74
|
+
*/
|
|
75
|
+
export declare class NarrowedSelectBuilder<T extends AnyTable, C extends keyof InferRow<T>> implements Executable {
|
|
76
|
+
#private;
|
|
77
|
+
constructor(executor: Executor, tableName: string, table: T, conditions: Condition[], selectedColumns: C[], orderByClauses: {
|
|
78
|
+
column: ColumnDef<any, any>;
|
|
79
|
+
direction: 'asc' | 'desc';
|
|
80
|
+
}[], limitValue: number | null, offsetValue: number | null, distinct: boolean);
|
|
81
|
+
where(condition: Condition): NarrowedSelectBuilder<T, C>;
|
|
82
|
+
distinct(): NarrowedSelectBuilder<T, C>;
|
|
83
|
+
single(): NarrowedSingleSelectBuilder<T, C>;
|
|
84
|
+
orderBy<K extends keyof InferRow<T>>(key: K, direction?: 'asc' | 'desc'): NarrowedSelectBuilder<T, C>;
|
|
85
|
+
limit(n: number): NarrowedSelectBuilder<T, C>;
|
|
86
|
+
offset(n: number): NarrowedSelectBuilder<T, C>;
|
|
87
|
+
toSQL(): {
|
|
88
|
+
sql: string;
|
|
89
|
+
params: unknown[];
|
|
90
|
+
};
|
|
91
|
+
/** Execute the query and return narrowed rows. */
|
|
92
|
+
execute(): Promise<Prettify<NarrowRow<InferRow<T>, C>>[]>;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Single-row SELECT builder — after `.single()` on a `SelectBuilder`.
|
|
96
|
+
* Returns `InferRow<T> | null` from `execute()`.
|
|
97
|
+
*/
|
|
98
|
+
export declare class SingleSelectBuilder<T extends AnyTable> implements Executable {
|
|
99
|
+
#private;
|
|
100
|
+
constructor(executor: Executor, tableName: string, table: T, conditions: Condition[], selectedColumns?: (keyof InferRow<T>)[] | null, orderByClauses?: {
|
|
101
|
+
column: ColumnDef<any, any>;
|
|
102
|
+
direction: 'asc' | 'desc';
|
|
103
|
+
}[], offsetValue?: number | null, distinct?: boolean);
|
|
104
|
+
where(condition: Condition): SingleSelectBuilder<T>;
|
|
105
|
+
orderBy<K extends keyof InferRow<T>>(key: K, direction?: 'asc' | 'desc'): SingleSelectBuilder<T>;
|
|
106
|
+
offset(n: number): SingleSelectBuilder<T>;
|
|
107
|
+
toSQL(): {
|
|
108
|
+
sql: string;
|
|
109
|
+
params: unknown[];
|
|
110
|
+
};
|
|
111
|
+
/** Returns a single row or null, never throws on empty results. */
|
|
112
|
+
execute(): Promise<InferRow<T> | null>;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Narrowed single-row SELECT builder — after `.single()` on a `NarrowedSelectBuilder`.
|
|
116
|
+
* Returns `NarrowRow<InferRow<T>, C> | null` from `execute()`.
|
|
117
|
+
*/
|
|
118
|
+
export declare class NarrowedSingleSelectBuilder<T extends AnyTable, C extends keyof InferRow<T>> implements Executable {
|
|
119
|
+
#private;
|
|
120
|
+
constructor(executor: Executor, tableName: string, table: T, conditions: Condition[], selectedColumns: C[], orderByClauses: {
|
|
121
|
+
column: ColumnDef<any, any>;
|
|
122
|
+
direction: 'asc' | 'desc';
|
|
123
|
+
}[], offsetValue: number | null, distinct: boolean);
|
|
124
|
+
where(condition: Condition): NarrowedSingleSelectBuilder<T, C>;
|
|
125
|
+
orderBy<K extends keyof InferRow<T>>(key: K, direction?: 'asc' | 'desc'): NarrowedSingleSelectBuilder<T, C>;
|
|
126
|
+
offset(n: number): NarrowedSingleSelectBuilder<T, C>;
|
|
127
|
+
toSQL(): {
|
|
128
|
+
sql: string;
|
|
129
|
+
params: unknown[];
|
|
130
|
+
};
|
|
131
|
+
/** Returns a single narrowed row or null. */
|
|
132
|
+
execute(): Promise<Prettify<NarrowRow<InferRow<T>, C>> | null>;
|
|
133
|
+
}
|
|
134
|
+
/** Phase 1 of a JOIN — only `.on()` is available. */
|
|
135
|
+
export interface JoinSelectStage1<Parent extends AnyTable> {
|
|
136
|
+
on<Child extends AnyTable>(child: Child, condition: Condition): JoinBuilder<Parent, [Child]>;
|
|
137
|
+
/** Auto-join: infer condition from foreign key references. */
|
|
138
|
+
on<Child extends AnyTable>(child: Child): JoinBuilder<Parent, [Child]>;
|
|
139
|
+
}
|
|
140
|
+
/** Full join builder — chain more `.on()` calls or call `.execute()`. */
|
|
141
|
+
export interface JoinBuilder<Parent extends AnyTable, Joined extends AnyTable[], ParentCols extends keyof InferRow<Parent> = keyof InferRow<Parent>> {
|
|
142
|
+
on<NewChild extends AnyTable>(child: NewChild, condition: Condition): JoinBuilder<Parent, [...Joined, NewChild], ParentCols>;
|
|
143
|
+
/** Auto-join: infer condition from foreign key references. */
|
|
144
|
+
on<NewChild extends AnyTable>(child: NewChild): JoinBuilder<Parent, [...Joined, NewChild], ParentCols>;
|
|
145
|
+
columns<K extends keyof InferRow<Parent>>(keys: K[]): JoinBuilder<Parent, Joined, K>;
|
|
146
|
+
where(condition: Condition): JoinBuilder<Parent, Joined, ParentCols>;
|
|
147
|
+
orderBy<K extends keyof InferRow<Parent>>(key: K, direction?: 'asc' | 'desc'): JoinBuilder<Parent, Joined, ParentCols>;
|
|
148
|
+
limit(n: number): JoinBuilder<Parent, Joined, ParentCols>;
|
|
149
|
+
offset(n: number): JoinBuilder<Parent, Joined, ParentCols>;
|
|
150
|
+
single(): SingleJoinBuilder<Parent, Joined, ParentCols>;
|
|
151
|
+
toSQL(): {
|
|
152
|
+
sql: string;
|
|
153
|
+
params: unknown[];
|
|
154
|
+
};
|
|
155
|
+
execute(): Promise<JoinResult<Parent, Joined, ParentCols>[]>;
|
|
156
|
+
}
|
|
157
|
+
/** The result type for joined queries. Parent fields are narrowed by `.columns()`; each joined table's data is nested under its table name. */
|
|
158
|
+
export type JoinResult<Parent extends AnyTable, _Joined extends AnyTable[], ParentCols extends keyof InferRow<Parent> = keyof InferRow<Parent>> = Prettify<Pick<InferRow<Parent>, ParentCols>> & Record<string, unknown>;
|
|
159
|
+
/** Phase 1 of an INSERT — only `.values()` is available. */
|
|
160
|
+
export interface InsertStage1<T extends AnyTable> {
|
|
161
|
+
values(row: InsertRow<T>): InsertBuilder<T>;
|
|
162
|
+
values(rows: InsertRow<T>[]): InsertBuilder<T>;
|
|
163
|
+
}
|
|
164
|
+
type OnConflictDoUpdate<T extends AnyTable> = {
|
|
165
|
+
mode: 'update';
|
|
166
|
+
target: ColumnDef<any, any> | ColumnDef<any, any>[];
|
|
167
|
+
set: Partial<InferRow<T>>;
|
|
168
|
+
};
|
|
169
|
+
type OnConflictStrategy<T extends AnyTable> = OnConflictDoNothing | OnConflictDoUpdate<T>;
|
|
170
|
+
/** Full INSERT builder — available after `.values()` has been called. */
|
|
171
|
+
export declare class InsertBuilder<T extends AnyTable, R extends boolean = false, K extends keyof InferRow<T> = keyof InferRow<T>> implements Executable {
|
|
172
|
+
#private;
|
|
173
|
+
constructor(executor: Executor, tableName: string, table: T, rowOrRows: InsertRow<T> | InsertRow<T>[], returning?: boolean | K[], onConflict?: OnConflictStrategy<T>);
|
|
174
|
+
/**
|
|
175
|
+
* Return the inserted row(s) instead of void.
|
|
176
|
+
* Pass an array of column names to narrow the result shape.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* db.insert(users).values({ id: "u1", name: "Alice" }).returning()
|
|
180
|
+
* db.insert(users).values({ id: "u1", name: "Alice" }).returning(["id", "name"])
|
|
181
|
+
*/
|
|
182
|
+
returning(): InsertBuilder<T, true>;
|
|
183
|
+
returning<NewK extends keyof InferRow<T>>(keys: NewK[]): InsertBuilder<T, true, NewK>;
|
|
184
|
+
/**
|
|
185
|
+
* On conflict, do nothing (ignore the insert).
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* db.insert(users).values(row).onConflictDoNothing()
|
|
189
|
+
*/
|
|
190
|
+
onConflictDoNothing(): InsertBuilder<T, R, K>;
|
|
191
|
+
/**
|
|
192
|
+
* On conflict, update specified columns with the proposed values.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* db.insert(users).values(row).onConflictDoUpdate({
|
|
196
|
+
* target: users.id,
|
|
197
|
+
* set: { name: "Alice" },
|
|
198
|
+
* })
|
|
199
|
+
*/
|
|
200
|
+
onConflictDoUpdate<C extends ColumnDef<any, any>>(options: {
|
|
201
|
+
target: C | C[];
|
|
202
|
+
set: Partial<InferRow<T>>;
|
|
203
|
+
}): InsertBuilder<T, R, K>;
|
|
204
|
+
toSQL(): {
|
|
205
|
+
sql: string;
|
|
206
|
+
params: unknown[];
|
|
207
|
+
};
|
|
208
|
+
execute(): Promise<R extends true ? Prettify<NarrowRow<InferRow<T>, K>>[] : void>;
|
|
209
|
+
}
|
|
210
|
+
/** Phase 1 of an UPDATE — only `.set()` is available. */
|
|
211
|
+
export interface UpdateStage1<T extends AnyTable> {
|
|
212
|
+
set(partial: Partial<InferRow<T>>): UpdateBuilder<T>;
|
|
213
|
+
}
|
|
214
|
+
/** Full UPDATE builder — available after `.set()` has been called. */
|
|
215
|
+
export declare class UpdateBuilder<T extends AnyTable, R extends boolean = false, K extends keyof InferRow<T> = keyof InferRow<T>> implements Executable {
|
|
216
|
+
#private;
|
|
217
|
+
constructor(executor: Executor, tableName: string, table: T, set: Partial<InferRow<T>>, conditions?: Condition[], returning?: boolean | K[]);
|
|
218
|
+
set(partial: Partial<InferRow<T>>): UpdateBuilder<T, R, K>;
|
|
219
|
+
where(condition: Condition): UpdateBuilder<T, R, K>;
|
|
220
|
+
/**
|
|
221
|
+
* Return the updated row(s) instead of void.
|
|
222
|
+
* Pass an array of column names to narrow the result shape.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* db.update(users).set({ name: "Bob" }).where(eq(users.id, "u1")).returning()
|
|
226
|
+
* db.update(users).set({ name: "Bob" }).where(eq(users.id, "u1")).returning(["id", "name"])
|
|
227
|
+
*/
|
|
228
|
+
returning(): UpdateBuilder<T, true>;
|
|
229
|
+
returning<NewK extends keyof InferRow<T>>(keys: NewK[]): UpdateBuilder<T, true, NewK>;
|
|
230
|
+
toSQL(): {
|
|
231
|
+
sql: string;
|
|
232
|
+
params: unknown[];
|
|
233
|
+
};
|
|
234
|
+
execute(): Promise<R extends true ? Prettify<NarrowRow<InferRow<T>, K>>[] : void>;
|
|
235
|
+
}
|
|
236
|
+
/** Full DELETE builder — chain `.where()` calls then `.execute()`. */
|
|
237
|
+
export declare class DeleteBuilder<T extends AnyTable, R extends boolean = false, K extends keyof InferRow<T> = keyof InferRow<T>> implements Executable {
|
|
238
|
+
#private;
|
|
239
|
+
constructor(executor: Executor, tableName: string, table: T, conditions?: Condition[], returning?: boolean | K[]);
|
|
240
|
+
where(condition: Condition): DeleteBuilder<T, R, K>;
|
|
241
|
+
/**
|
|
242
|
+
* Return the deleted row(s) instead of void.
|
|
243
|
+
* Pass an array of column names to narrow the result shape.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* db.delete(users).where(eq(users.id, "u1")).returning()
|
|
247
|
+
* db.delete(users).where(eq(users.id, "u1")).returning(["id", "name"])
|
|
248
|
+
*/
|
|
249
|
+
returning(): DeleteBuilder<T, true>;
|
|
250
|
+
returning<NewK extends keyof InferRow<T>>(keys: NewK[]): DeleteBuilder<T, true, NewK>;
|
|
251
|
+
toSQL(): {
|
|
252
|
+
sql: string;
|
|
253
|
+
params: unknown[];
|
|
254
|
+
};
|
|
255
|
+
execute(): Promise<R extends true ? Prettify<NarrowRow<InferRow<T>, K>>[] : void>;
|
|
256
|
+
}
|
|
257
|
+
export {};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import type { ColumnDef } from '../schema/columns';
|
|
2
|
+
/** A condition node used in WHERE clauses. */
|
|
3
|
+
export type Condition = {
|
|
4
|
+
type: 'eq';
|
|
5
|
+
column: ColumnDef<any, any>;
|
|
6
|
+
value: unknown;
|
|
7
|
+
} | {
|
|
8
|
+
type: 'eqColumn';
|
|
9
|
+
left: ColumnDef<any, any>;
|
|
10
|
+
right: ColumnDef<any, any>;
|
|
11
|
+
} | {
|
|
12
|
+
type: 'gt';
|
|
13
|
+
column: ColumnDef<any, any>;
|
|
14
|
+
value: unknown;
|
|
15
|
+
} | {
|
|
16
|
+
type: 'gte';
|
|
17
|
+
column: ColumnDef<any, any>;
|
|
18
|
+
value: unknown;
|
|
19
|
+
} | {
|
|
20
|
+
type: 'lt';
|
|
21
|
+
column: ColumnDef<any, any>;
|
|
22
|
+
value: unknown;
|
|
23
|
+
} | {
|
|
24
|
+
type: 'lte';
|
|
25
|
+
column: ColumnDef<any, any>;
|
|
26
|
+
value: unknown;
|
|
27
|
+
} | {
|
|
28
|
+
type: 'neq';
|
|
29
|
+
column: ColumnDef<any, any>;
|
|
30
|
+
value: unknown;
|
|
31
|
+
} | {
|
|
32
|
+
type: 'in';
|
|
33
|
+
column: ColumnDef<any, any>;
|
|
34
|
+
values: unknown[];
|
|
35
|
+
} | {
|
|
36
|
+
type: 'notIn';
|
|
37
|
+
column: ColumnDef<any, any>;
|
|
38
|
+
values: unknown[];
|
|
39
|
+
} | {
|
|
40
|
+
type: 'isNull';
|
|
41
|
+
column: ColumnDef<any, any>;
|
|
42
|
+
} | {
|
|
43
|
+
type: 'isNotNull';
|
|
44
|
+
column: ColumnDef<any, any>;
|
|
45
|
+
} | {
|
|
46
|
+
type: 'like';
|
|
47
|
+
column: ColumnDef<any, any>;
|
|
48
|
+
pattern: string;
|
|
49
|
+
} | {
|
|
50
|
+
type: 'glob';
|
|
51
|
+
column: ColumnDef<any, any>;
|
|
52
|
+
pattern: string;
|
|
53
|
+
} | {
|
|
54
|
+
type: 'between';
|
|
55
|
+
column: ColumnDef<any, any>;
|
|
56
|
+
low: unknown;
|
|
57
|
+
high: unknown;
|
|
58
|
+
} | {
|
|
59
|
+
type: 'and';
|
|
60
|
+
conditions: Condition[];
|
|
61
|
+
} | {
|
|
62
|
+
type: 'or';
|
|
63
|
+
conditions: Condition[];
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Check if a column equals a value, or if two columns are equal.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* // Value comparison
|
|
70
|
+
* where(eq(users.name, "Alice"))
|
|
71
|
+
*
|
|
72
|
+
* // Column-to-column comparison
|
|
73
|
+
* where(eq(orders.userId, users.id))
|
|
74
|
+
*/
|
|
75
|
+
export declare function eq<T>(column: ColumnDef<T, any>, value: T): Condition;
|
|
76
|
+
export declare function eq<T>(left: ColumnDef<T, any>, right: ColumnDef<T, any>): Condition;
|
|
77
|
+
/**
|
|
78
|
+
* Combine conditions with AND.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* where(and(eq(users.name, "Alice"), eq(users.active, true)))
|
|
82
|
+
*/
|
|
83
|
+
export declare function and(...conditions: Condition[]): Condition;
|
|
84
|
+
/**
|
|
85
|
+
* Combine conditions with OR.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* where(or(eq(users.role, "admin"), eq(users.role, "moderator")))
|
|
89
|
+
*/
|
|
90
|
+
export declare function or(...conditions: Condition[]): Condition;
|
|
91
|
+
/**
|
|
92
|
+
* Check if a column's value is in the given array.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* where(isIn(users.id, ["u1", "u2", "u3"]))
|
|
96
|
+
*/
|
|
97
|
+
export declare function isIn<T>(column: ColumnDef<T, any>, values: T[]): Condition;
|
|
98
|
+
/**
|
|
99
|
+
* Check if a column's value is not in the given array.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* where(isNotIn(users.id, ["u4", "u5"]))
|
|
103
|
+
*/
|
|
104
|
+
export declare function isNotIn<T>(column: ColumnDef<T, any>, values: T[]): Condition;
|
|
105
|
+
/**
|
|
106
|
+
* Check if a column is NULL.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* where(isNull(users.deletedAt))
|
|
110
|
+
*/
|
|
111
|
+
export declare function isNull(column: ColumnDef<any, any>): Condition;
|
|
112
|
+
/**
|
|
113
|
+
* Check if a column is NOT NULL.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* where(isNotNull(users.name))
|
|
117
|
+
*/
|
|
118
|
+
export declare function isNotNull(column: ColumnDef<any, any>): Condition;
|
|
119
|
+
/**
|
|
120
|
+
* Pattern match using SQL `LIKE`. Use `%` for any sequence of characters, `_` for a single character.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* where(like(users.name, "A%"))
|
|
124
|
+
*/
|
|
125
|
+
export declare function like(column: ColumnDef<any, any>, pattern: string): Condition;
|
|
126
|
+
/**
|
|
127
|
+
* Pattern match using SQL `GLOB`. Use `*` for any sequence of characters, `?` for a single character.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* where(glob(users.name, "A*"))
|
|
131
|
+
*/
|
|
132
|
+
export declare function glob(column: ColumnDef<any, any>, pattern: string): Condition;
|
|
133
|
+
/**
|
|
134
|
+
* Check if a column's value is between `low` and `high` (inclusive).
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* where(between(users.age, 18, 65))
|
|
138
|
+
*/
|
|
139
|
+
export declare function between<T>(column: ColumnDef<T, any>, low: T, high: T): Condition;
|
|
140
|
+
/**
|
|
141
|
+
* Check if a column's value is greater than a value.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* where(gt(users.age, 18))
|
|
145
|
+
*/
|
|
146
|
+
export declare function gt<T>(column: ColumnDef<T, any>, value: T): Condition;
|
|
147
|
+
/**
|
|
148
|
+
* Check if a column's value is greater than or equal to a value.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* where(gte(users.age, 18))
|
|
152
|
+
*/
|
|
153
|
+
export declare function gte<T>(column: ColumnDef<T, any>, value: T): Condition;
|
|
154
|
+
/**
|
|
155
|
+
* Check if a column's value is less than a value.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* where(lt(users.age, 65))
|
|
159
|
+
*/
|
|
160
|
+
export declare function lt<T>(column: ColumnDef<T, any>, value: T): Condition;
|
|
161
|
+
/**
|
|
162
|
+
* Check if a column's value is less than or equal to a value.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* where(lte(users.age, 65))
|
|
166
|
+
*/
|
|
167
|
+
export declare function lte<T>(column: ColumnDef<T, any>, value: T): Condition;
|
|
168
|
+
/**
|
|
169
|
+
* Check if a column's value is not equal to a value.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* where(neq(users.id, "u1"))
|
|
173
|
+
*/
|
|
174
|
+
export declare function neq<T>(column: ColumnDef<T, any>, value: T): Condition;
|
|
175
|
+
export declare function compileCondition(cond: Condition, params: unknown[]): string;
|
|
176
|
+
export declare function compileConditions(conditions: Condition[], params: unknown[]): string;
|