@type32/tauri-sqlite-orm 0.1.0
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/LICENSE +21 -0
- package/README.md +209 -0
- package/dist/index.d.mts +193 -0
- package/dist/index.d.ts +193 -0
- package/dist/index.js +764 -0
- package/dist/index.mjs +704 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Wilson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
## tauri-sqlite-orm
|
|
2
|
+
|
|
3
|
+
A Drizzle-like TypeScript ORM tailored for Tauri v2's `@tauri-apps/plugin-sql` (SQLite). Plug-and-play for Nuxt/Tauri apps: define schema in TS, run tracked migrations, and query with a soft-relations API.
|
|
4
|
+
|
|
5
|
+
### Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add tauri-sqlite-orm @tauri-apps/plugin-sql
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Make sure the SQL plugin is registered on the Rust side (see Tauri docs).
|
|
12
|
+
|
|
13
|
+
### Quick start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import {
|
|
17
|
+
initDb,
|
|
18
|
+
db,
|
|
19
|
+
defineTable,
|
|
20
|
+
integer,
|
|
21
|
+
text,
|
|
22
|
+
relations,
|
|
23
|
+
} from "tauri-sqlite-orm";
|
|
24
|
+
|
|
25
|
+
await initDb("sqlite:app.db");
|
|
26
|
+
|
|
27
|
+
export const users = defineTable("users", {
|
|
28
|
+
id: integer("id", { isPrimaryKey: true, autoIncrement: true }).primaryKey({
|
|
29
|
+
autoIncrement: true,
|
|
30
|
+
}),
|
|
31
|
+
name: text("name").notNull(),
|
|
32
|
+
email: text("email"),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export const posts = defineTable("posts", {
|
|
36
|
+
id: integer("id", { isPrimaryKey: true }).primaryKey(),
|
|
37
|
+
content: text("content"),
|
|
38
|
+
authorId: integer("author_id"),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export const usersRelations = relations(users, ({ many }) => ({
|
|
42
|
+
posts: many(posts),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
db.configure({ users, posts }, { users: usersRelations });
|
|
46
|
+
await db.migrateConfigured({ name: "init:users,posts" });
|
|
47
|
+
|
|
48
|
+
// Create
|
|
49
|
+
await db
|
|
50
|
+
.insert(users)
|
|
51
|
+
.values({ name: "Dan", email: "dan@example.com" })
|
|
52
|
+
.execute();
|
|
53
|
+
|
|
54
|
+
// Query with relations (join-based when flat)
|
|
55
|
+
const res = await db.query.users.findMany({
|
|
56
|
+
with: { posts: true },
|
|
57
|
+
join: true,
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Schema builder
|
|
62
|
+
|
|
63
|
+
Chainable, Drizzle-style:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import {
|
|
67
|
+
defineTable,
|
|
68
|
+
integer,
|
|
69
|
+
text,
|
|
70
|
+
real,
|
|
71
|
+
blob,
|
|
72
|
+
numeric,
|
|
73
|
+
sql,
|
|
74
|
+
} from "tauri-sqlite-orm";
|
|
75
|
+
|
|
76
|
+
type Data = { foo: string; bar: number };
|
|
77
|
+
|
|
78
|
+
export const example = defineTable("example", {
|
|
79
|
+
id: integer("id", { isPrimaryKey: true, autoIncrement: true }).primaryKey({
|
|
80
|
+
autoIncrement: true,
|
|
81
|
+
}),
|
|
82
|
+
isActive: integer("is_active", { mode: "boolean" }),
|
|
83
|
+
createdAt: integer("created_at", { mode: "timestamp" }).default(
|
|
84
|
+
sql`(strftime('%s','now'))`
|
|
85
|
+
),
|
|
86
|
+
rating: real("rating"),
|
|
87
|
+
status: text("status", { enum: ["active", "inactive"] as const }),
|
|
88
|
+
name: text("name").notNull().default("Anonymous"),
|
|
89
|
+
data: blob("data"),
|
|
90
|
+
jsonField: blob("json_field", { mode: "json" }).$type<Data>(),
|
|
91
|
+
bigCounter: blob("big_counter", { mode: "bigint" }),
|
|
92
|
+
valueNumeric: numeric("value_numeric"),
|
|
93
|
+
valueNumericNum: numeric("value_numeric_num", { mode: "number" }),
|
|
94
|
+
valueNumericBig: numeric("value_numeric_big", { mode: "bigint" }),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Foreign key
|
|
98
|
+
export const posts = defineTable("posts", {
|
|
99
|
+
id: integer("id", { isPrimaryKey: true }).primaryKey(),
|
|
100
|
+
userId: integer("user_id")
|
|
101
|
+
.references(() => users.id, { onDelete: "cascade" })
|
|
102
|
+
.notNull(),
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Migrations
|
|
107
|
+
|
|
108
|
+
Tracked simple migrations are part of the ORM instance:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
// one-off for specific tables
|
|
112
|
+
await db.migrate([users, posts], { name: "init:users,posts" });
|
|
113
|
+
|
|
114
|
+
// or using configured schema
|
|
115
|
+
await db.migrateConfigured({ name: "init:users,posts" });
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
DDL emitted respects: primaryKey + autoIncrement, notNull, default(value or sql), references (with onDelete/onUpdate).
|
|
119
|
+
|
|
120
|
+
### CRUD (Drizzle-like builders)
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
// Insert
|
|
124
|
+
await db.insert(users).values({ name: "Alice" }).execute();
|
|
125
|
+
await db
|
|
126
|
+
.insert(users)
|
|
127
|
+
.values([{ name: "A" }, { name: "B" }])
|
|
128
|
+
.execute();
|
|
129
|
+
|
|
130
|
+
// Update
|
|
131
|
+
import { eq } from "tauri-sqlite-orm";
|
|
132
|
+
await db
|
|
133
|
+
.update(users)
|
|
134
|
+
.set({ email: "new@mail.com" })
|
|
135
|
+
.where(eq(users.id, 1))
|
|
136
|
+
.execute();
|
|
137
|
+
|
|
138
|
+
// Delete
|
|
139
|
+
await db.delete(users).where(eq(users.id, 2)).execute();
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Query API (relations)
|
|
143
|
+
|
|
144
|
+
Auto-generated with `db.configure(tables, relations?)`:
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
// Flat relations with join
|
|
148
|
+
const usersWithPosts = await db.query.users.findMany({
|
|
149
|
+
with: { posts: true },
|
|
150
|
+
join: true,
|
|
151
|
+
where: { id: 1 },
|
|
152
|
+
orderBy: ["users.id ASC"],
|
|
153
|
+
limit: 10,
|
|
154
|
+
offset: 0,
|
|
155
|
+
columns: { id: true, name: true },
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Nested relations (batched loader)
|
|
159
|
+
const nested = await db.query.users.findMany({
|
|
160
|
+
with: {
|
|
161
|
+
posts: {
|
|
162
|
+
with: { comments: true },
|
|
163
|
+
columns: ["id", "content"],
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Notes:
|
|
170
|
+
|
|
171
|
+
- `where` accepts SQL helpers (eq, lt, gte, like) or object map.
|
|
172
|
+
- `columns` accepts string[] or object map of base table columns.
|
|
173
|
+
- `join: true` only for one-level `with` (flat). Nested uses batched selects.
|
|
174
|
+
|
|
175
|
+
### SQL helpers
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
import { eq, ne, gt, gte, lt, lte, like, asc, desc } from "tauri-sqlite-orm";
|
|
179
|
+
db.query.posts.findMany({
|
|
180
|
+
where: eq(posts.authorId, 1),
|
|
181
|
+
orderBy: [asc(posts.id)],
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Nuxt + Tauri usage
|
|
186
|
+
|
|
187
|
+
Initialize in a client plugin and ensure `initDb()` runs once:
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
// plugins/orm.client.ts
|
|
191
|
+
import { initDb, db } from "tauri-sqlite-orm";
|
|
192
|
+
import { users, posts, usersRelations } from "@/lib/schema";
|
|
193
|
+
|
|
194
|
+
export default defineNuxtPlugin(async () => {
|
|
195
|
+
await initDb("sqlite:app.db");
|
|
196
|
+
db.configure({ users, posts }, { users: usersRelations });
|
|
197
|
+
await db.migrateConfigured({ name: "init:users,posts" });
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Roadmap
|
|
202
|
+
|
|
203
|
+
- Aliasing helpers and typed orderBy (asc(users.id)) for findMany
|
|
204
|
+
- Unique(), check(), composite primary/unique constraints
|
|
205
|
+
- Insert returning / batch returning (where feasible)
|
|
206
|
+
|
|
207
|
+
### License
|
|
208
|
+
|
|
209
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import Database from '@tauri-apps/plugin-sql';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Initializes the database connection. This must be called once at the start of your application.
|
|
5
|
+
* @param dbPath The path to the database file, e.g., 'sqlite:mydatabase.db'
|
|
6
|
+
* @returns The database instance.
|
|
7
|
+
*/
|
|
8
|
+
declare function initDb(dbPath: string): Promise<Database>;
|
|
9
|
+
/**
|
|
10
|
+
* Gets the singleton database instance.
|
|
11
|
+
* @throws If `initDb` has not been called.
|
|
12
|
+
* @returns The database instance.
|
|
13
|
+
*/
|
|
14
|
+
declare function getDb(): Database;
|
|
15
|
+
|
|
16
|
+
type UpdateDeleteAction = "cascade" | "restrict" | "no action" | "set null" | "set default";
|
|
17
|
+
interface SQLExpression {
|
|
18
|
+
raw: string;
|
|
19
|
+
}
|
|
20
|
+
declare function sql(strings: TemplateStringsArray, ...values: any[]): SQLExpression;
|
|
21
|
+
interface Column<T = any> {
|
|
22
|
+
name: string;
|
|
23
|
+
type: "TEXT" | "INTEGER" | "REAL" | "BLOB" | "NUMERIC";
|
|
24
|
+
isPrimaryKey?: boolean;
|
|
25
|
+
autoIncrement?: boolean;
|
|
26
|
+
isNotNull?: boolean;
|
|
27
|
+
defaultValue?: T | SQLExpression;
|
|
28
|
+
references?: {
|
|
29
|
+
table: string;
|
|
30
|
+
column: string;
|
|
31
|
+
onDelete?: UpdateDeleteAction;
|
|
32
|
+
onUpdate?: UpdateDeleteAction;
|
|
33
|
+
};
|
|
34
|
+
enumValues?: readonly string[];
|
|
35
|
+
mode?: string;
|
|
36
|
+
_dataType: T;
|
|
37
|
+
/**
|
|
38
|
+
* Populated by defineTable so we can reason about relations and aliasing.
|
|
39
|
+
*/
|
|
40
|
+
tableName?: string;
|
|
41
|
+
}
|
|
42
|
+
type ColumnBuilder<T> = Column<T> & {
|
|
43
|
+
primaryKey: (opts?: {
|
|
44
|
+
autoIncrement?: boolean;
|
|
45
|
+
}) => ColumnBuilder<T>;
|
|
46
|
+
notNull: () => ColumnBuilder<T>;
|
|
47
|
+
default: (value: T | SQLExpression) => ColumnBuilder<T>;
|
|
48
|
+
$type: <U>() => ColumnBuilder<U>;
|
|
49
|
+
references: (target: () => Column<any>, actions?: {
|
|
50
|
+
onDelete?: UpdateDeleteAction;
|
|
51
|
+
onUpdate?: UpdateDeleteAction;
|
|
52
|
+
}) => ColumnBuilder<T>;
|
|
53
|
+
};
|
|
54
|
+
declare function text<TEnum extends string>(name: string, config?: {
|
|
55
|
+
isPrimaryKey?: boolean;
|
|
56
|
+
enum?: readonly TEnum[];
|
|
57
|
+
}): ColumnBuilder<TEnum extends string ? TEnum : string>;
|
|
58
|
+
declare function integer(name: string, config?: {
|
|
59
|
+
isPrimaryKey?: boolean;
|
|
60
|
+
mode?: "number" | "boolean" | "timestamp";
|
|
61
|
+
autoIncrement?: boolean;
|
|
62
|
+
}): ColumnBuilder<number | boolean | Date>;
|
|
63
|
+
declare function real(name: string): ColumnBuilder<number>;
|
|
64
|
+
declare function blob(name: string, config?: {
|
|
65
|
+
mode?: "json" | "bigint";
|
|
66
|
+
}): ColumnBuilder<unknown | bigint | Uint8Array>;
|
|
67
|
+
declare function numeric(name: string, config?: {
|
|
68
|
+
mode?: "string" | "number" | "bigint";
|
|
69
|
+
}): ColumnBuilder<string | number | bigint>;
|
|
70
|
+
declare const boolean: (name: string) => ColumnBuilder<boolean>;
|
|
71
|
+
declare const timestamp: (name: string) => ColumnBuilder<Date>;
|
|
72
|
+
type SchemaDefinition = Record<string, Column<any>>;
|
|
73
|
+
type InferModel<T extends SchemaDefinition> = {
|
|
74
|
+
[K in keyof T]: T[K]["_dataType"];
|
|
75
|
+
};
|
|
76
|
+
declare function defineTable<T extends SchemaDefinition>(tableName: string, schema: T): any & InferModel<T>;
|
|
77
|
+
type Table<T extends SchemaDefinition> = ReturnType<typeof defineTable<T>>;
|
|
78
|
+
|
|
79
|
+
interface SQL {
|
|
80
|
+
toSQL: () => {
|
|
81
|
+
clause: string;
|
|
82
|
+
bindings: any[];
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
declare const eq: (column: Column, value: any) => SQL;
|
|
86
|
+
declare const ne: (column: Column, value: any) => SQL;
|
|
87
|
+
declare const gt: (column: Column, value: number | Date) => SQL;
|
|
88
|
+
declare const gte: (column: Column, value: number | Date) => SQL;
|
|
89
|
+
declare const lt: (column: Column, value: number | Date) => SQL;
|
|
90
|
+
declare const lte: (column: Column, value: number | Date) => SQL;
|
|
91
|
+
declare const like: (column: Column<string>, value: string) => SQL;
|
|
92
|
+
declare const asc: (column: Column) => string;
|
|
93
|
+
declare const desc: (column: Column) => string;
|
|
94
|
+
|
|
95
|
+
declare class SelectQueryBuilder<T> {
|
|
96
|
+
private _table;
|
|
97
|
+
private _selectedColumns;
|
|
98
|
+
private _joins;
|
|
99
|
+
private _where;
|
|
100
|
+
private _orderBy;
|
|
101
|
+
private _limit;
|
|
102
|
+
private _offset;
|
|
103
|
+
private _eager;
|
|
104
|
+
constructor(fields?: Record<string, Column<any>>);
|
|
105
|
+
from(table: Table<any>): this;
|
|
106
|
+
where(...conditions: SQL[]): this;
|
|
107
|
+
leftJoin(otherTable: Table<any>, on: SQL): this;
|
|
108
|
+
orderBy(...clauses: string[]): this;
|
|
109
|
+
limit(value: number): this;
|
|
110
|
+
offset(value: number): this;
|
|
111
|
+
execute(): Promise<T[]>;
|
|
112
|
+
}
|
|
113
|
+
declare class TauriORM {
|
|
114
|
+
query: Record<string, any>;
|
|
115
|
+
private _tables;
|
|
116
|
+
private _relations;
|
|
117
|
+
configureQuery(tables: Record<string, Table<any>>, relations: Record<string, Record<string, RelationConfig>>): void;
|
|
118
|
+
select<T>(fields?: Record<string, Column<any>>): SelectQueryBuilder<T>;
|
|
119
|
+
insert(table: Table<any>): {
|
|
120
|
+
_table: any;
|
|
121
|
+
_rows: Record<string, any>[];
|
|
122
|
+
values(rowOrRows: Record<string, any> | Record<string, any>[]): /*elided*/ any;
|
|
123
|
+
execute(): Promise<void>;
|
|
124
|
+
};
|
|
125
|
+
update(table: Table<any>): {
|
|
126
|
+
_table: any;
|
|
127
|
+
_data: Record<string, any> | null;
|
|
128
|
+
_where: Record<string, any> | SQL | null;
|
|
129
|
+
set(data: Record<string, any>): /*elided*/ any;
|
|
130
|
+
where(cond: Record<string, any> | SQL): /*elided*/ any;
|
|
131
|
+
execute(): Promise<void>;
|
|
132
|
+
};
|
|
133
|
+
delete(table: Table<any>): {
|
|
134
|
+
_table: any;
|
|
135
|
+
_where: Record<string, any> | SQL | null;
|
|
136
|
+
where(cond: Record<string, any> | SQL): /*elided*/ any;
|
|
137
|
+
execute(): Promise<void>;
|
|
138
|
+
};
|
|
139
|
+
run(query: string, bindings?: any[]): Promise<void>;
|
|
140
|
+
private generateCreateTableSql;
|
|
141
|
+
createTableIfNotExists(table: Table<any>): Promise<void>;
|
|
142
|
+
createTablesIfNotExist(tables: Table<any>[]): Promise<void>;
|
|
143
|
+
private ensureMigrationsTable;
|
|
144
|
+
private hasMigration;
|
|
145
|
+
private recordMigration;
|
|
146
|
+
migrate(tables: Table<any>[], options?: {
|
|
147
|
+
name?: string;
|
|
148
|
+
track?: boolean;
|
|
149
|
+
}): Promise<void>;
|
|
150
|
+
configure(tables: Record<string, Table<any>>, relDefs?: Record<string, Record<string, RelationConfig>>): this;
|
|
151
|
+
migrateConfigured(options?: {
|
|
152
|
+
name?: string;
|
|
153
|
+
track?: boolean;
|
|
154
|
+
}): Promise<void>;
|
|
155
|
+
}
|
|
156
|
+
declare const db: TauriORM;
|
|
157
|
+
type OneConfig = {
|
|
158
|
+
fields?: Column[];
|
|
159
|
+
references?: Column[];
|
|
160
|
+
relationName?: string;
|
|
161
|
+
};
|
|
162
|
+
type ManyConfig = {
|
|
163
|
+
relationName?: string;
|
|
164
|
+
};
|
|
165
|
+
type RelationBuilderCtx = {
|
|
166
|
+
one: (table: Table<any>, cfg?: OneConfig) => OneRelation;
|
|
167
|
+
many: (table: Table<any>, cfg?: ManyConfig) => ManyRelation;
|
|
168
|
+
};
|
|
169
|
+
type OneRelation = {
|
|
170
|
+
kind: "one";
|
|
171
|
+
table: Table<any>;
|
|
172
|
+
cfg?: OneConfig;
|
|
173
|
+
};
|
|
174
|
+
type ManyRelation = {
|
|
175
|
+
kind: "many";
|
|
176
|
+
table: Table<any>;
|
|
177
|
+
cfg?: ManyConfig;
|
|
178
|
+
};
|
|
179
|
+
declare function relations(baseTable: Table<any>, builder: (ctx: RelationBuilderCtx) => Record<string, OneRelation | ManyRelation>): Record<string, OneRelation | ManyRelation>;
|
|
180
|
+
type RelationConfig = OneRelation | ManyRelation;
|
|
181
|
+
type WithSpec = Record<string, boolean | {
|
|
182
|
+
with?: WithSpec;
|
|
183
|
+
columns?: string[];
|
|
184
|
+
}>;
|
|
185
|
+
declare function makeQueryAPI(tables: Record<string, Table<any>>, relDefs: Record<string, Record<string, RelationConfig>>): { [K in keyof typeof tables]: {
|
|
186
|
+
findMany: (opts?: {
|
|
187
|
+
with?: WithSpec;
|
|
188
|
+
join?: boolean;
|
|
189
|
+
columns?: string[];
|
|
190
|
+
}) => Promise<any[]>;
|
|
191
|
+
}; };
|
|
192
|
+
|
|
193
|
+
export { type Column, type ManyRelation, type OneRelation, type SQL, type SQLExpression, type Table, TauriORM, type UpdateDeleteAction, asc, blob, boolean, db, defineTable, desc, eq, getDb, gt, gte, initDb, integer, like, lt, lte, makeQueryAPI, ne, numeric, real, relations, sql, text, timestamp };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import Database from '@tauri-apps/plugin-sql';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Initializes the database connection. This must be called once at the start of your application.
|
|
5
|
+
* @param dbPath The path to the database file, e.g., 'sqlite:mydatabase.db'
|
|
6
|
+
* @returns The database instance.
|
|
7
|
+
*/
|
|
8
|
+
declare function initDb(dbPath: string): Promise<Database>;
|
|
9
|
+
/**
|
|
10
|
+
* Gets the singleton database instance.
|
|
11
|
+
* @throws If `initDb` has not been called.
|
|
12
|
+
* @returns The database instance.
|
|
13
|
+
*/
|
|
14
|
+
declare function getDb(): Database;
|
|
15
|
+
|
|
16
|
+
type UpdateDeleteAction = "cascade" | "restrict" | "no action" | "set null" | "set default";
|
|
17
|
+
interface SQLExpression {
|
|
18
|
+
raw: string;
|
|
19
|
+
}
|
|
20
|
+
declare function sql(strings: TemplateStringsArray, ...values: any[]): SQLExpression;
|
|
21
|
+
interface Column<T = any> {
|
|
22
|
+
name: string;
|
|
23
|
+
type: "TEXT" | "INTEGER" | "REAL" | "BLOB" | "NUMERIC";
|
|
24
|
+
isPrimaryKey?: boolean;
|
|
25
|
+
autoIncrement?: boolean;
|
|
26
|
+
isNotNull?: boolean;
|
|
27
|
+
defaultValue?: T | SQLExpression;
|
|
28
|
+
references?: {
|
|
29
|
+
table: string;
|
|
30
|
+
column: string;
|
|
31
|
+
onDelete?: UpdateDeleteAction;
|
|
32
|
+
onUpdate?: UpdateDeleteAction;
|
|
33
|
+
};
|
|
34
|
+
enumValues?: readonly string[];
|
|
35
|
+
mode?: string;
|
|
36
|
+
_dataType: T;
|
|
37
|
+
/**
|
|
38
|
+
* Populated by defineTable so we can reason about relations and aliasing.
|
|
39
|
+
*/
|
|
40
|
+
tableName?: string;
|
|
41
|
+
}
|
|
42
|
+
type ColumnBuilder<T> = Column<T> & {
|
|
43
|
+
primaryKey: (opts?: {
|
|
44
|
+
autoIncrement?: boolean;
|
|
45
|
+
}) => ColumnBuilder<T>;
|
|
46
|
+
notNull: () => ColumnBuilder<T>;
|
|
47
|
+
default: (value: T | SQLExpression) => ColumnBuilder<T>;
|
|
48
|
+
$type: <U>() => ColumnBuilder<U>;
|
|
49
|
+
references: (target: () => Column<any>, actions?: {
|
|
50
|
+
onDelete?: UpdateDeleteAction;
|
|
51
|
+
onUpdate?: UpdateDeleteAction;
|
|
52
|
+
}) => ColumnBuilder<T>;
|
|
53
|
+
};
|
|
54
|
+
declare function text<TEnum extends string>(name: string, config?: {
|
|
55
|
+
isPrimaryKey?: boolean;
|
|
56
|
+
enum?: readonly TEnum[];
|
|
57
|
+
}): ColumnBuilder<TEnum extends string ? TEnum : string>;
|
|
58
|
+
declare function integer(name: string, config?: {
|
|
59
|
+
isPrimaryKey?: boolean;
|
|
60
|
+
mode?: "number" | "boolean" | "timestamp";
|
|
61
|
+
autoIncrement?: boolean;
|
|
62
|
+
}): ColumnBuilder<number | boolean | Date>;
|
|
63
|
+
declare function real(name: string): ColumnBuilder<number>;
|
|
64
|
+
declare function blob(name: string, config?: {
|
|
65
|
+
mode?: "json" | "bigint";
|
|
66
|
+
}): ColumnBuilder<unknown | bigint | Uint8Array>;
|
|
67
|
+
declare function numeric(name: string, config?: {
|
|
68
|
+
mode?: "string" | "number" | "bigint";
|
|
69
|
+
}): ColumnBuilder<string | number | bigint>;
|
|
70
|
+
declare const boolean: (name: string) => ColumnBuilder<boolean>;
|
|
71
|
+
declare const timestamp: (name: string) => ColumnBuilder<Date>;
|
|
72
|
+
type SchemaDefinition = Record<string, Column<any>>;
|
|
73
|
+
type InferModel<T extends SchemaDefinition> = {
|
|
74
|
+
[K in keyof T]: T[K]["_dataType"];
|
|
75
|
+
};
|
|
76
|
+
declare function defineTable<T extends SchemaDefinition>(tableName: string, schema: T): any & InferModel<T>;
|
|
77
|
+
type Table<T extends SchemaDefinition> = ReturnType<typeof defineTable<T>>;
|
|
78
|
+
|
|
79
|
+
interface SQL {
|
|
80
|
+
toSQL: () => {
|
|
81
|
+
clause: string;
|
|
82
|
+
bindings: any[];
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
declare const eq: (column: Column, value: any) => SQL;
|
|
86
|
+
declare const ne: (column: Column, value: any) => SQL;
|
|
87
|
+
declare const gt: (column: Column, value: number | Date) => SQL;
|
|
88
|
+
declare const gte: (column: Column, value: number | Date) => SQL;
|
|
89
|
+
declare const lt: (column: Column, value: number | Date) => SQL;
|
|
90
|
+
declare const lte: (column: Column, value: number | Date) => SQL;
|
|
91
|
+
declare const like: (column: Column<string>, value: string) => SQL;
|
|
92
|
+
declare const asc: (column: Column) => string;
|
|
93
|
+
declare const desc: (column: Column) => string;
|
|
94
|
+
|
|
95
|
+
declare class SelectQueryBuilder<T> {
|
|
96
|
+
private _table;
|
|
97
|
+
private _selectedColumns;
|
|
98
|
+
private _joins;
|
|
99
|
+
private _where;
|
|
100
|
+
private _orderBy;
|
|
101
|
+
private _limit;
|
|
102
|
+
private _offset;
|
|
103
|
+
private _eager;
|
|
104
|
+
constructor(fields?: Record<string, Column<any>>);
|
|
105
|
+
from(table: Table<any>): this;
|
|
106
|
+
where(...conditions: SQL[]): this;
|
|
107
|
+
leftJoin(otherTable: Table<any>, on: SQL): this;
|
|
108
|
+
orderBy(...clauses: string[]): this;
|
|
109
|
+
limit(value: number): this;
|
|
110
|
+
offset(value: number): this;
|
|
111
|
+
execute(): Promise<T[]>;
|
|
112
|
+
}
|
|
113
|
+
declare class TauriORM {
|
|
114
|
+
query: Record<string, any>;
|
|
115
|
+
private _tables;
|
|
116
|
+
private _relations;
|
|
117
|
+
configureQuery(tables: Record<string, Table<any>>, relations: Record<string, Record<string, RelationConfig>>): void;
|
|
118
|
+
select<T>(fields?: Record<string, Column<any>>): SelectQueryBuilder<T>;
|
|
119
|
+
insert(table: Table<any>): {
|
|
120
|
+
_table: any;
|
|
121
|
+
_rows: Record<string, any>[];
|
|
122
|
+
values(rowOrRows: Record<string, any> | Record<string, any>[]): /*elided*/ any;
|
|
123
|
+
execute(): Promise<void>;
|
|
124
|
+
};
|
|
125
|
+
update(table: Table<any>): {
|
|
126
|
+
_table: any;
|
|
127
|
+
_data: Record<string, any> | null;
|
|
128
|
+
_where: Record<string, any> | SQL | null;
|
|
129
|
+
set(data: Record<string, any>): /*elided*/ any;
|
|
130
|
+
where(cond: Record<string, any> | SQL): /*elided*/ any;
|
|
131
|
+
execute(): Promise<void>;
|
|
132
|
+
};
|
|
133
|
+
delete(table: Table<any>): {
|
|
134
|
+
_table: any;
|
|
135
|
+
_where: Record<string, any> | SQL | null;
|
|
136
|
+
where(cond: Record<string, any> | SQL): /*elided*/ any;
|
|
137
|
+
execute(): Promise<void>;
|
|
138
|
+
};
|
|
139
|
+
run(query: string, bindings?: any[]): Promise<void>;
|
|
140
|
+
private generateCreateTableSql;
|
|
141
|
+
createTableIfNotExists(table: Table<any>): Promise<void>;
|
|
142
|
+
createTablesIfNotExist(tables: Table<any>[]): Promise<void>;
|
|
143
|
+
private ensureMigrationsTable;
|
|
144
|
+
private hasMigration;
|
|
145
|
+
private recordMigration;
|
|
146
|
+
migrate(tables: Table<any>[], options?: {
|
|
147
|
+
name?: string;
|
|
148
|
+
track?: boolean;
|
|
149
|
+
}): Promise<void>;
|
|
150
|
+
configure(tables: Record<string, Table<any>>, relDefs?: Record<string, Record<string, RelationConfig>>): this;
|
|
151
|
+
migrateConfigured(options?: {
|
|
152
|
+
name?: string;
|
|
153
|
+
track?: boolean;
|
|
154
|
+
}): Promise<void>;
|
|
155
|
+
}
|
|
156
|
+
declare const db: TauriORM;
|
|
157
|
+
type OneConfig = {
|
|
158
|
+
fields?: Column[];
|
|
159
|
+
references?: Column[];
|
|
160
|
+
relationName?: string;
|
|
161
|
+
};
|
|
162
|
+
type ManyConfig = {
|
|
163
|
+
relationName?: string;
|
|
164
|
+
};
|
|
165
|
+
type RelationBuilderCtx = {
|
|
166
|
+
one: (table: Table<any>, cfg?: OneConfig) => OneRelation;
|
|
167
|
+
many: (table: Table<any>, cfg?: ManyConfig) => ManyRelation;
|
|
168
|
+
};
|
|
169
|
+
type OneRelation = {
|
|
170
|
+
kind: "one";
|
|
171
|
+
table: Table<any>;
|
|
172
|
+
cfg?: OneConfig;
|
|
173
|
+
};
|
|
174
|
+
type ManyRelation = {
|
|
175
|
+
kind: "many";
|
|
176
|
+
table: Table<any>;
|
|
177
|
+
cfg?: ManyConfig;
|
|
178
|
+
};
|
|
179
|
+
declare function relations(baseTable: Table<any>, builder: (ctx: RelationBuilderCtx) => Record<string, OneRelation | ManyRelation>): Record<string, OneRelation | ManyRelation>;
|
|
180
|
+
type RelationConfig = OneRelation | ManyRelation;
|
|
181
|
+
type WithSpec = Record<string, boolean | {
|
|
182
|
+
with?: WithSpec;
|
|
183
|
+
columns?: string[];
|
|
184
|
+
}>;
|
|
185
|
+
declare function makeQueryAPI(tables: Record<string, Table<any>>, relDefs: Record<string, Record<string, RelationConfig>>): { [K in keyof typeof tables]: {
|
|
186
|
+
findMany: (opts?: {
|
|
187
|
+
with?: WithSpec;
|
|
188
|
+
join?: boolean;
|
|
189
|
+
columns?: string[];
|
|
190
|
+
}) => Promise<any[]>;
|
|
191
|
+
}; };
|
|
192
|
+
|
|
193
|
+
export { type Column, type ManyRelation, type OneRelation, type SQL, type SQLExpression, type Table, TauriORM, type UpdateDeleteAction, asc, blob, boolean, db, defineTable, desc, eq, getDb, gt, gte, initDb, integer, like, lt, lte, makeQueryAPI, ne, numeric, real, relations, sql, text, timestamp };
|