@type32/tauri-sqlite-orm 0.1.2 → 0.1.4

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 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
- pnpm 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
- 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
- randomId: text("random_id").$defaultFn(() => crypto.randomUUID()),
39
- authorId: integer("author_id"),
40
- });
41
-
42
- export const usersRelations = relations(users, ({ many }) => ({
43
- posts: many(posts),
44
- }));
45
-
46
- db.configure({ users, posts }, { users: usersRelations });
47
- await db.migrateConfigured({ name: "init:users,posts" });
48
-
49
- // Create
50
- await db
51
- .insert(users)
52
- .values({ name: "Dan", email: "dan@example.com" })
53
- .execute();
54
-
55
- // Query with relations (join-based when flat)
56
- const res = await db.query.users.findMany({
57
- with: { posts: true },
58
- join: true,
59
- });
60
- ```
61
-
62
- ### Schema builder
63
-
64
- Chainable, Drizzle-style:
65
-
66
- ```ts
67
- import {
68
- defineTable,
69
- integer,
70
- text,
71
- real,
72
- blob,
73
- numeric,
74
- sql,
75
- } from "tauri-sqlite-orm";
76
-
77
- type Data = { foo: string; bar: number };
78
-
79
- export const example = defineTable("example", {
80
- id: integer("id", { isPrimaryKey: true, autoIncrement: true }).primaryKey({
81
- autoIncrement: true,
82
- }),
83
- isActive: integer("is_active", { mode: "boolean" }),
84
- createdAt: integer("created_at", { mode: "timestamp" }).default(
85
- sql`(strftime('%s','now'))`
86
- ),
87
- rating: real("rating"),
88
- status: text("status", { enum: ["active", "inactive"] as const }),
89
- name: text("name").notNull().default("Anonymous"),
90
- data: blob("data"),
91
- jsonField: blob("json_field", { mode: "json" }).$type<Data>(),
92
- bigCounter: blob("big_counter", { mode: "bigint" }),
93
- valueNumeric: numeric("value_numeric"),
94
- valueNumericNum: numeric("value_numeric_num", { mode: "number" }),
95
- valueNumericBig: numeric("value_numeric_big", { mode: "bigint" }),
96
- });
97
-
98
- // Foreign key
99
- export const posts = defineTable("posts", {
100
- id: integer("id", { isPrimaryKey: true }).primaryKey(),
101
- userId: integer("user_id")
102
- .references(() => users.id, { onDelete: "cascade" })
103
- .notNull(),
104
- });
105
- ```
106
-
107
- ### Migrations
108
-
109
- Tracked simple migrations are part of the ORM instance:
110
-
111
- ```ts
112
- // one-off for specific tables
113
- await db.migrate([users, posts], { name: "init:users,posts" });
114
-
115
- // or using configured schema
116
- await db.migrateConfigured({ name: "init:users,posts" });
117
- ```
118
-
119
- DDL emitted respects: primaryKey + autoIncrement, notNull, default(value or sql), references (with onDelete/onUpdate).
120
-
121
- ### CRUD (Drizzle-like builders)
122
-
123
- ```ts
124
- // Insert
125
- await db.insert(users).values({ name: "Alice" }).execute();
126
- await db
127
- .insert(users)
128
- .values([{ name: "A" }, { name: "B" }])
129
- .execute();
130
-
131
- // Update
132
- import { eq } from "tauri-sqlite-orm";
133
- await db
134
- .update(users)
135
- .set({ email: "new@mail.com" })
136
- .where(eq(users.id, 1))
137
- .execute();
138
-
139
- // Delete
140
- await db.delete(users).where(eq(users.id, 2)).execute();
141
- ```
142
-
143
- ### Query API (relations)
144
-
145
- Auto-generated with `db.configure(tables, relations?)`:
146
-
147
- ```ts
148
- // Flat relations with join
149
- const usersWithPosts = await db.query.users.findMany({
150
- with: { posts: true },
151
- join: true,
152
- where: { id: 1 },
153
- orderBy: ["users.id ASC"],
154
- limit: 10,
155
- offset: 0,
156
- columns: { id: true, name: true },
157
- });
158
-
159
- // Nested relations (batched loader)
160
- const nested = await db.query.users.findMany({
161
- with: {
162
- posts: {
163
- with: { comments: true },
164
- columns: ["id", "content"],
165
- },
166
- },
167
- });
168
- ```
169
-
170
- Notes:
171
-
172
- - `where` accepts SQL helpers (eq, lt, gte, like) or object map.
173
- - `columns` accepts string[] or object map of base table columns.
174
- - `join: true` only for one-level `with` (flat). Nested uses batched selects.
175
-
176
- ### SQL helpers
177
-
178
- ```ts
179
- import { eq, ne, gt, gte, lt, lte, like, asc, desc } from "tauri-sqlite-orm";
180
- db.query.posts.findMany({
181
- where: eq(posts.authorId, 1),
182
- orderBy: [asc(posts.id)],
183
- });
184
- ```
185
-
186
- ### Nuxt + Tauri usage
187
-
188
- Initialize in a client plugin and ensure `initDb()` runs once:
189
-
190
- ```ts
191
- // plugins/orm.client.ts
192
- import { initDb, db } from "tauri-sqlite-orm";
193
- import { users, posts, usersRelations } from "@/lib/schema";
194
-
195
- export default defineNuxtPlugin(async () => {
196
- await initDb("sqlite:app.db");
197
- db.configure({ users, posts }, { users: usersRelations });
198
- await db.migrateConfigured({ name: "init:users,posts" });
199
- });
200
- ```
201
-
202
- ### Roadmap
203
-
204
- - Aliasing helpers and typed orderBy (asc(users.id)) for findMany
205
- - Unique(), check(), composite primary/unique constraints
206
- - Insert returning / batch returning (where feasible)
207
-
208
- ### License
209
-
210
- MIT
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