@type32/tauri-sqlite-orm 0.1.20 → 0.2.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.
- package/README.md +76 -0
- package/dist/index.d.mts +136 -11
- package/dist/index.d.ts +136 -11
- package/dist/index.js +580 -91
- package/dist/index.mjs +551 -92
- package/package.json +38 -36
package/README.md
CHANGED
|
@@ -6,6 +6,15 @@ A Drizzle-like TypeScript ORM tailored for Tauri v2's `@tauri-apps/plugin-sql` (
|
|
|
6
6
|
|
|
7
7
|
- **Drizzle-like Schema:** Define your database schema using a familiar, chainable API.
|
|
8
8
|
- **Type-Safe Query Builder:** Build SQL queries with TypeScript, ensuring type safety and autocompletion.
|
|
9
|
+
- **Relations Support:** Define and query one-to-one, one-to-many, and many-to-many relationships between tables.
|
|
10
|
+
- **Nested Includes:** Load relations of relations with intuitive nested syntax.
|
|
11
|
+
- **Advanced Operators:** Comprehensive set of operators including `ne`, `between`, `notIn`, `ilike`, `startsWith`, `endsWith`, `contains`, and more.
|
|
12
|
+
- **Subquery Support:** Use subqueries in WHERE and SELECT clauses with full type safety.
|
|
13
|
+
- **Aggregate Functions:** Type-safe aggregates like `count`, `sum`, `avg`, `min`, `max`, and SQLite's `groupConcat`.
|
|
14
|
+
- **Query Debugging:** Use `.toSQL()` on any query to inspect generated SQL and parameters.
|
|
15
|
+
- **Safety Features:** Automatic WHERE clause validation for UPDATE/DELETE prevents accidental data loss.
|
|
16
|
+
- **Increment/Decrement:** Atomic increment/decrement operations for safe counter updates.
|
|
17
|
+
- **Better Error Handling:** Custom error classes for clear, actionable error messages.
|
|
9
18
|
- **Simplified Migrations:** Keep your database schema in sync with your application's models using automatic schema detection and migration tools.
|
|
10
19
|
- **Lightweight & Performant:** Designed to be a thin layer over the Tauri SQL plugin, ensuring minimal overhead.
|
|
11
20
|
|
|
@@ -16,3 +25,70 @@ bun add @type32/tauri-sqlite-orm @tauri-apps/plugin-sql
|
|
|
16
25
|
```
|
|
17
26
|
|
|
18
27
|
Make sure the SQL plugin is registered on the Rust side (see Tauri docs).
|
|
28
|
+
|
|
29
|
+
### Quick Example
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import Database from '@tauri-apps/plugin-sql'
|
|
33
|
+
import { TauriORM, sqliteTable, int, text, relations } from '@type32/tauri-sqlite-orm'
|
|
34
|
+
|
|
35
|
+
// Define tables
|
|
36
|
+
const users = sqliteTable('users', {
|
|
37
|
+
id: int('id').primaryKey().autoincrement(),
|
|
38
|
+
name: text('name').notNull(),
|
|
39
|
+
email: text('email').notNull().unique(),
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const posts = sqliteTable('posts', {
|
|
43
|
+
id: int('id').primaryKey().autoincrement(),
|
|
44
|
+
title: text('title').notNull(),
|
|
45
|
+
content: text('content').notNull(),
|
|
46
|
+
userId: int('user_id').notNull().references(users, 'id'),
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
// Define relations
|
|
50
|
+
const usersRelations = relations(users, ({ many }) => ({
|
|
51
|
+
posts: many(posts),
|
|
52
|
+
}))
|
|
53
|
+
|
|
54
|
+
const postsRelations = relations(posts, ({ one }) => ({
|
|
55
|
+
user: one(users, {
|
|
56
|
+
fields: [posts._.columns.userId],
|
|
57
|
+
references: [users._.columns.id],
|
|
58
|
+
}),
|
|
59
|
+
}))
|
|
60
|
+
|
|
61
|
+
// Initialize ORM
|
|
62
|
+
const db = await Database.load('sqlite:mydb.db')
|
|
63
|
+
const orm = new TauriORM(db, {
|
|
64
|
+
users,
|
|
65
|
+
usersRelations,
|
|
66
|
+
posts,
|
|
67
|
+
postsRelations,
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
// Run migrations
|
|
71
|
+
await orm.migrate()
|
|
72
|
+
|
|
73
|
+
// Query with relations
|
|
74
|
+
const usersWithPosts = await orm
|
|
75
|
+
.select(users)
|
|
76
|
+
.include({ posts: true })
|
|
77
|
+
.all()
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Documentation
|
|
81
|
+
|
|
82
|
+
- [Many-to-Many Relations Guide](./docs/many-to-many-example.md) - Learn how to implement many-to-many relationships with junction tables
|
|
83
|
+
- [Advanced Queries Guide](./docs/advanced-queries-example.md) - Learn about `.toSQL()`, aggregates, and subqueries
|
|
84
|
+
- [Error Handling and Safety](./docs/error-handling-and-safety.md) - Learn about WHERE validation, increment/decrement, and error handling
|
|
85
|
+
|
|
86
|
+
### Relations
|
|
87
|
+
|
|
88
|
+
The ORM supports three types of relations:
|
|
89
|
+
|
|
90
|
+
1. **One-to-One / Many-to-One**: Use `one()` to define a relation where the current table references another table
|
|
91
|
+
2. **One-to-Many**: Use `many()` to define a relation where another table references the current table
|
|
92
|
+
3. **Many-to-Many**: Use `manyToMany()` to define a many-to-many relation through a junction table
|
|
93
|
+
|
|
94
|
+
See the [many-to-many example](./docs/many-to-many-example.md) for detailed usage.
|
package/dist/index.d.mts
CHANGED
|
@@ -24,12 +24,15 @@ type AnyTable = Table<Record<string, AnySQLiteColumn>, string>;
|
|
|
24
24
|
type InferSelectModel<T extends AnyTable> = {
|
|
25
25
|
[K in keyof T['_']['columns']]: ExtractColumnType<T['_']['columns'][K]>;
|
|
26
26
|
};
|
|
27
|
-
type RelationType = 'one' | 'many';
|
|
27
|
+
type RelationType = 'one' | 'many' | 'manyToMany';
|
|
28
28
|
interface RelationConfig {
|
|
29
29
|
type: RelationType;
|
|
30
30
|
foreignTable: AnyTable;
|
|
31
31
|
fields?: AnySQLiteColumn[];
|
|
32
32
|
references?: AnySQLiteColumn[];
|
|
33
|
+
junctionTable?: AnyTable;
|
|
34
|
+
junctionFields?: AnySQLiteColumn[];
|
|
35
|
+
junctionReferences?: AnySQLiteColumn[];
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
declare class BaseQueryBuilder {
|
|
@@ -48,8 +51,16 @@ declare class BaseQueryBuilder {
|
|
|
48
51
|
sql: string;
|
|
49
52
|
params: any[];
|
|
50
53
|
};
|
|
54
|
+
toSQL(): {
|
|
55
|
+
sql: string;
|
|
56
|
+
params: any[];
|
|
57
|
+
};
|
|
51
58
|
}
|
|
52
59
|
|
|
60
|
+
type NestedInclude = boolean | {
|
|
61
|
+
with?: Record<string, NestedInclude>;
|
|
62
|
+
};
|
|
63
|
+
type IncludeRelations<T extends AnyTable> = Partial<Record<keyof T['relations'], NestedInclude>>;
|
|
53
64
|
declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns extends (keyof TTable['_']['columns'])[] | undefined = undefined> extends BaseQueryBuilder {
|
|
54
65
|
private table;
|
|
55
66
|
private columns?;
|
|
@@ -66,24 +77,39 @@ declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns exten
|
|
|
66
77
|
having(condition: SQLCondition): this;
|
|
67
78
|
leftJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias: string): this;
|
|
68
79
|
innerJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias: string): this;
|
|
69
|
-
include(relations:
|
|
80
|
+
include(relations: IncludeRelations<TTable>): this;
|
|
70
81
|
private buildJoins;
|
|
71
82
|
execute(): Promise<any[]>;
|
|
72
83
|
private processRelationResults;
|
|
73
84
|
all(): Promise<any[]>;
|
|
74
85
|
get(): Promise<any | undefined>;
|
|
86
|
+
toSQL(): {
|
|
87
|
+
sql: string;
|
|
88
|
+
params: any[];
|
|
89
|
+
};
|
|
75
90
|
}
|
|
76
91
|
|
|
77
92
|
declare class UpdateQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
78
93
|
private table;
|
|
79
94
|
private updateData;
|
|
80
95
|
private returningColumns;
|
|
96
|
+
private hasWhereClause;
|
|
97
|
+
private allowGlobal;
|
|
98
|
+
private incrementDecrementOps;
|
|
81
99
|
constructor(db: Database, table: T);
|
|
82
100
|
set(data: Partial<InferInsertModel<T>>): this;
|
|
101
|
+
where(condition: any): this;
|
|
102
|
+
increment(column: keyof T["_"]["columns"], value?: number): this;
|
|
103
|
+
decrement(column: keyof T["_"]["columns"], value?: number): this;
|
|
104
|
+
allowGlobalOperation(): this;
|
|
83
105
|
returning(...columns: (keyof T["_"]["columns"])[]): this;
|
|
84
106
|
private buildUpdateClause;
|
|
85
107
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
86
108
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
109
|
+
toSQL(): {
|
|
110
|
+
sql: string;
|
|
111
|
+
params: any[];
|
|
112
|
+
};
|
|
87
113
|
}
|
|
88
114
|
|
|
89
115
|
declare class InsertQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
@@ -105,15 +131,27 @@ declare class InsertQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
105
131
|
private buildConflictClause;
|
|
106
132
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
107
133
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
134
|
+
toSQL(): {
|
|
135
|
+
sql: string;
|
|
136
|
+
params: any[];
|
|
137
|
+
};
|
|
108
138
|
}
|
|
109
139
|
|
|
110
140
|
declare class DeleteQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
111
141
|
private table;
|
|
112
142
|
private returningColumns;
|
|
143
|
+
private hasWhereClause;
|
|
144
|
+
private allowGlobal;
|
|
113
145
|
constructor(db: Database, table: T);
|
|
146
|
+
where(condition: any): this;
|
|
147
|
+
allowGlobalOperation(): this;
|
|
114
148
|
returning(...columns: (keyof T['_']['columns'])[]): this;
|
|
115
149
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
116
150
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
151
|
+
toSQL(): {
|
|
152
|
+
sql: string;
|
|
153
|
+
params: any[];
|
|
154
|
+
};
|
|
117
155
|
}
|
|
118
156
|
|
|
119
157
|
declare class WithQueryBuilder {
|
|
@@ -137,6 +175,11 @@ type RelationsBuilder = {
|
|
|
137
175
|
references: AnySQLiteColumn[];
|
|
138
176
|
}) => OneRelation<U>;
|
|
139
177
|
many: <U extends AnyTable>(table: U) => ManyRelation<U>;
|
|
178
|
+
manyToMany: <U extends AnyTable>(table: U, config: {
|
|
179
|
+
junctionTable: AnyTable;
|
|
180
|
+
junctionFields: AnySQLiteColumn[];
|
|
181
|
+
junctionReferences: AnySQLiteColumn[];
|
|
182
|
+
}) => ManyToManyRelation<U>;
|
|
140
183
|
};
|
|
141
184
|
|
|
142
185
|
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> {
|
|
@@ -159,7 +202,7 @@ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDa
|
|
|
159
202
|
primaryKey(): SQLiteColumn<TName, TType, TMode, true, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
160
203
|
autoincrement(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, true, TEnum, TCustomType>;
|
|
161
204
|
unique(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
162
|
-
references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
205
|
+
references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K | T['_']['columns'][K]): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
163
206
|
$onUpdateFn(fn: () => ColumnValueTypes<TType, TMode>): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
164
207
|
$type<T>(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, T>;
|
|
165
208
|
as(alias: string): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
@@ -189,6 +232,16 @@ type SQLCondition = {
|
|
|
189
232
|
sql: string;
|
|
190
233
|
params: any[];
|
|
191
234
|
};
|
|
235
|
+
type SQLAggregate<T = number> = {
|
|
236
|
+
sql: string;
|
|
237
|
+
params: any[];
|
|
238
|
+
_type?: T;
|
|
239
|
+
};
|
|
240
|
+
type SQLSubquery = {
|
|
241
|
+
sql: string;
|
|
242
|
+
params: any[];
|
|
243
|
+
_isSubquery: true;
|
|
244
|
+
};
|
|
192
245
|
declare const asc: (column: AnySQLiteColumn) => {
|
|
193
246
|
sql: string;
|
|
194
247
|
params: never[];
|
|
@@ -258,11 +311,24 @@ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
|
258
311
|
declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
259
312
|
constructor(foreignTable: T);
|
|
260
313
|
}
|
|
314
|
+
declare class ManyToManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
315
|
+
config: {
|
|
316
|
+
junctionTable: AnyTable;
|
|
317
|
+
junctionFields: AnySQLiteColumn[];
|
|
318
|
+
junctionReferences: AnySQLiteColumn[];
|
|
319
|
+
};
|
|
320
|
+
constructor(foreignTable: T, config: {
|
|
321
|
+
junctionTable: AnyTable;
|
|
322
|
+
junctionFields: AnySQLiteColumn[];
|
|
323
|
+
junctionReferences: AnySQLiteColumn[];
|
|
324
|
+
});
|
|
325
|
+
}
|
|
261
326
|
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
|
|
262
327
|
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
263
328
|
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
264
329
|
|
|
265
330
|
declare const eq: <T>(column: AnySQLiteColumn, value: T, tableAlias?: string) => SQLCondition;
|
|
331
|
+
declare const ne: <T>(column: AnySQLiteColumn, value: T, tableAlias?: string) => SQLCondition;
|
|
266
332
|
declare const and: (...conditions: SQLCondition[]) => SQLCondition;
|
|
267
333
|
declare const or: (...conditions: SQLCondition[]) => SQLCondition;
|
|
268
334
|
declare const not: (condition: SQLCondition) => SQLCondition;
|
|
@@ -271,15 +337,43 @@ declare const gte: <T>(column: AnySQLiteColumn, value: T) => SQLCondition;
|
|
|
271
337
|
declare const lt: <T>(column: AnySQLiteColumn, value: T) => SQLCondition;
|
|
272
338
|
declare const lte: <T>(column: AnySQLiteColumn, value: T) => SQLCondition;
|
|
273
339
|
declare const like: (column: AnySQLiteColumn, pattern: string) => SQLCondition;
|
|
340
|
+
declare const ilike: (column: AnySQLiteColumn, pattern: string) => SQLCondition;
|
|
341
|
+
declare const startsWith: (column: AnySQLiteColumn, value: string) => SQLCondition;
|
|
342
|
+
declare const endsWith: (column: AnySQLiteColumn, value: string) => SQLCondition;
|
|
343
|
+
declare const contains: (column: AnySQLiteColumn, value: string) => SQLCondition;
|
|
274
344
|
declare const isNull: (column: AnySQLiteColumn) => SQLCondition;
|
|
275
345
|
declare const isNotNull: (column: AnySQLiteColumn) => SQLCondition;
|
|
276
|
-
declare const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
declare const
|
|
281
|
-
|
|
282
|
-
|
|
346
|
+
declare const exists: (subquery: {
|
|
347
|
+
sql: string;
|
|
348
|
+
params: any[];
|
|
349
|
+
}) => SQLCondition;
|
|
350
|
+
declare const notExists: (subquery: {
|
|
351
|
+
sql: string;
|
|
352
|
+
params: any[];
|
|
353
|
+
}) => SQLCondition;
|
|
354
|
+
declare const eqSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
355
|
+
declare const neSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
356
|
+
declare const gtSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
357
|
+
declare const gteSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
358
|
+
declare const ltSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
359
|
+
declare const lteSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
360
|
+
declare const inArray: <T>(column: AnySQLiteColumn, values: T[] | SQLSubquery) => SQLCondition;
|
|
361
|
+
declare const notIn: <T>(column: AnySQLiteColumn, values: T[] | SQLSubquery) => SQLCondition;
|
|
362
|
+
declare const between: <T>(column: AnySQLiteColumn, min: T, max: T) => SQLCondition;
|
|
363
|
+
|
|
364
|
+
declare const count: (column?: AnySQLiteColumn) => SQLAggregate<number>;
|
|
365
|
+
declare const countDistinct: (column: AnySQLiteColumn) => SQLAggregate<number>;
|
|
366
|
+
declare const sum: (column: AnySQLiteColumn) => SQLAggregate<number>;
|
|
367
|
+
declare const avg: (column: AnySQLiteColumn) => SQLAggregate<number>;
|
|
368
|
+
declare const max: <T = any>(column: AnySQLiteColumn) => SQLAggregate<T>;
|
|
369
|
+
declare const min: <T = any>(column: AnySQLiteColumn) => SQLAggregate<T>;
|
|
370
|
+
declare const groupConcat: (column: AnySQLiteColumn, separator?: string) => SQLAggregate<string>;
|
|
371
|
+
declare const as: <T>(aggregate: SQLAggregate<T>, alias: string) => SQLAggregate<T> & {
|
|
372
|
+
alias: string;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
declare const subquery: <T extends AnyTable>(query: SelectQueryBuilder<T, any>) => SQLSubquery;
|
|
376
|
+
declare const scalarSubquery: <T extends AnyTable>(query: SelectQueryBuilder<T, any>) => SQLSubquery;
|
|
283
377
|
|
|
284
378
|
declare const text: <TName extends string, TEnum extends readonly string[]>(name: TName, config?: {
|
|
285
379
|
mode?: "json";
|
|
@@ -298,4 +392,35 @@ declare const numeric: <TName extends string>(name: TName, config?: {
|
|
|
298
392
|
}) => SQLiteColumn<TName, "NUMERIC", "bigint", false, false, false, never, never>;
|
|
299
393
|
declare const enumType: <TName extends string, TValues extends readonly [string, ...string[]]>(name: TName, values: TValues) => SQLiteColumn<TName, "TEXT", "json", false, false, false, TValues, never>;
|
|
300
394
|
|
|
301
|
-
|
|
395
|
+
declare class TauriORMError extends Error {
|
|
396
|
+
constructor(message: string);
|
|
397
|
+
}
|
|
398
|
+
declare class QueryBuilderError extends TauriORMError {
|
|
399
|
+
constructor(message: string);
|
|
400
|
+
}
|
|
401
|
+
declare class MissingWhereClauseError extends QueryBuilderError {
|
|
402
|
+
constructor(operation: 'UPDATE' | 'DELETE', tableName: string);
|
|
403
|
+
}
|
|
404
|
+
declare class ValidationError extends TauriORMError {
|
|
405
|
+
constructor(message: string);
|
|
406
|
+
}
|
|
407
|
+
declare class InsertValidationError extends ValidationError {
|
|
408
|
+
constructor(message: string);
|
|
409
|
+
}
|
|
410
|
+
declare class UpdateValidationError extends ValidationError {
|
|
411
|
+
constructor(message: string);
|
|
412
|
+
}
|
|
413
|
+
declare class MigrationError extends TauriORMError {
|
|
414
|
+
constructor(message: string);
|
|
415
|
+
}
|
|
416
|
+
declare class RelationError extends TauriORMError {
|
|
417
|
+
constructor(message: string);
|
|
418
|
+
}
|
|
419
|
+
declare class ColumnNotFoundError extends TauriORMError {
|
|
420
|
+
constructor(columnName: string, tableName: string);
|
|
421
|
+
}
|
|
422
|
+
declare class TableNotFoundError extends TauriORMError {
|
|
423
|
+
constructor(tableName: string);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export { type AnySQLiteColumn, type AnyTable, BaseQueryBuilder, type ColumnDataType, ColumnNotFoundError, type ColumnOptions, type ColumnValueTypes, DeleteQueryBuilder, type ExtractColumnType, type InferInsertModel, type InferSelectModel, InsertQueryBuilder, InsertValidationError, ManyRelation, ManyToManyRelation, MigrationError, MissingWhereClauseError, type Mode, OneRelation, QueryBuilderError, Relation, type RelationConfig, RelationError, type RelationType, type RelationsBuilder, type SQLAggregate, type SQLCondition, type SQLSubquery, SQLiteColumn, SelectQueryBuilder, Table, TableNotFoundError, 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, sql, sqliteTable, startsWith, subquery, sum, text };
|
package/dist/index.d.ts
CHANGED
|
@@ -24,12 +24,15 @@ type AnyTable = Table<Record<string, AnySQLiteColumn>, string>;
|
|
|
24
24
|
type InferSelectModel<T extends AnyTable> = {
|
|
25
25
|
[K in keyof T['_']['columns']]: ExtractColumnType<T['_']['columns'][K]>;
|
|
26
26
|
};
|
|
27
|
-
type RelationType = 'one' | 'many';
|
|
27
|
+
type RelationType = 'one' | 'many' | 'manyToMany';
|
|
28
28
|
interface RelationConfig {
|
|
29
29
|
type: RelationType;
|
|
30
30
|
foreignTable: AnyTable;
|
|
31
31
|
fields?: AnySQLiteColumn[];
|
|
32
32
|
references?: AnySQLiteColumn[];
|
|
33
|
+
junctionTable?: AnyTable;
|
|
34
|
+
junctionFields?: AnySQLiteColumn[];
|
|
35
|
+
junctionReferences?: AnySQLiteColumn[];
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
declare class BaseQueryBuilder {
|
|
@@ -48,8 +51,16 @@ declare class BaseQueryBuilder {
|
|
|
48
51
|
sql: string;
|
|
49
52
|
params: any[];
|
|
50
53
|
};
|
|
54
|
+
toSQL(): {
|
|
55
|
+
sql: string;
|
|
56
|
+
params: any[];
|
|
57
|
+
};
|
|
51
58
|
}
|
|
52
59
|
|
|
60
|
+
type NestedInclude = boolean | {
|
|
61
|
+
with?: Record<string, NestedInclude>;
|
|
62
|
+
};
|
|
63
|
+
type IncludeRelations<T extends AnyTable> = Partial<Record<keyof T['relations'], NestedInclude>>;
|
|
53
64
|
declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns extends (keyof TTable['_']['columns'])[] | undefined = undefined> extends BaseQueryBuilder {
|
|
54
65
|
private table;
|
|
55
66
|
private columns?;
|
|
@@ -66,24 +77,39 @@ declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns exten
|
|
|
66
77
|
having(condition: SQLCondition): this;
|
|
67
78
|
leftJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias: string): this;
|
|
68
79
|
innerJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias: string): this;
|
|
69
|
-
include(relations:
|
|
80
|
+
include(relations: IncludeRelations<TTable>): this;
|
|
70
81
|
private buildJoins;
|
|
71
82
|
execute(): Promise<any[]>;
|
|
72
83
|
private processRelationResults;
|
|
73
84
|
all(): Promise<any[]>;
|
|
74
85
|
get(): Promise<any | undefined>;
|
|
86
|
+
toSQL(): {
|
|
87
|
+
sql: string;
|
|
88
|
+
params: any[];
|
|
89
|
+
};
|
|
75
90
|
}
|
|
76
91
|
|
|
77
92
|
declare class UpdateQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
78
93
|
private table;
|
|
79
94
|
private updateData;
|
|
80
95
|
private returningColumns;
|
|
96
|
+
private hasWhereClause;
|
|
97
|
+
private allowGlobal;
|
|
98
|
+
private incrementDecrementOps;
|
|
81
99
|
constructor(db: Database, table: T);
|
|
82
100
|
set(data: Partial<InferInsertModel<T>>): this;
|
|
101
|
+
where(condition: any): this;
|
|
102
|
+
increment(column: keyof T["_"]["columns"], value?: number): this;
|
|
103
|
+
decrement(column: keyof T["_"]["columns"], value?: number): this;
|
|
104
|
+
allowGlobalOperation(): this;
|
|
83
105
|
returning(...columns: (keyof T["_"]["columns"])[]): this;
|
|
84
106
|
private buildUpdateClause;
|
|
85
107
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
86
108
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
109
|
+
toSQL(): {
|
|
110
|
+
sql: string;
|
|
111
|
+
params: any[];
|
|
112
|
+
};
|
|
87
113
|
}
|
|
88
114
|
|
|
89
115
|
declare class InsertQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
@@ -105,15 +131,27 @@ declare class InsertQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
105
131
|
private buildConflictClause;
|
|
106
132
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
107
133
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
134
|
+
toSQL(): {
|
|
135
|
+
sql: string;
|
|
136
|
+
params: any[];
|
|
137
|
+
};
|
|
108
138
|
}
|
|
109
139
|
|
|
110
140
|
declare class DeleteQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
111
141
|
private table;
|
|
112
142
|
private returningColumns;
|
|
143
|
+
private hasWhereClause;
|
|
144
|
+
private allowGlobal;
|
|
113
145
|
constructor(db: Database, table: T);
|
|
146
|
+
where(condition: any): this;
|
|
147
|
+
allowGlobalOperation(): this;
|
|
114
148
|
returning(...columns: (keyof T['_']['columns'])[]): this;
|
|
115
149
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
116
150
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
151
|
+
toSQL(): {
|
|
152
|
+
sql: string;
|
|
153
|
+
params: any[];
|
|
154
|
+
};
|
|
117
155
|
}
|
|
118
156
|
|
|
119
157
|
declare class WithQueryBuilder {
|
|
@@ -137,6 +175,11 @@ type RelationsBuilder = {
|
|
|
137
175
|
references: AnySQLiteColumn[];
|
|
138
176
|
}) => OneRelation<U>;
|
|
139
177
|
many: <U extends AnyTable>(table: U) => ManyRelation<U>;
|
|
178
|
+
manyToMany: <U extends AnyTable>(table: U, config: {
|
|
179
|
+
junctionTable: AnyTable;
|
|
180
|
+
junctionFields: AnySQLiteColumn[];
|
|
181
|
+
junctionReferences: AnySQLiteColumn[];
|
|
182
|
+
}) => ManyToManyRelation<U>;
|
|
140
183
|
};
|
|
141
184
|
|
|
142
185
|
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> {
|
|
@@ -159,7 +202,7 @@ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDa
|
|
|
159
202
|
primaryKey(): SQLiteColumn<TName, TType, TMode, true, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
160
203
|
autoincrement(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, true, TEnum, TCustomType>;
|
|
161
204
|
unique(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
162
|
-
references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
205
|
+
references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K | T['_']['columns'][K]): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
163
206
|
$onUpdateFn(fn: () => ColumnValueTypes<TType, TMode>): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
164
207
|
$type<T>(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, T>;
|
|
165
208
|
as(alias: string): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
|
|
@@ -189,6 +232,16 @@ type SQLCondition = {
|
|
|
189
232
|
sql: string;
|
|
190
233
|
params: any[];
|
|
191
234
|
};
|
|
235
|
+
type SQLAggregate<T = number> = {
|
|
236
|
+
sql: string;
|
|
237
|
+
params: any[];
|
|
238
|
+
_type?: T;
|
|
239
|
+
};
|
|
240
|
+
type SQLSubquery = {
|
|
241
|
+
sql: string;
|
|
242
|
+
params: any[];
|
|
243
|
+
_isSubquery: true;
|
|
244
|
+
};
|
|
192
245
|
declare const asc: (column: AnySQLiteColumn) => {
|
|
193
246
|
sql: string;
|
|
194
247
|
params: never[];
|
|
@@ -258,11 +311,24 @@ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
|
258
311
|
declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
259
312
|
constructor(foreignTable: T);
|
|
260
313
|
}
|
|
314
|
+
declare class ManyToManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
315
|
+
config: {
|
|
316
|
+
junctionTable: AnyTable;
|
|
317
|
+
junctionFields: AnySQLiteColumn[];
|
|
318
|
+
junctionReferences: AnySQLiteColumn[];
|
|
319
|
+
};
|
|
320
|
+
constructor(foreignTable: T, config: {
|
|
321
|
+
junctionTable: AnyTable;
|
|
322
|
+
junctionFields: AnySQLiteColumn[];
|
|
323
|
+
junctionReferences: AnySQLiteColumn[];
|
|
324
|
+
});
|
|
325
|
+
}
|
|
261
326
|
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
|
|
262
327
|
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
263
328
|
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
264
329
|
|
|
265
330
|
declare const eq: <T>(column: AnySQLiteColumn, value: T, tableAlias?: string) => SQLCondition;
|
|
331
|
+
declare const ne: <T>(column: AnySQLiteColumn, value: T, tableAlias?: string) => SQLCondition;
|
|
266
332
|
declare const and: (...conditions: SQLCondition[]) => SQLCondition;
|
|
267
333
|
declare const or: (...conditions: SQLCondition[]) => SQLCondition;
|
|
268
334
|
declare const not: (condition: SQLCondition) => SQLCondition;
|
|
@@ -271,15 +337,43 @@ declare const gte: <T>(column: AnySQLiteColumn, value: T) => SQLCondition;
|
|
|
271
337
|
declare const lt: <T>(column: AnySQLiteColumn, value: T) => SQLCondition;
|
|
272
338
|
declare const lte: <T>(column: AnySQLiteColumn, value: T) => SQLCondition;
|
|
273
339
|
declare const like: (column: AnySQLiteColumn, pattern: string) => SQLCondition;
|
|
340
|
+
declare const ilike: (column: AnySQLiteColumn, pattern: string) => SQLCondition;
|
|
341
|
+
declare const startsWith: (column: AnySQLiteColumn, value: string) => SQLCondition;
|
|
342
|
+
declare const endsWith: (column: AnySQLiteColumn, value: string) => SQLCondition;
|
|
343
|
+
declare const contains: (column: AnySQLiteColumn, value: string) => SQLCondition;
|
|
274
344
|
declare const isNull: (column: AnySQLiteColumn) => SQLCondition;
|
|
275
345
|
declare const isNotNull: (column: AnySQLiteColumn) => SQLCondition;
|
|
276
|
-
declare const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
declare const
|
|
281
|
-
|
|
282
|
-
|
|
346
|
+
declare const exists: (subquery: {
|
|
347
|
+
sql: string;
|
|
348
|
+
params: any[];
|
|
349
|
+
}) => SQLCondition;
|
|
350
|
+
declare const notExists: (subquery: {
|
|
351
|
+
sql: string;
|
|
352
|
+
params: any[];
|
|
353
|
+
}) => SQLCondition;
|
|
354
|
+
declare const eqSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
355
|
+
declare const neSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
356
|
+
declare const gtSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
357
|
+
declare const gteSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
358
|
+
declare const ltSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
359
|
+
declare const lteSubquery: (column: AnySQLiteColumn, subquery: SQLSubquery) => SQLCondition;
|
|
360
|
+
declare const inArray: <T>(column: AnySQLiteColumn, values: T[] | SQLSubquery) => SQLCondition;
|
|
361
|
+
declare const notIn: <T>(column: AnySQLiteColumn, values: T[] | SQLSubquery) => SQLCondition;
|
|
362
|
+
declare const between: <T>(column: AnySQLiteColumn, min: T, max: T) => SQLCondition;
|
|
363
|
+
|
|
364
|
+
declare const count: (column?: AnySQLiteColumn) => SQLAggregate<number>;
|
|
365
|
+
declare const countDistinct: (column: AnySQLiteColumn) => SQLAggregate<number>;
|
|
366
|
+
declare const sum: (column: AnySQLiteColumn) => SQLAggregate<number>;
|
|
367
|
+
declare const avg: (column: AnySQLiteColumn) => SQLAggregate<number>;
|
|
368
|
+
declare const max: <T = any>(column: AnySQLiteColumn) => SQLAggregate<T>;
|
|
369
|
+
declare const min: <T = any>(column: AnySQLiteColumn) => SQLAggregate<T>;
|
|
370
|
+
declare const groupConcat: (column: AnySQLiteColumn, separator?: string) => SQLAggregate<string>;
|
|
371
|
+
declare const as: <T>(aggregate: SQLAggregate<T>, alias: string) => SQLAggregate<T> & {
|
|
372
|
+
alias: string;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
declare const subquery: <T extends AnyTable>(query: SelectQueryBuilder<T, any>) => SQLSubquery;
|
|
376
|
+
declare const scalarSubquery: <T extends AnyTable>(query: SelectQueryBuilder<T, any>) => SQLSubquery;
|
|
283
377
|
|
|
284
378
|
declare const text: <TName extends string, TEnum extends readonly string[]>(name: TName, config?: {
|
|
285
379
|
mode?: "json";
|
|
@@ -298,4 +392,35 @@ declare const numeric: <TName extends string>(name: TName, config?: {
|
|
|
298
392
|
}) => SQLiteColumn<TName, "NUMERIC", "bigint", false, false, false, never, never>;
|
|
299
393
|
declare const enumType: <TName extends string, TValues extends readonly [string, ...string[]]>(name: TName, values: TValues) => SQLiteColumn<TName, "TEXT", "json", false, false, false, TValues, never>;
|
|
300
394
|
|
|
301
|
-
|
|
395
|
+
declare class TauriORMError extends Error {
|
|
396
|
+
constructor(message: string);
|
|
397
|
+
}
|
|
398
|
+
declare class QueryBuilderError extends TauriORMError {
|
|
399
|
+
constructor(message: string);
|
|
400
|
+
}
|
|
401
|
+
declare class MissingWhereClauseError extends QueryBuilderError {
|
|
402
|
+
constructor(operation: 'UPDATE' | 'DELETE', tableName: string);
|
|
403
|
+
}
|
|
404
|
+
declare class ValidationError extends TauriORMError {
|
|
405
|
+
constructor(message: string);
|
|
406
|
+
}
|
|
407
|
+
declare class InsertValidationError extends ValidationError {
|
|
408
|
+
constructor(message: string);
|
|
409
|
+
}
|
|
410
|
+
declare class UpdateValidationError extends ValidationError {
|
|
411
|
+
constructor(message: string);
|
|
412
|
+
}
|
|
413
|
+
declare class MigrationError extends TauriORMError {
|
|
414
|
+
constructor(message: string);
|
|
415
|
+
}
|
|
416
|
+
declare class RelationError extends TauriORMError {
|
|
417
|
+
constructor(message: string);
|
|
418
|
+
}
|
|
419
|
+
declare class ColumnNotFoundError extends TauriORMError {
|
|
420
|
+
constructor(columnName: string, tableName: string);
|
|
421
|
+
}
|
|
422
|
+
declare class TableNotFoundError extends TauriORMError {
|
|
423
|
+
constructor(tableName: string);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export { type AnySQLiteColumn, type AnyTable, BaseQueryBuilder, type ColumnDataType, ColumnNotFoundError, type ColumnOptions, type ColumnValueTypes, DeleteQueryBuilder, type ExtractColumnType, type InferInsertModel, type InferSelectModel, InsertQueryBuilder, InsertValidationError, ManyRelation, ManyToManyRelation, MigrationError, MissingWhereClauseError, type Mode, OneRelation, QueryBuilderError, Relation, type RelationConfig, RelationError, type RelationType, type RelationsBuilder, type SQLAggregate, type SQLCondition, type SQLSubquery, SQLiteColumn, SelectQueryBuilder, Table, TableNotFoundError, 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, sql, sqliteTable, startsWith, subquery, sum, text };
|