@type32/tauri-sqlite-orm 0.1.3 → 0.1.5
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 -21
- package/README.md +247 -210
- package/dist/index.d.mts +194 -34
- package/dist/index.d.ts +194 -34
- package/dist/index.js +781 -158
- package/dist/index.mjs +758 -154
- package/package.json +7 -7
package/LICENSE
CHANGED
|
@@ -1,21 +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.
|
|
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
CHANGED
|
@@ -1,210 +1,247 @@
|
|
|
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
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
await db
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
),
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
await db
|
|
134
|
-
|
|
135
|
-
.
|
|
136
|
-
.
|
|
137
|
-
.execute();
|
|
138
|
-
|
|
139
|
-
//
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
});
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
- `
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
where: eq(
|
|
182
|
-
orderBy: [asc(
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
+
bun add @type32/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
|
+
TauriORM,
|
|
18
|
+
defineTable,
|
|
19
|
+
integer,
|
|
20
|
+
text,
|
|
21
|
+
relations,
|
|
22
|
+
} from "tauri-sqlite-orm";
|
|
23
|
+
|
|
24
|
+
const db = new TauriORM("sqlite:app.db");
|
|
25
|
+
|
|
26
|
+
export const users = defineTable("users", {
|
|
27
|
+
id: integer("id").primaryKey({ autoIncrement: true }),
|
|
28
|
+
name: text("name").notNull(),
|
|
29
|
+
email: text("email"),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export const posts = defineTable("posts", {
|
|
33
|
+
id: integer("id").primaryKey(),
|
|
34
|
+
content: text("content"),
|
|
35
|
+
randomId: text("random_id").$defaultFn(() => crypto.randomUUID()),
|
|
36
|
+
authorId: integer("author_id"),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export const usersRelations = relations(users, ({ many }) => ({
|
|
40
|
+
posts: many(posts),
|
|
41
|
+
}));
|
|
42
|
+
|
|
43
|
+
db.configure({ users, posts }, { users: usersRelations });
|
|
44
|
+
await db.migrateConfigured({ name: "init:users,posts" });
|
|
45
|
+
|
|
46
|
+
// Create
|
|
47
|
+
await db
|
|
48
|
+
.insert(users)
|
|
49
|
+
.values({ name: "Dan", email: "dan@example.com" })
|
|
50
|
+
.execute();
|
|
51
|
+
|
|
52
|
+
// Query with relations (join-based when flat)
|
|
53
|
+
const res = await db.query.users.findMany({
|
|
54
|
+
with: { posts: true },
|
|
55
|
+
join: true,
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Schema builder
|
|
60
|
+
|
|
61
|
+
Chainable, Drizzle-style:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import {
|
|
65
|
+
defineTable,
|
|
66
|
+
integer,
|
|
67
|
+
text,
|
|
68
|
+
real,
|
|
69
|
+
blob,
|
|
70
|
+
numeric,
|
|
71
|
+
sql,
|
|
72
|
+
} from "tauri-sqlite-orm";
|
|
73
|
+
|
|
74
|
+
type Data = { foo: string; bar: number };
|
|
75
|
+
|
|
76
|
+
export const example = defineTable("example", {
|
|
77
|
+
id: integer("id").primaryKey({ autoIncrement: true }),
|
|
78
|
+
isActive: integer("is_active", { mode: "boolean" }),
|
|
79
|
+
createdAt: integer("created_at", { mode: "timestamp" }).default(
|
|
80
|
+
sql`(strftime('%s','now'))`
|
|
81
|
+
),
|
|
82
|
+
rating: real("rating"),
|
|
83
|
+
status: text("status", { enum: ["active", "inactive"] as const }),
|
|
84
|
+
name: text("name").notNull().default("Anonymous"),
|
|
85
|
+
data: blob("data"),
|
|
86
|
+
jsonField: blob("json_field", { mode: "json" }).$type<Data>(),
|
|
87
|
+
bigCounter: blob("big_counter", { mode: "bigint" }),
|
|
88
|
+
valueNumeric: numeric("value_numeric"),
|
|
89
|
+
valueNumericNum: numeric("value_numeric_num", { mode: "number" }),
|
|
90
|
+
valueNumericBig: numeric("value_numeric_big", { mode: "bigint" }),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Foreign key
|
|
94
|
+
export const posts = defineTable("posts", {
|
|
95
|
+
id: integer("id").primaryKey(),
|
|
96
|
+
userId: integer("user_id")
|
|
97
|
+
.references(() => users.id, { onDelete: "cascade" })
|
|
98
|
+
.notNull(),
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### More data types and modes
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
// JSON stored in TEXT with proper SQLite JSON function support
|
|
106
|
+
const cfg = defineTable("cfg", {
|
|
107
|
+
jsonText: text("json_text", { mode: "json" }).$type<{ foo: string }>(),
|
|
108
|
+
tsMs: integer("ts_ms", { mode: "timestamp_ms" }),
|
|
109
|
+
dataBuf: blob("data_buf", { mode: "buffer" }),
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Tip: Prefer `text(name, { mode: 'json' })` over `blob(name, { mode: 'json' })` to use SQLite JSON functions.
|
|
114
|
+
|
|
115
|
+
### Migrations
|
|
116
|
+
|
|
117
|
+
Tracked simple migrations are part of the ORM instance:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
// one-off for specific tables
|
|
121
|
+
await db.migrate([users, posts], { name: "init:users,posts" });
|
|
122
|
+
|
|
123
|
+
// or using configured schema
|
|
124
|
+
await db.migrateConfigured({ name: "init:users,posts" });
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
DDL emitted respects: primaryKey + autoIncrement, notNull, default(value or sql), references (with onDelete/onUpdate).
|
|
128
|
+
|
|
129
|
+
### CRUD (Drizzle-like builders)
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
// Insert
|
|
133
|
+
await db.insert(users).values({ name: "Alice" }).execute();
|
|
134
|
+
await db
|
|
135
|
+
.insert(users)
|
|
136
|
+
.values([{ name: "A" }, { name: "B" }])
|
|
137
|
+
.execute();
|
|
138
|
+
|
|
139
|
+
// Update
|
|
140
|
+
import { eq } from "tauri-sqlite-orm";
|
|
141
|
+
await db
|
|
142
|
+
.update(users)
|
|
143
|
+
.set({ email: "new@mail.com" })
|
|
144
|
+
.where(eq(users.id, 1))
|
|
145
|
+
.execute();
|
|
146
|
+
|
|
147
|
+
// Delete
|
|
148
|
+
await db.delete(users).where(eq(users.id, 2)).execute();
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Runtime defaults and onUpdate
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { defineTable, integer, text, increments } from "tauri-sqlite-orm";
|
|
155
|
+
|
|
156
|
+
export const audit = defineTable("audit", {
|
|
157
|
+
id: increments("id"),
|
|
158
|
+
// Called on insert if value not provided
|
|
159
|
+
createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(
|
|
160
|
+
() => new Date()
|
|
161
|
+
),
|
|
162
|
+
// Called on update when not explicitly set; if no default is provided, also used on insert
|
|
163
|
+
updatedAt: integer("updated_at", { mode: "timestamp" }).$onUpdateFn(
|
|
164
|
+
() => new Date()
|
|
165
|
+
),
|
|
166
|
+
token: text("token").$default(() => crypto.randomUUID()),
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Query API (relations)
|
|
171
|
+
|
|
172
|
+
Auto-generated with `db.configure(tables, relations?)`:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
// Flat relations with join
|
|
176
|
+
import { asc } from "tauri-sqlite-orm";
|
|
177
|
+
|
|
178
|
+
const usersWithPosts = await db.query.users.findMany({
|
|
179
|
+
with: { posts: true },
|
|
180
|
+
join: true,
|
|
181
|
+
where: (users, { eq }) => eq(users.id, 1),
|
|
182
|
+
orderBy: (users, { asc }) => [asc(users.id)],
|
|
183
|
+
limit: 10,
|
|
184
|
+
offset: 0,
|
|
185
|
+
columns: { id: true, name: true },
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// Nested relations (batched loader)
|
|
189
|
+
const nested = await db.query.users.findMany({
|
|
190
|
+
with: {
|
|
191
|
+
posts: {
|
|
192
|
+
with: { comments: true },
|
|
193
|
+
columns: ["id", "content"],
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// First row helper
|
|
199
|
+
const firstUser = await db.query.users.findFirst({
|
|
200
|
+
where: (users, { eq }) => eq(users.id, 1),
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Notes:
|
|
205
|
+
|
|
206
|
+
- `where` accepts SQL helpers (eq, lt, gte, like) or object map, or a callback `(table, ops) => SQL`.
|
|
207
|
+
- `orderBy` accepts typed helpers or a callback `(table, { asc, desc }) => [...]`.
|
|
208
|
+
- `columns` accepts string[] or object map of base table columns.
|
|
209
|
+
- `join: true` only for one-level `with` (flat). Nested uses batched selects.
|
|
210
|
+
|
|
211
|
+
### SQL helpers
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
import { eq, ne, gt, gte, lt, lte, like, asc, desc } from "tauri-sqlite-orm";
|
|
215
|
+
db.query.posts.findMany({
|
|
216
|
+
where: (posts, { eq }) => eq(posts.authorId, 1),
|
|
217
|
+
orderBy: (posts, { asc }) => [asc(posts.id)],
|
|
218
|
+
});
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Nuxt + Tauri usage
|
|
222
|
+
|
|
223
|
+
Initialize in a client plugin and ensure a single ORM instance is created:
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
// plugins/orm.client.ts
|
|
227
|
+
import { TauriORM } from "tauri-sqlite-orm";
|
|
228
|
+
import { users, posts, usersRelations } from "@/lib/schema";
|
|
229
|
+
|
|
230
|
+
export default defineNuxtPlugin(async () => {
|
|
231
|
+
const db = new TauriORM("sqlite:app.db");
|
|
232
|
+
db.configure({ users, posts }, { users: usersRelations });
|
|
233
|
+
await db.migrateConfigured({ name: "init:users,posts" });
|
|
234
|
+
|
|
235
|
+
return { provide: { db } };
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Roadmap
|
|
240
|
+
|
|
241
|
+
- Aliasing helpers and typed orderBy (asc(users.id)) for findMany
|
|
242
|
+
- Unique(), check(), composite primary/unique constraints
|
|
243
|
+
- Insert returning / batch returning (where feasible)
|
|
244
|
+
|
|
245
|
+
### License
|
|
246
|
+
|
|
247
|
+
MIT
|