@type32/tauri-sqlite-orm 0.2.12 → 0.2.15
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/README.md +14 -8
- package/dist/index.d.mts +219 -177
- package/dist/index.d.ts +219 -177
- package/dist/index.js +700 -817
- package/dist/index.mjs +704 -815
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ A Drizzle-like TypeScript ORM tailored for Tauri v2's `@tauri-apps/plugin-sql` (
|
|
|
7
7
|
- **Drizzle-like Schema:** Define your database schema using a familiar, chainable API.
|
|
8
8
|
- **Strict Type Inference:** Full TypeScript type safety with no `any` types - nullable columns, custom types, and required/optional fields are accurately inferred.
|
|
9
9
|
- **Type-Safe Query Builder:** Build SQL queries with TypeScript, ensuring type safety and autocompletion.
|
|
10
|
-
- **Relations Support:** Define and query one-to-one, one-to-many, and many-to-many relationships between tables.
|
|
10
|
+
- **Relations Support:** Define and query one-to-one, one-to-many, and many-to-many (via junction tables, Drizzle-style) relationships between tables.
|
|
11
11
|
- **Nested Includes:** Load relations of relations with intuitive nested syntax.
|
|
12
12
|
- **Advanced Operators:** Comprehensive set of operators including `ne`, `between`, `notIn`, `ilike`, `startsWith`, `endsWith`, `contains`, and more.
|
|
13
13
|
- **Subquery Support:** Use subqueries in WHERE and SELECT clauses with full type safety.
|
|
@@ -16,6 +16,7 @@ A Drizzle-like TypeScript ORM tailored for Tauri v2's `@tauri-apps/plugin-sql` (
|
|
|
16
16
|
- **Safety Features:** Automatic WHERE clause validation for UPDATE/DELETE prevents accidental data loss.
|
|
17
17
|
- **Increment/Decrement:** Atomic increment/decrement operations for safe counter updates.
|
|
18
18
|
- **Better Error Handling:** Custom error classes for clear, actionable error messages.
|
|
19
|
+
- **Cascade Actions:** `onDelete` and `onUpdate` (cascade, set null, set default, restrict, no action) for foreign key references.
|
|
19
20
|
- **Simplified Migrations:** Keep your database schema in sync with your application's models using automatic schema detection and migration tools.
|
|
20
21
|
- **Lightweight & Performant:** Designed to be a thin layer over the Tauri SQL plugin, ensuring minimal overhead.
|
|
21
22
|
|
|
@@ -33,20 +34,20 @@ Make sure the SQL plugin is registered on the Rust side (see Tauri docs).
|
|
|
33
34
|
|
|
34
35
|
```typescript
|
|
35
36
|
import Database from '@tauri-apps/plugin-sql'
|
|
36
|
-
import { TauriORM, sqliteTable,
|
|
37
|
+
import { TauriORM, sqliteTable, integer, text, relations, InferSelectModel, InferRelationalSelectModel } from '@type32/tauri-sqlite-orm'
|
|
37
38
|
|
|
38
39
|
// Define tables
|
|
39
40
|
const users = sqliteTable('users', {
|
|
40
|
-
id:
|
|
41
|
+
id: integer('id').primaryKey().autoincrement(),
|
|
41
42
|
name: text('name').notNull(),
|
|
42
43
|
email: text('email').notNull().unique(),
|
|
43
44
|
})
|
|
44
45
|
|
|
45
46
|
const posts = sqliteTable('posts', {
|
|
46
|
-
id:
|
|
47
|
+
id: integer('id').primaryKey().autoincrement(),
|
|
47
48
|
title: text('title').notNull(),
|
|
48
49
|
content: text('content').notNull(),
|
|
49
|
-
userId:
|
|
50
|
+
userId: integer('user_id').notNull().references(users, users._.columns.id, { onDelete: 'cascade' }),
|
|
50
51
|
})
|
|
51
52
|
|
|
52
53
|
// Define relations
|
|
@@ -78,6 +79,11 @@ const usersWithPosts = await orm
|
|
|
78
79
|
.select(users)
|
|
79
80
|
.include({ posts: true })
|
|
80
81
|
.all()
|
|
82
|
+
|
|
83
|
+
// Type relational results with InferRelationalSelectModel
|
|
84
|
+
type User = InferSelectModel<typeof users>
|
|
85
|
+
const withPosts = { posts: true } as const
|
|
86
|
+
type UserWithPosts = InferRelationalSelectModel<typeof users, typeof usersRelations, typeof withPosts>
|
|
81
87
|
```
|
|
82
88
|
|
|
83
89
|
### Documentation
|
|
@@ -88,10 +94,10 @@ const usersWithPosts = await orm
|
|
|
88
94
|
|
|
89
95
|
### Relations
|
|
90
96
|
|
|
91
|
-
The ORM supports
|
|
97
|
+
The ORM supports relations in a Drizzle-style pattern:
|
|
92
98
|
|
|
93
|
-
1. **One-to-One / Many-to-One**: Use `one()` to define a relation where the current table references another table
|
|
99
|
+
1. **One-to-One / Many-to-One**: Use `one()` with `fields` and `references` to define a relation where the current table references another table
|
|
94
100
|
2. **One-to-Many**: Use `many()` to define a relation where another table references the current table
|
|
95
|
-
3. **Many-to-Many**: Use `
|
|
101
|
+
3. **Many-to-Many**: Use `many(junctionTable)` on both sides and define `one()` on the junction with `fields`/`references` to each entity. Load with nested includes: `include({ postTags: { with: { tag: true } } })`
|
|
96
102
|
|
|
97
103
|
See the [many-to-many example](./docs/many-to-many-example.md) for detailed usage.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,26 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Dialect, DialectAdapter, Driver, Kysely, DatabaseIntrospector, QueryCompiler, Expression, SqlBool } from 'kysely';
|
|
2
|
+
export { Expression, sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Minimal interface that any SQLite-over-Tauri (or compatible mock) database must satisfy.
|
|
6
|
+
* Using this instead of the concrete `@tauri-apps/plugin-sql` type keeps the dialect
|
|
7
|
+
* portable and testable outside a Tauri runtime.
|
|
8
|
+
*/
|
|
9
|
+
interface DatabaseLike {
|
|
10
|
+
select<T>(query: string, params?: any[]): Promise<T>;
|
|
11
|
+
execute(query: string, params?: any[]): Promise<{
|
|
12
|
+
lastInsertId: number;
|
|
13
|
+
rowsAffected: number;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
declare class TauriDialect implements Dialect {
|
|
17
|
+
private readonly db;
|
|
18
|
+
constructor(db: DatabaseLike);
|
|
19
|
+
createAdapter(): DialectAdapter;
|
|
20
|
+
createDriver(): Driver;
|
|
21
|
+
createIntrospector(db: Kysely<any>): DatabaseIntrospector;
|
|
22
|
+
createQueryCompiler(): QueryCompiler;
|
|
23
|
+
}
|
|
2
24
|
|
|
3
25
|
type ColumnDataType = 'TEXT' | 'INTEGER' | 'REAL' | 'BLOB' | 'BOOLEAN' | 'NUMERIC';
|
|
4
26
|
type Mode = 'default' | 'timestamp' | 'timestamp_ms' | 'json' | 'boolean' | 'bigint';
|
|
@@ -13,6 +35,8 @@ interface ColumnOptions<TData, TEnum extends readonly string[] = readonly string
|
|
|
13
35
|
references?: {
|
|
14
36
|
table: AnyTable;
|
|
15
37
|
column: AnySQLiteColumn;
|
|
38
|
+
onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
|
|
39
|
+
onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
|
|
16
40
|
};
|
|
17
41
|
mode?: Mode;
|
|
18
42
|
$onUpdateFn?: () => TData;
|
|
@@ -28,69 +52,77 @@ type AnyTable = Table<Record<string, AnySQLiteColumn>, string>;
|
|
|
28
52
|
type InferSelectModel<T extends AnyTable> = {
|
|
29
53
|
[K in keyof T['_']['columns']]: ExtractColumnType<T['_']['columns'][K]>;
|
|
30
54
|
};
|
|
31
|
-
type RelationType = 'one' | 'many'
|
|
55
|
+
type RelationType = 'one' | 'many';
|
|
32
56
|
interface RelationConfig {
|
|
33
57
|
type: RelationType;
|
|
34
58
|
foreignTable: AnyTable;
|
|
35
59
|
fields?: AnySQLiteColumn[];
|
|
36
60
|
references?: AnySQLiteColumn[];
|
|
37
|
-
junctionTable?: AnyTable;
|
|
38
|
-
junctionFields?: AnySQLiteColumn[];
|
|
39
|
-
junctionReferences?: AnySQLiteColumn[];
|
|
40
61
|
}
|
|
41
62
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
type Condition = Expression<SqlBool>;
|
|
64
|
+
declare const eq: <T>(column: AnySQLiteColumn, value: T, tableAlias?: string) => Condition;
|
|
65
|
+
declare const ne: <T>(column: AnySQLiteColumn, value: T, tableAlias?: string) => Condition;
|
|
66
|
+
declare const and: (...conditions: Condition[]) => Condition;
|
|
67
|
+
declare const or: (...conditions: Condition[]) => Condition;
|
|
68
|
+
declare const not: (condition: Condition) => Condition;
|
|
69
|
+
declare const gt: <T>(column: AnySQLiteColumn, value: T) => Condition;
|
|
70
|
+
declare const gte: <T>(column: AnySQLiteColumn, value: T) => Condition;
|
|
71
|
+
declare const lt: <T>(column: AnySQLiteColumn, value: T) => Condition;
|
|
72
|
+
declare const lte: <T>(column: AnySQLiteColumn, value: T) => Condition;
|
|
73
|
+
declare const like: (column: AnySQLiteColumn, pattern: string) => Condition;
|
|
74
|
+
declare const ilike: (column: AnySQLiteColumn, pattern: string) => Condition;
|
|
75
|
+
declare const startsWith: (column: AnySQLiteColumn, value: string) => Condition;
|
|
76
|
+
declare const endsWith: (column: AnySQLiteColumn, value: string) => Condition;
|
|
77
|
+
declare const contains: (column: AnySQLiteColumn, value: string) => Condition;
|
|
78
|
+
declare const isNull: (column: AnySQLiteColumn) => Condition;
|
|
79
|
+
declare const isNotNull: (column: AnySQLiteColumn) => Condition;
|
|
80
|
+
declare const exists: (subquery: Expression<any>) => Condition;
|
|
81
|
+
declare const notExists: (subquery: Expression<any>) => Condition;
|
|
82
|
+
declare const eqSubquery: (column: AnySQLiteColumn, subquery: Expression<any>) => Condition;
|
|
83
|
+
declare const neSubquery: (column: AnySQLiteColumn, subquery: Expression<any>) => Condition;
|
|
84
|
+
declare const gtSubquery: (column: AnySQLiteColumn, subquery: Expression<any>) => Condition;
|
|
85
|
+
declare const gteSubquery: (column: AnySQLiteColumn, subquery: Expression<any>) => Condition;
|
|
86
|
+
declare const ltSubquery: (column: AnySQLiteColumn, subquery: Expression<any>) => Condition;
|
|
87
|
+
declare const lteSubquery: (column: AnySQLiteColumn, subquery: Expression<any>) => Condition;
|
|
88
|
+
declare const inArray: <T>(column: AnySQLiteColumn, values: T[] | Expression<any>) => Condition;
|
|
89
|
+
declare const notIn: <T>(column: AnySQLiteColumn, values: T[] | Expression<any>) => Condition;
|
|
90
|
+
declare const between: <T>(column: AnySQLiteColumn, min: T, max: T) => Condition;
|
|
63
91
|
|
|
64
92
|
type NestedInclude = boolean | {
|
|
93
|
+
columns?: string[] | Record<string, boolean>;
|
|
65
94
|
with?: Record<string, NestedInclude>;
|
|
66
95
|
};
|
|
67
96
|
type ExtractRelationNames<T extends AnyTable> = T['relations'] extends Record<string, any> ? keyof T['relations'] & string : never;
|
|
68
97
|
type IncludeRelations<T extends AnyTable> = T['relations'] extends Record<string, any> ? Partial<Record<ExtractRelationNames<T>, NestedInclude>> : Record<string, never>;
|
|
69
|
-
declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns extends (keyof TTable['_']['columns'])[] | undefined = undefined>
|
|
70
|
-
private
|
|
71
|
-
private
|
|
72
|
-
private
|
|
73
|
-
private
|
|
74
|
-
private
|
|
75
|
-
private
|
|
76
|
-
private
|
|
77
|
-
private
|
|
78
|
-
|
|
79
|
-
constructor(db: Database, table: TTable, columns?: TSelectedColumns | undefined);
|
|
98
|
+
declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns extends (keyof TTable['_']['columns'])[] | undefined = undefined> {
|
|
99
|
+
private readonly kysely;
|
|
100
|
+
private _builder;
|
|
101
|
+
private _table;
|
|
102
|
+
private _columns?;
|
|
103
|
+
private _includeRelations;
|
|
104
|
+
private _manualJoins;
|
|
105
|
+
private _isDistinct;
|
|
106
|
+
private _includedColumnAliases;
|
|
107
|
+
constructor(kysely: Kysely<any>, table: TTable, columns?: TSelectedColumns);
|
|
80
108
|
distinct(): this;
|
|
109
|
+
where(condition: Condition): this;
|
|
110
|
+
orderBy(column: AnySQLiteColumn | Expression<any>, direction?: 'asc' | 'desc'): this;
|
|
111
|
+
limit(count: number): this;
|
|
112
|
+
offset(count: number): this;
|
|
81
113
|
groupBy(...columns: AnySQLiteColumn[]): this;
|
|
82
|
-
having(condition:
|
|
83
|
-
leftJoin<T extends AnyTable>(table: T, condition:
|
|
84
|
-
innerJoin<T extends AnyTable>(table: T, condition:
|
|
114
|
+
having(condition: Condition): this;
|
|
115
|
+
leftJoin<T extends AnyTable>(table: T, condition: Expression<SqlBool>, alias: string): this;
|
|
116
|
+
innerJoin<T extends AnyTable>(table: T, condition: Expression<SqlBool>, alias: string): this;
|
|
85
117
|
include(relations: IncludeRelations<TTable>): this;
|
|
86
|
-
private
|
|
118
|
+
private applyIncludes;
|
|
87
119
|
execute(): Promise<InferSelectModel<TTable>[]>;
|
|
88
120
|
private processRelationResults;
|
|
89
121
|
all(): Promise<InferSelectModel<TTable>[]>;
|
|
90
122
|
get(): Promise<InferSelectModel<TTable> | undefined>;
|
|
123
|
+
first(): Promise<InferSelectModel<TTable> | undefined>;
|
|
91
124
|
exists(): Promise<boolean>;
|
|
92
125
|
count(): Promise<number>;
|
|
93
|
-
first(): Promise<InferSelectModel<TTable> | undefined>;
|
|
94
126
|
pluck<K extends keyof TTable['_']['columns']>(column: K): Promise<InferSelectModel<TTable>[K][]>;
|
|
95
127
|
paginate(page?: number, pageSize?: number): Promise<{
|
|
96
128
|
data: InferSelectModel<TTable>[];
|
|
@@ -105,23 +137,27 @@ declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns exten
|
|
|
105
137
|
sql: string;
|
|
106
138
|
params: any[];
|
|
107
139
|
};
|
|
140
|
+
toKyselyExpression(): Expression<any>;
|
|
108
141
|
}
|
|
109
142
|
|
|
110
|
-
declare class UpdateQueryBuilder<T extends AnyTable>
|
|
111
|
-
private
|
|
112
|
-
private
|
|
113
|
-
private
|
|
114
|
-
private
|
|
115
|
-
private
|
|
116
|
-
private
|
|
117
|
-
|
|
143
|
+
declare class UpdateQueryBuilder<T extends AnyTable> {
|
|
144
|
+
private readonly kysely;
|
|
145
|
+
private _builder;
|
|
146
|
+
private _table;
|
|
147
|
+
private _updateData;
|
|
148
|
+
private _returningColumns;
|
|
149
|
+
private _hasWhereClause;
|
|
150
|
+
private _allowGlobal;
|
|
151
|
+
private _incrementDecrementOps;
|
|
152
|
+
constructor(kysely: Kysely<any>, table: T);
|
|
118
153
|
set(data: Partial<InferInsertModel<T>>): this;
|
|
119
|
-
where(condition:
|
|
120
|
-
increment(column: keyof T[
|
|
121
|
-
decrement(column: keyof T[
|
|
154
|
+
where(condition: Condition): this;
|
|
155
|
+
increment(column: keyof T['_']['columns'], value?: number): this;
|
|
156
|
+
decrement(column: keyof T['_']['columns'], value?: number): this;
|
|
122
157
|
allowGlobalOperation(): this;
|
|
123
|
-
returning(...columns: (keyof T[
|
|
124
|
-
private
|
|
158
|
+
returning(...columns: (keyof T['_']['columns'])[]): this;
|
|
159
|
+
private mapReturningRows;
|
|
160
|
+
private buildSetClause;
|
|
125
161
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
126
162
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
127
163
|
returningFirst(): Promise<InferSelectModel<T> | undefined>;
|
|
@@ -131,23 +167,26 @@ declare class UpdateQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
131
167
|
};
|
|
132
168
|
}
|
|
133
169
|
|
|
134
|
-
declare class InsertQueryBuilder<T extends AnyTable>
|
|
135
|
-
private
|
|
136
|
-
private
|
|
137
|
-
private
|
|
138
|
-
private
|
|
139
|
-
private
|
|
140
|
-
private
|
|
141
|
-
|
|
170
|
+
declare class InsertQueryBuilder<T extends AnyTable> {
|
|
171
|
+
private readonly kysely;
|
|
172
|
+
private _builder;
|
|
173
|
+
private _table;
|
|
174
|
+
private _dataSets;
|
|
175
|
+
private _returningColumns;
|
|
176
|
+
private _onConflictAction;
|
|
177
|
+
private _conflictTarget;
|
|
178
|
+
private _updateSet;
|
|
179
|
+
constructor(kysely: Kysely<any>, table: T);
|
|
142
180
|
values(data: InferInsertModel<T> | InferInsertModel<T>[]): this;
|
|
143
|
-
returning(...columns: (keyof T[
|
|
181
|
+
returning(...columns: (keyof T['_']['columns'])[]): this;
|
|
144
182
|
onConflictDoNothing(target?: AnySQLiteColumn | AnySQLiteColumn[]): this;
|
|
145
183
|
onConflictDoUpdate(config: {
|
|
146
184
|
target: AnySQLiteColumn | AnySQLiteColumn[];
|
|
147
185
|
set: Partial<InferInsertModel<T>>;
|
|
148
186
|
}): this;
|
|
149
|
-
private
|
|
150
|
-
private
|
|
187
|
+
private processDefaults;
|
|
188
|
+
private mapReturningRows;
|
|
189
|
+
private serializeDataSet;
|
|
151
190
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
152
191
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
153
192
|
returningFirst(): Promise<InferSelectModel<T> | undefined>;
|
|
@@ -157,13 +196,16 @@ declare class InsertQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
157
196
|
};
|
|
158
197
|
}
|
|
159
198
|
|
|
160
|
-
declare class DeleteQueryBuilder<T extends AnyTable>
|
|
161
|
-
private
|
|
162
|
-
private
|
|
163
|
-
private
|
|
164
|
-
private
|
|
165
|
-
|
|
166
|
-
|
|
199
|
+
declare class DeleteQueryBuilder<T extends AnyTable> {
|
|
200
|
+
private readonly kysely;
|
|
201
|
+
private _builder;
|
|
202
|
+
private _table;
|
|
203
|
+
private _returningColumns;
|
|
204
|
+
private _hasWhereClause;
|
|
205
|
+
private _allowGlobal;
|
|
206
|
+
constructor(kysely: Kysely<any>, table: T);
|
|
207
|
+
private mapReturningRows;
|
|
208
|
+
where(condition: Condition): this;
|
|
167
209
|
allowGlobalOperation(): this;
|
|
168
210
|
returning(...columns: (keyof T['_']['columns'])[]): this;
|
|
169
211
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
@@ -176,18 +218,15 @@ declare class DeleteQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
176
218
|
}
|
|
177
219
|
|
|
178
220
|
declare class WithQueryBuilder {
|
|
179
|
-
private
|
|
180
|
-
private
|
|
181
|
-
constructor(
|
|
182
|
-
with(alias: string, query:
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}): this;
|
|
186
|
-
select<T extends AnyTable, C extends (keyof T["_"]["columns"])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
|
|
221
|
+
private readonly kysely;
|
|
222
|
+
private _ctes;
|
|
223
|
+
constructor(kysely: Kysely<any>);
|
|
224
|
+
with(alias: string, query: SelectQueryBuilder<any, any>): this;
|
|
225
|
+
private applyWith;
|
|
226
|
+
select<T extends AnyTable, C extends (keyof T['_']['columns'])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
|
|
187
227
|
insert<T extends AnyTable>(table: T): InsertQueryBuilder<T>;
|
|
188
228
|
update<T extends AnyTable>(table: T): UpdateQueryBuilder<T>;
|
|
189
229
|
delete<T extends AnyTable>(table: T): DeleteQueryBuilder<T>;
|
|
190
|
-
private applyWithClause;
|
|
191
230
|
}
|
|
192
231
|
|
|
193
232
|
type RelationsBuilder = {
|
|
@@ -196,11 +235,6 @@ type RelationsBuilder = {
|
|
|
196
235
|
references: AnySQLiteColumn[];
|
|
197
236
|
}) => OneRelation<U>;
|
|
198
237
|
many: <U extends AnyTable>(table: U) => ManyRelation<U>;
|
|
199
|
-
manyToMany: <U extends AnyTable>(table: U, config: {
|
|
200
|
-
junctionTable: AnyTable;
|
|
201
|
-
junctionFields: AnySQLiteColumn[];
|
|
202
|
-
junctionReferences: AnySQLiteColumn[];
|
|
203
|
-
}) => ManyToManyRelation<U>;
|
|
204
238
|
};
|
|
205
239
|
|
|
206
240
|
declare class SQLiteColumn<TName extends string = string, TType extends ColumnDataType = ColumnDataType, TMode extends Mode = 'default', TNotNull extends boolean = false, THasDefault extends boolean = false, TAutoincrement extends boolean = false, TEnum extends readonly string[] = never, TCustomType = never> {
|
|
@@ -223,7 +257,10 @@ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDa
|
|
|
223
257
|
primaryKey(): SQLiteColumn<TName, TType, TMode, true, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
224
258
|
autoincrement(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, true, TEnum, TCustomType>;
|
|
225
259
|
unique(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
226
|
-
references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K | T['_']['columns'][K]
|
|
260
|
+
references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K | T['_']['columns'][K], options?: {
|
|
261
|
+
onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
|
|
262
|
+
onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
|
|
263
|
+
}): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
227
264
|
$onUpdateFn(fn: () => ColumnValueTypes<TType, TMode>): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
228
265
|
$type<T>(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, T>;
|
|
229
266
|
as(alias: string): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
@@ -254,37 +291,16 @@ declare class Table<TColumns extends Record<string, AnySQLiteColumn>, TTableName
|
|
|
254
291
|
constructor(name: TTableName, columns: TColumns);
|
|
255
292
|
}
|
|
256
293
|
declare const sqliteTable: <TTableName extends string, TColumns extends Record<string, AnySQLiteColumn>>(tableName: TTableName, columns: TColumns) => Table<TColumns, TTableName>;
|
|
257
|
-
type SQLCondition =
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
sql: string;
|
|
263
|
-
params: any[];
|
|
264
|
-
_type?: T;
|
|
265
|
-
};
|
|
266
|
-
type SQLSubquery = {
|
|
267
|
-
sql: string;
|
|
268
|
-
params: any[];
|
|
269
|
-
_isSubquery: true;
|
|
270
|
-
};
|
|
271
|
-
declare const asc: (column: AnySQLiteColumn) => {
|
|
272
|
-
sql: string;
|
|
273
|
-
params: never[];
|
|
274
|
-
};
|
|
275
|
-
declare const desc: (column: AnySQLiteColumn) => {
|
|
276
|
-
sql: string;
|
|
277
|
-
params: never[];
|
|
278
|
-
};
|
|
279
|
-
declare const sql: <T = unknown>(strings: TemplateStringsArray, ...values: any[]) => {
|
|
280
|
-
sql: string;
|
|
281
|
-
params: any[];
|
|
282
|
-
mapWith?: (value: any) => T;
|
|
283
|
-
};
|
|
294
|
+
type SQLCondition = Expression<SqlBool>;
|
|
295
|
+
type SQLAggregate<T = number> = Expression<T>;
|
|
296
|
+
type SQLSubquery = Expression<any>;
|
|
297
|
+
declare const asc: (column: AnySQLiteColumn) => Expression<any>;
|
|
298
|
+
declare const desc: (column: AnySQLiteColumn) => Expression<any>;
|
|
284
299
|
declare class TauriORM {
|
|
285
300
|
private db;
|
|
286
301
|
private tables;
|
|
287
|
-
|
|
302
|
+
private kysely;
|
|
303
|
+
constructor(db: DatabaseLike, schema?: Record<string, AnyTable | Record<string, Relation>> | undefined);
|
|
288
304
|
private buildColumnDefinition;
|
|
289
305
|
checkMigration(): Promise<{
|
|
290
306
|
safe: boolean;
|
|
@@ -314,10 +330,7 @@ declare class TauriORM {
|
|
|
314
330
|
rowsAffected: number;
|
|
315
331
|
}[] : never>;
|
|
316
332
|
$with(alias: string): {
|
|
317
|
-
as: (query:
|
|
318
|
-
sql: string;
|
|
319
|
-
params: any[];
|
|
320
|
-
}) => WithQueryBuilder;
|
|
333
|
+
as: (query: SelectQueryBuilder<any, any>) => WithQueryBuilder;
|
|
321
334
|
};
|
|
322
335
|
transaction<T>(callback: (tx: TauriORM) => Promise<T>): Promise<T>;
|
|
323
336
|
rollback(): never;
|
|
@@ -357,69 +370,23 @@ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
|
357
370
|
declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
358
371
|
constructor(foreignTable: T);
|
|
359
372
|
}
|
|
360
|
-
declare class ManyToManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
361
|
-
config: {
|
|
362
|
-
junctionTable: AnyTable;
|
|
363
|
-
junctionFields: AnySQLiteColumn[];
|
|
364
|
-
junctionReferences: AnySQLiteColumn[];
|
|
365
|
-
};
|
|
366
|
-
constructor(foreignTable: T, config: {
|
|
367
|
-
junctionTable: AnyTable;
|
|
368
|
-
junctionFields: AnySQLiteColumn[];
|
|
369
|
-
junctionReferences: AnySQLiteColumn[];
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
373
|
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
|
|
373
374
|
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
374
375
|
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
375
376
|
|
|
376
|
-
declare const
|
|
377
|
-
declare const
|
|
378
|
-
declare const
|
|
379
|
-
declare const
|
|
380
|
-
declare const
|
|
381
|
-
declare const
|
|
382
|
-
declare const
|
|
383
|
-
declare const
|
|
384
|
-
declare const lte: <T>(column: AnySQLiteColumn, value: T) => SQLCondition;
|
|
385
|
-
declare const like: (column: AnySQLiteColumn, pattern: string) => SQLCondition;
|
|
386
|
-
declare const ilike: (column: AnySQLiteColumn, pattern: string) => SQLCondition;
|
|
387
|
-
declare const startsWith: (column: AnySQLiteColumn, value: string) => SQLCondition;
|
|
388
|
-
declare const endsWith: (column: AnySQLiteColumn, value: string) => SQLCondition;
|
|
389
|
-
declare const contains: (column: AnySQLiteColumn, value: string) => SQLCondition;
|
|
390
|
-
declare const isNull: (column: AnySQLiteColumn) => SQLCondition;
|
|
391
|
-
declare const isNotNull: (column: AnySQLiteColumn) => SQLCondition;
|
|
392
|
-
declare const exists: (subquery: {
|
|
393
|
-
sql: string;
|
|
394
|
-
params: any[];
|
|
395
|
-
}) => SQLCondition;
|
|
396
|
-
declare const notExists: (subquery: {
|
|
397
|
-
sql: string;
|
|
398
|
-
params: any[];
|
|
399
|
-
}) => SQLCondition;
|
|
400
|
-
declare const eqSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
401
|
-
declare const neSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
402
|
-
declare const gtSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
403
|
-
declare const gteSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
404
|
-
declare const ltSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
405
|
-
declare const lteSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
406
|
-
declare const inArray: <T>(column: AnySQLiteColumn, values: T[] | SQLSubquery) => SQLCondition;
|
|
407
|
-
declare const notIn: <T>(column: AnySQLiteColumn, values: T[] | SQLSubquery) => SQLCondition;
|
|
408
|
-
declare const between: <T>(column: AnySQLiteColumn, min: T, max: T) => SQLCondition;
|
|
409
|
-
|
|
410
|
-
declare const count: (column?: AnySQLiteColumn) => SQLAggregate<number>;
|
|
411
|
-
declare const countDistinct: (column: AnySQLiteColumn) => SQLAggregate<number>;
|
|
412
|
-
declare const sum: (column: AnySQLiteColumn) => SQLAggregate<number>;
|
|
413
|
-
declare const avg: (column: AnySQLiteColumn) => SQLAggregate<number>;
|
|
414
|
-
declare const max: <T = any>(column: AnySQLiteColumn) => SQLAggregate<T>;
|
|
415
|
-
declare const min: <T = any>(column: AnySQLiteColumn) => SQLAggregate<T>;
|
|
416
|
-
declare const groupConcat: (column: AnySQLiteColumn, separator?: string) => SQLAggregate<string>;
|
|
417
|
-
declare const as: <T>(aggregate: SQLAggregate<T>, alias: string) => SQLAggregate<T> & {
|
|
377
|
+
declare const count: (column?: AnySQLiteColumn) => Expression<number>;
|
|
378
|
+
declare const countDistinct: (column: AnySQLiteColumn) => Expression<number>;
|
|
379
|
+
declare const sum: (column: AnySQLiteColumn) => Expression<number>;
|
|
380
|
+
declare const avg: (column: AnySQLiteColumn) => Expression<number>;
|
|
381
|
+
declare const max: <T = any>(column: AnySQLiteColumn) => Expression<T>;
|
|
382
|
+
declare const min: <T = any>(column: AnySQLiteColumn) => Expression<T>;
|
|
383
|
+
declare const groupConcat: (column: AnySQLiteColumn, separator?: string) => Expression<string>;
|
|
384
|
+
declare const as: <T>(aggregate: Expression<T>, alias: string) => Expression<T> & {
|
|
418
385
|
alias: string;
|
|
419
386
|
};
|
|
420
387
|
|
|
421
|
-
declare const subquery: <T extends AnyTable>(query: SelectQueryBuilder<T, any>) =>
|
|
422
|
-
declare const scalarSubquery: <T extends AnyTable>(query: SelectQueryBuilder<T, any>) =>
|
|
388
|
+
declare const subquery: <T extends AnyTable>(query: SelectQueryBuilder<T, any>) => Expression<any>;
|
|
389
|
+
declare const scalarSubquery: <T extends AnyTable>(query: SelectQueryBuilder<T, any>) => Expression<any>;
|
|
423
390
|
|
|
424
391
|
declare function text<TName extends string>(name: TName): SQLiteColumn<TName, 'TEXT', 'default', false, false, false, never, never>;
|
|
425
392
|
declare function text<TName extends string, const TConfig extends {
|
|
@@ -442,6 +409,81 @@ declare function numeric<TName extends string, const TConfig extends {
|
|
|
442
409
|
}>(name: TName, config: TConfig): SQLiteColumn<TName, 'NUMERIC', TConfig['mode'], false, false, false, never, never>;
|
|
443
410
|
declare const enumType: <TName extends string, TValues extends readonly [string, ...string[]]>(name: TName, values: TValues) => SQLiteColumn<TName, "TEXT", "default", false, false, false, TValues extends readonly string[] ? TValues : never, never>;
|
|
444
411
|
|
|
412
|
+
/** Maps relations() return type to typed relation configs with foreign table preserved */
|
|
413
|
+
type InferRelationsMap<R extends Record<string, OneRelation | ManyRelation>> = {
|
|
414
|
+
[K in keyof R]: R[K] extends OneRelation<infer T> ? {
|
|
415
|
+
type: 'one';
|
|
416
|
+
foreignTable: T;
|
|
417
|
+
} : R[K] extends ManyRelation<infer T> ? {
|
|
418
|
+
type: 'many';
|
|
419
|
+
foreignTable: T;
|
|
420
|
+
} : never;
|
|
421
|
+
};
|
|
422
|
+
/** With/include object shape - matches NestedInclude from select builder */
|
|
423
|
+
type WithShape = boolean | {
|
|
424
|
+
columns?: string[] | Record<string, boolean>;
|
|
425
|
+
with?: Record<string, WithShape>;
|
|
426
|
+
};
|
|
427
|
+
/** Map of table name -> relations() return type, for nested with support */
|
|
428
|
+
type RelationsByTable = Record<string, Record<string, OneRelation | ManyRelation>>;
|
|
429
|
+
/** Get relations for a table from the all-relations map */
|
|
430
|
+
type GetRelationsForTable<TTable extends AnyTable, TAllRelations extends RelationsByTable> = TAllRelations[TTable['_']['name']] extends Record<string, OneRelation | ManyRelation> ? InferRelationsMap<TAllRelations[TTable['_']['name']]> : Record<string, never>;
|
|
431
|
+
/** Infer nested relation fields when TWith has a nested `with` */
|
|
432
|
+
type InferNestedFields<TForeignTable extends AnyTable, TWith extends WithShape, TAllRelations extends RelationsByTable> = TWith extends {
|
|
433
|
+
with?: infer TW;
|
|
434
|
+
} ? TW extends Record<string, WithShape> ? InferRelationFields<TForeignTable, GetRelationsForTable<TForeignTable, TAllRelations>, TAllRelations, TW> : unknown : unknown;
|
|
435
|
+
/** Recursively build relation fields from a with object */
|
|
436
|
+
type InferRelationFields<TTable extends AnyTable, TRelationsMap extends Record<string, {
|
|
437
|
+
type: 'one' | 'many';
|
|
438
|
+
foreignTable: AnyTable;
|
|
439
|
+
}>, TAllRelations extends RelationsByTable, TWith extends Record<string, WithShape>> = {
|
|
440
|
+
[K in keyof TWith & keyof TRelationsMap]: TWith[K] extends false | undefined ? never : TRelationsMap[K] extends {
|
|
441
|
+
type: 'one';
|
|
442
|
+
foreignTable: infer T;
|
|
443
|
+
} ? T extends AnyTable ? InferSelectModel<T> & InferNestedFields<T, TWith[K], TAllRelations> : never : TRelationsMap[K] extends {
|
|
444
|
+
type: 'many';
|
|
445
|
+
foreignTable: infer T;
|
|
446
|
+
} ? T extends AnyTable ? (InferSelectModel<T> & InferNestedFields<T, TWith[K], TAllRelations>)[] : never : never;
|
|
447
|
+
};
|
|
448
|
+
/**
|
|
449
|
+
* Infer the result type of a select query that includes relations via `.include(with)`.
|
|
450
|
+
*
|
|
451
|
+
* Use this to type variables that hold results from relational queries, e.g.:
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* ```ts
|
|
455
|
+
* export type User = InferSelectModel<typeof schema.user>
|
|
456
|
+
*
|
|
457
|
+
* const withRelationalObject = { sessions: true, accounts: true } as const
|
|
458
|
+
* export type UserWithRelations = InferRelationalSelectModel<
|
|
459
|
+
* typeof schema.user,
|
|
460
|
+
* typeof schema.userRelations,
|
|
461
|
+
* typeof withRelationalObject
|
|
462
|
+
* >
|
|
463
|
+
* ```
|
|
464
|
+
*
|
|
465
|
+
* For nested includes, pass a map of table name -> relations as the fourth parameter:
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* ```ts
|
|
469
|
+
* const withNested = { sessions: { with: { user: true } } } as const
|
|
470
|
+
* type UserWithSessionsAndUser = InferRelationalSelectModel<
|
|
471
|
+
* typeof schema.user,
|
|
472
|
+
* typeof schema.userRelations,
|
|
473
|
+
* typeof withNested,
|
|
474
|
+
* { user: typeof userRelations; session: typeof sessionRelations }
|
|
475
|
+
* >
|
|
476
|
+
* ```
|
|
477
|
+
*
|
|
478
|
+
* @param TTable - The table type (e.g. typeof schema.user)
|
|
479
|
+
* @param TRelations - The relations for this table (e.g. typeof schema.userRelations)
|
|
480
|
+
* @param TWith - The with/include object shape (use `as const` for literal inference)
|
|
481
|
+
* @param TAllRelations - Optional map of table name -> relations for nested `with` support
|
|
482
|
+
*/
|
|
483
|
+
type InferRelationalSelectModel<TTable extends AnyTable, TRelations extends Record<string, OneRelation | ManyRelation>, TWith extends Record<string, WithShape>, TAllRelations extends RelationsByTable = {
|
|
484
|
+
[K in TTable['_']['name']]: TRelations;
|
|
485
|
+
}> = InferSelectModel<TTable> & InferRelationFields<TTable, InferRelationsMap<TRelations>, TAllRelations, TWith>;
|
|
486
|
+
|
|
445
487
|
declare class TauriORMError extends Error {
|
|
446
488
|
constructor(message: string);
|
|
447
489
|
}
|
|
@@ -473,4 +515,4 @@ declare class TableNotFoundError extends TauriORMError {
|
|
|
473
515
|
constructor(tableName: string);
|
|
474
516
|
}
|
|
475
517
|
|
|
476
|
-
export { type AnySQLiteColumn, type AnyTable,
|
|
518
|
+
export { type AnySQLiteColumn, type AnyTable, type ColumnDataType, ColumnNotFoundError, type ColumnOptions, type ColumnValueTypes, type Condition, type DatabaseLike, DeleteQueryBuilder, type ExtractColumnType, type InferInsertModel, type InferRelationalSelectModel, type InferRelationsMap, type InferSelectModel, InsertQueryBuilder, InsertValidationError, ManyRelation, MigrationError, MissingWhereClauseError, type Mode, OneRelation, QueryBuilderError, Relation, type RelationConfig, RelationError, type RelationType, type RelationsBuilder, type SQLAggregate, type SQLCondition, type SQLSubquery, SQLiteColumn, SelectQueryBuilder, Table, TableNotFoundError, TauriDialect, TauriORM, TauriORMError, UpdateQueryBuilder, UpdateValidationError, ValidationError, WithQueryBuilder, alias, and, as, asc, avg, between, blob, boolean, contains, count, countDistinct, desc, endsWith, enumType, eq, eqSubquery, exists, getTableColumns, groupConcat, gt, gtSubquery, gte, gteSubquery, ilike, inArray, integer, isNotNull, isNull, like, lt, ltSubquery, lte, lteSubquery, max, min, ne, neSubquery, not, notExists, notIn, numeric, or, real, relations, scalarSubquery, sqliteTable, startsWith, subquery, sum, text };
|