@b4moss/crudian 0.5.0 → 0.6.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/README.md +332 -22
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/libsql/crud.d.ts +31 -0
- package/dist/libsql/crud.d.ts.map +1 -0
- package/dist/libsql/crud.js +62 -0
- package/dist/libsql/index.d.ts +8 -0
- package/dist/libsql/index.d.ts.map +1 -0
- package/dist/libsql/index.js +6 -0
- package/package.json +16 -4
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
CRUD abstraction for DDD repositories.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
One npm package, adapter subpaths. Shared contracts live at the package root; backends are imported from subpaths. Callers inject their own DB/client — crudian never opens connections or reads env secrets for you.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -10,15 +10,18 @@ Single npm package with adapter subpaths. Shared contracts live at the package r
|
|
|
10
10
|
npm install @b4moss/crudian
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Install only the peer deps for the adapter you use:
|
|
14
14
|
|
|
15
|
-
| Subpath | Peer dependencies |
|
|
16
|
-
|
|
17
|
-
| `@b4moss/crudian/bun-sqlite` | Bun (`bun:sqlite`) |
|
|
18
|
-
| `@b4moss/crudian/drizzle` | `drizzle-orm`, `better-sqlite3` |
|
|
19
|
-
| `@b4moss/crudian/prisma` | `@prisma/client` |
|
|
15
|
+
| Subpath | Runtime | Sync / async | Peer dependencies |
|
|
16
|
+
|---------|---------|--------------|-------------------|
|
|
17
|
+
| `@b4moss/crudian/bun-sqlite` | Bun | sync | Bun (`bun:sqlite`) |
|
|
18
|
+
| `@b4moss/crudian/drizzle` | Node.js 24+ | sync | `drizzle-orm`, `better-sqlite3` |
|
|
19
|
+
| `@b4moss/crudian/prisma` | Node.js 24+ | async | `@prisma/client` |
|
|
20
|
+
| `@b4moss/crudian/libsql` | Node.js 24+ / Bun | async | `@libsql/client` |
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
Node must not import `@b4moss/crudian/bun-sqlite` (resolves to an explicit error stub).
|
|
23
|
+
|
|
24
|
+
## Quick start (adapters)
|
|
22
25
|
|
|
23
26
|
### Shared contracts
|
|
24
27
|
|
|
@@ -26,7 +29,7 @@ Adapter peers (install only what you use):
|
|
|
26
29
|
import { where, CrudianError } from "@b4moss/crudian"
|
|
27
30
|
```
|
|
28
31
|
|
|
29
|
-
### Bun (`bun:sqlite`)
|
|
32
|
+
### Bun (`bun:sqlite`) — sync
|
|
30
33
|
|
|
31
34
|
```ts
|
|
32
35
|
import { Database } from "bun:sqlite"
|
|
@@ -34,15 +37,19 @@ import { createCrud } from "@b4moss/crudian/bun-sqlite"
|
|
|
34
37
|
import { where } from "@b4moss/crudian"
|
|
35
38
|
|
|
36
39
|
const db = new Database(":memory:")
|
|
40
|
+
db.exec(`
|
|
41
|
+
CREATE TABLE items (
|
|
42
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
43
|
+
name TEXT NOT NULL,
|
|
44
|
+
score INTEGER,
|
|
45
|
+
note TEXT
|
|
46
|
+
)
|
|
47
|
+
`)
|
|
37
48
|
const crud = createCrud(db)
|
|
38
|
-
|
|
39
|
-
const row = crud.create("items", { name: "alpha", score: 1 })
|
|
40
|
-
const found = crud.read("items", { where: where().eq("id", row.id) })
|
|
49
|
+
// raw DB is also available as crud.db
|
|
41
50
|
```
|
|
42
51
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
### Drizzle (better-sqlite3)
|
|
52
|
+
### Drizzle (better-sqlite3) — sync
|
|
46
53
|
|
|
47
54
|
```ts
|
|
48
55
|
import Database from "better-sqlite3"
|
|
@@ -50,11 +57,17 @@ import { drizzle } from "drizzle-orm/better-sqlite3"
|
|
|
50
57
|
import { createCrud } from "@b4moss/crudian/drizzle"
|
|
51
58
|
|
|
52
59
|
const sqlite = new Database(":memory:")
|
|
60
|
+
sqlite.exec(`CREATE TABLE items (
|
|
61
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
62
|
+
name TEXT NOT NULL,
|
|
63
|
+
score INTEGER,
|
|
64
|
+
note TEXT
|
|
65
|
+
)`)
|
|
53
66
|
const db = drizzle(sqlite)
|
|
54
67
|
const crud = createCrud(db)
|
|
55
68
|
```
|
|
56
69
|
|
|
57
|
-
### Prisma (SQLite)
|
|
70
|
+
### Prisma (SQLite) — async
|
|
58
71
|
|
|
59
72
|
```ts
|
|
60
73
|
import { PrismaClient } from "@prisma/client"
|
|
@@ -66,15 +79,312 @@ const crud = createCrud(client)
|
|
|
66
79
|
await crud.create("items", { name: "alpha", score: 1 })
|
|
67
80
|
```
|
|
68
81
|
|
|
69
|
-
|
|
82
|
+
### libSQL (`@libsql/client`) — async
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { createClient } from "@libsql/client"
|
|
86
|
+
import { createCrud } from "@b4moss/crudian/libsql"
|
|
87
|
+
|
|
88
|
+
const client = createClient({
|
|
89
|
+
url: process.env.LIBSQL_URL ?? "file:local.db",
|
|
90
|
+
authToken: process.env.LIBSQL_AUTH_TOKEN, // optional; set for remote hosts
|
|
91
|
+
})
|
|
92
|
+
const crud = createCrud(client)
|
|
93
|
+
|
|
94
|
+
await crud.create("items", { name: "alpha", score: 1 })
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
URL / auth token belong on the caller-created client (typically from `.env`).
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## API reference (all methods)
|
|
102
|
+
|
|
103
|
+
Examples below use the **sync** Bun adapter. For `prisma` / `libsql`, `await` every call (same shapes).
|
|
104
|
+
|
|
105
|
+
Assume a table:
|
|
106
|
+
|
|
107
|
+
```sql
|
|
108
|
+
CREATE TABLE items (
|
|
109
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
110
|
+
name TEXT NOT NULL,
|
|
111
|
+
score INTEGER,
|
|
112
|
+
note TEXT
|
|
113
|
+
);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { Database } from "bun:sqlite"
|
|
118
|
+
import { createCrud } from "@b4moss/crudian/bun-sqlite"
|
|
119
|
+
import { where } from "@b4moss/crudian"
|
|
120
|
+
|
|
121
|
+
const db = new Database(":memory:")
|
|
122
|
+
db.exec(/* schema above */)
|
|
123
|
+
const crud = createCrud(db)
|
|
124
|
+
|
|
125
|
+
type Item = { id: number; name: string; score: number | null; note: string | null }
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### `create(table, cols)` → row
|
|
129
|
+
|
|
130
|
+
Inserts one row and returns it (including generated `id`).
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
const row = crud.create<Item>("items", { name: "alpha", score: 10, note: "n" })
|
|
134
|
+
// { id: 1, name: "alpha", score: 10, note: "n" }
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### `read(table, query?)` → row | `null`
|
|
138
|
+
|
|
139
|
+
Reads one row. Misses return `null` (does not throw).
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
const found = crud.read<Item>("items", {
|
|
143
|
+
columns: ["id", "name"],
|
|
144
|
+
where: where().eq("id", row.id),
|
|
145
|
+
})
|
|
146
|
+
// { id: 1, name: "alpha" }
|
|
147
|
+
|
|
148
|
+
const missing = crud.read<Item>("items", { where: where().eq("id", 999) })
|
|
149
|
+
// null
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### `update(table, cols, query)` → row | `null`
|
|
153
|
+
|
|
154
|
+
Updates matching rows; returns the updated row. Zero matches → `null`. `where` is required.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
const updated = crud.update<Item>(
|
|
158
|
+
"items",
|
|
159
|
+
{ score: 9 },
|
|
160
|
+
{ where: where().eq("id", row.id) },
|
|
161
|
+
)
|
|
162
|
+
// { id: 1, name: "alpha", score: 9, note: "n" }
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### `delete(table, query)` → number
|
|
166
|
+
|
|
167
|
+
Deletes matching rows; returns affected count. `where` is required.
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
const deleted = crud.delete("items", { where: where().eq("id", row.id) })
|
|
171
|
+
// 1
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### `search(table, query?)` → `{ items, nextCursor, hasMore, total }`
|
|
175
|
+
|
|
176
|
+
Canonical list API. Cursor pagination on `id` ascending. `total` is the full where-match count (ignores `limit` / `cursor`).
|
|
177
|
+
|
|
178
|
+
`SearchQuery` fields:
|
|
179
|
+
|
|
180
|
+
| Field | Meaning |
|
|
181
|
+
|-------|---------|
|
|
182
|
+
| `columns?` | Column list for `SELECT` (same idea as `read`). Omit → `*` |
|
|
183
|
+
| `where?` | Condition builder / node |
|
|
184
|
+
| `limit?` | Page size (default `20`) |
|
|
185
|
+
| `cursor?` | Raw `id` cursor (keyset); rows with `id > cursor` |
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
for (let i = 0; i < 5; i++) {
|
|
189
|
+
crud.create("items", { name: `n${i}`, score: i })
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const page1 = crud.search<Item>("items", { limit: 2 })
|
|
193
|
+
// {
|
|
194
|
+
// items: [ { id: 1, ... }, { id: 2, ... } ],
|
|
195
|
+
// nextCursor: 2,
|
|
196
|
+
// hasMore: true,
|
|
197
|
+
// total: 5,
|
|
198
|
+
// }
|
|
199
|
+
|
|
200
|
+
const page2 = crud.search<Item>("items", {
|
|
201
|
+
limit: 2,
|
|
202
|
+
cursor: page1.nextCursor,
|
|
203
|
+
})
|
|
204
|
+
// items ids [3, 4], total still 5
|
|
205
|
+
|
|
206
|
+
const filtered = crud.search<Item>("items", {
|
|
207
|
+
columns: ["id", "name"],
|
|
208
|
+
where: where().gte("score", 3),
|
|
209
|
+
limit: 10,
|
|
210
|
+
})
|
|
211
|
+
// each item is { id, name } only
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### `list(table, query?)` → same as `search`
|
|
215
|
+
|
|
216
|
+
Thin alias of `search` (same `SearchQuery`, including `columns`).
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
const a = crud.list<Item>("items", {
|
|
220
|
+
columns: ["id", "name"],
|
|
221
|
+
limit: 10,
|
|
222
|
+
})
|
|
223
|
+
const b = crud.search<Item>("items", {
|
|
224
|
+
columns: ["id", "name"],
|
|
225
|
+
limit: 10,
|
|
226
|
+
})
|
|
227
|
+
// a and b are deep-equal
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### `count(table, query?)` → number
|
|
231
|
+
|
|
232
|
+
Where-match count only (`CountQuery` is `{ where? }`). No `limit` / `cursor` / `columns`.
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
crud.count("items") // 5
|
|
236
|
+
crud.count("items", { where: where().eq("name", "n0") }) // 1
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### `upsert(table, cols)` → row
|
|
240
|
+
|
|
241
|
+
Conflict target is primary key `id`. Inserts or updates; returns the row. `id` is required in `cols`.
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
const inserted = crud.upsert<Item>("items", { id: 10, name: "new", score: 1 })
|
|
245
|
+
const again = crud.upsert<Item>("items", { id: 10, name: "upd", score: 9 })
|
|
246
|
+
// again.name === "upd"
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### `duplicate(table, query)` → row | `null`
|
|
250
|
+
|
|
251
|
+
Copies the first matching row (new `id`). Optional `overrides`. Zero matches → `null`. `where` is required.
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
const source = crud.create<Item>("items", { name: "a", score: 1, note: "n" })
|
|
255
|
+
const copy = crud.duplicate<Item>("items", {
|
|
256
|
+
where: where().eq("id", source.id),
|
|
257
|
+
overrides: { name: "b" },
|
|
258
|
+
})
|
|
259
|
+
// copy.id !== source.id, copy.name === "b"
|
|
260
|
+
|
|
261
|
+
const none = crud.duplicate<Item>("items", { where: where().eq("id", 999) })
|
|
262
|
+
// null
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### `bulkCreate(table, rows)` → number
|
|
266
|
+
|
|
267
|
+
Inserts many rows; returns inserted count. Empty array → `0`.
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
const n = crud.bulkCreate("items", [
|
|
271
|
+
{ name: "x", score: 1 },
|
|
272
|
+
{ name: "y", score: 2 },
|
|
273
|
+
])
|
|
274
|
+
// 2
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### `bulkUpdate(table, cols, query)` → number
|
|
278
|
+
|
|
279
|
+
Updates many rows; returns affected count. `where` is required.
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
const n = crud.bulkUpdate(
|
|
283
|
+
"items",
|
|
284
|
+
{ score: 8 },
|
|
285
|
+
{ where: where().eq("name", "x") },
|
|
286
|
+
)
|
|
287
|
+
// 1
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### `bulkDelete(table, query)` → number
|
|
291
|
+
|
|
292
|
+
Deletes many rows; returns affected count. `where` is required.
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
const n = crud.bulkDelete("items", { where: where().eq("name", "y") })
|
|
296
|
+
// 1
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### `bulkUpsert(table, rows)` → number
|
|
300
|
+
|
|
301
|
+
Upserts many rows (each row needs `id`); returns processed count. Empty array → `0`.
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
const n = crud.bulkUpsert("items", [
|
|
305
|
+
{ id: 100, name: "z", score: 1 },
|
|
306
|
+
{ id: 10, name: "z2", score: 2 },
|
|
307
|
+
])
|
|
308
|
+
// 2
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### `transaction(fn)` → `fn` return value
|
|
312
|
+
|
|
313
|
+
Runs `fn` inside a transaction helper. Crudian does **not** auto-wrap each CRUD call; use this when you need atomic multi-step work. Success commits; throw rolls back.
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
const result = crud.transaction(() => {
|
|
317
|
+
crud.create("items", { name: "a", score: 1 })
|
|
318
|
+
crud.create("items", { name: "b", score: 2 })
|
|
319
|
+
return "ok"
|
|
320
|
+
})
|
|
321
|
+
// result === "ok"; both rows visible afterward
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
crud.transaction(() => {
|
|
325
|
+
crud.create("items", { name: "c", score: 3 })
|
|
326
|
+
throw new Error("boom")
|
|
327
|
+
})
|
|
328
|
+
} catch {
|
|
329
|
+
// partial writes from that callback are rolled back
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Async adapters:
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
await crud.transaction(async () => {
|
|
337
|
+
await crud.create("items", { name: "a", score: 1 })
|
|
338
|
+
await crud.create("items", { name: "b", score: 2 })
|
|
339
|
+
})
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
## `where()` builder
|
|
345
|
+
|
|
346
|
+
```ts
|
|
347
|
+
import { where } from "@b4moss/crudian"
|
|
348
|
+
|
|
349
|
+
where().eq("name", "alpha")
|
|
350
|
+
where().ne("score", 0)
|
|
351
|
+
where().lt("score", 10)
|
|
352
|
+
where().lte("score", 10)
|
|
353
|
+
where().gt("score", 0)
|
|
354
|
+
where().gte("score", 0)
|
|
355
|
+
where().in("name", ["a", "b"])
|
|
356
|
+
where().like("name", "a%")
|
|
357
|
+
where().isNull("note")
|
|
358
|
+
where().isNotNull("note")
|
|
359
|
+
|
|
360
|
+
where()
|
|
361
|
+
.eq("name", "alpha")
|
|
362
|
+
.and(where().gte("score", 5))
|
|
363
|
+
|
|
364
|
+
where()
|
|
365
|
+
.eq("name", "alpha")
|
|
366
|
+
.or(where().eq("name", "beta"))
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
Nestable `and` / `or`. Empty `in([])` is rejected.
|
|
70
370
|
|
|
71
|
-
|
|
371
|
+
---
|
|
72
372
|
|
|
73
|
-
|
|
373
|
+
## Behavior notes
|
|
74
374
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
375
|
+
| Topic | Behavior |
|
|
376
|
+
|-------|----------|
|
|
377
|
+
| Entry | `createCrud(db)` — inject a caller-owned client; exposed as `crud.db` |
|
|
378
|
+
| Sync vs async | `bun-sqlite` / `drizzle` sync; `prisma` / `libsql` return `Promise`s |
|
|
379
|
+
| `read` / `update` / `duplicate` miss | `null` |
|
|
380
|
+
| `delete` / bulk miss | `0` |
|
|
381
|
+
| Upsert conflict | primary key `id` |
|
|
382
|
+
| Pagination | cursor on `id` ASC only (no offset) |
|
|
383
|
+
| `columns` | optional on `read` / `search` / `list`; omit → `*` |
|
|
384
|
+
| `search.total` / `count` | full where count; not page length |
|
|
385
|
+
| Errors | minimal `CrudianError`; other errors propagate from the driver |
|
|
386
|
+
| Identifiers | string required; no format validation |
|
|
387
|
+
| Out of scope | relations, migrations, full-text search, ORM models |
|
|
78
388
|
|
|
79
389
|
## License
|
|
80
390
|
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* - @b4moss/crudian/bun-sqlite
|
|
5
5
|
* - @b4moss/crudian/drizzle
|
|
6
6
|
* - @b4moss/crudian/prisma
|
|
7
|
+
* - @b4moss/crudian/libsql
|
|
7
8
|
*/
|
|
8
9
|
export { CrudianError, assertString } from "./errors.js";
|
|
9
10
|
export { WhereBuilder, where, isWhereBuilder, type Op, type CondNode, type GroupNode, type WhereNode, } from "./where.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,EACL,YAAY,EACZ,KAAK,EACL,cAAc,EACd,KAAK,EAAE,EACP,KAAK,QAAQ,EACb,KAAK,SAAS,EACd,KAAK,SAAS,GACf,MAAM,YAAY,CAAA;AACnB,YAAY,EACV,GAAG,EACH,YAAY,EACZ,UAAU,EACV,SAAS,EACT,WAAW,EACX,UAAU,EACV,WAAW,EACX,WAAW,EACX,cAAc,GACf,MAAM,YAAY,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type AsyncSqliteCrud } from "../sqlite/async-crud.js";
|
|
2
|
+
import type { Row } from "../types.js";
|
|
3
|
+
/** Result shape used by the libSQL executor bridge. */
|
|
4
|
+
export type LibsqlResultSet = {
|
|
5
|
+
rows: Array<Row | unknown>;
|
|
6
|
+
rowsAffected: number;
|
|
7
|
+
};
|
|
8
|
+
/** Minimal execute surface shared by Client and Transaction. */
|
|
9
|
+
export type LibsqlExecutor = {
|
|
10
|
+
execute(stmt: {
|
|
11
|
+
sql: string;
|
|
12
|
+
args?: unknown[];
|
|
13
|
+
} | string): Promise<LibsqlResultSet>;
|
|
14
|
+
};
|
|
15
|
+
/** Minimal surface of @libsql/client Client used by the adapter. */
|
|
16
|
+
export type LibsqlLikeClient = LibsqlExecutor & {
|
|
17
|
+
transaction(mode?: "write" | "read" | "deferred"): Promise<LibsqlLikeTransaction>;
|
|
18
|
+
};
|
|
19
|
+
export type LibsqlLikeTransaction = LibsqlExecutor & {
|
|
20
|
+
commit(): Promise<void>;
|
|
21
|
+
rollback(): Promise<void>;
|
|
22
|
+
close(): void;
|
|
23
|
+
};
|
|
24
|
+
export type LibsqlCrud = AsyncSqliteCrud<LibsqlLikeClient>;
|
|
25
|
+
/**
|
|
26
|
+
* Create a Crud bound to a libSQL client (`@libsql/client`).
|
|
27
|
+
* Methods are async. The injected client is exposed as `crud.db`.
|
|
28
|
+
* Auth / URL are configured on the caller-created client (e.g. from env).
|
|
29
|
+
*/
|
|
30
|
+
export declare function createCrud(client: LibsqlLikeClient): LibsqlCrud;
|
|
31
|
+
//# sourceMappingURL=crud.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crud.d.ts","sourceRoot":"","sources":["../../src/libsql/crud.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,eAAe,EACrB,MAAM,yBAAyB,CAAA;AAChC,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AAEtC,uDAAuD;AACvD,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,KAAK,CAAC,GAAG,GAAG,OAAO,CAAC,CAAA;IAC1B,YAAY,EAAE,MAAM,CAAA;CACrB,CAAA;AAED,gEAAgE;AAChE,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,CACL,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,EAAE,CAAA;KAAE,GAAG,MAAM,GAC/C,OAAO,CAAC,eAAe,CAAC,CAAA;CAC5B,CAAA;AAED,oEAAoE;AACpE,MAAM,MAAM,gBAAgB,GAAG,cAAc,GAAG;IAC9C,WAAW,CACT,IAAI,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,UAAU,GACnC,OAAO,CAAC,qBAAqB,CAAC,CAAA;CAClC,CAAA;AAED,MAAM,MAAM,qBAAqB,GAAG,cAAc,GAAG;IACnD,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IACvB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IACzB,KAAK,IAAI,IAAI,CAAA;CACd,CAAA;AAED,MAAM,MAAM,UAAU,GAAG,eAAe,CAAC,gBAAgB,CAAC,CAAA;AAa1D;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,UAAU,CA6C/D"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { CrudianError } from "../index.js";
|
|
2
|
+
import { createAsyncSqliteCrud, } from "../sqlite/async-crud.js";
|
|
3
|
+
function normalizeRow(row) {
|
|
4
|
+
if (row == null || typeof row !== "object") {
|
|
5
|
+
throw new CrudianError("expected row object");
|
|
6
|
+
}
|
|
7
|
+
const out = { ...row };
|
|
8
|
+
for (const [k, v] of Object.entries(out)) {
|
|
9
|
+
if (typeof v === "bigint")
|
|
10
|
+
out[k] = Number(v);
|
|
11
|
+
}
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Create a Crud bound to a libSQL client (`@libsql/client`).
|
|
16
|
+
* Methods are async. The injected client is exposed as `crud.db`.
|
|
17
|
+
* Auth / URL are configured on the caller-created client (e.g. from env).
|
|
18
|
+
*/
|
|
19
|
+
export function createCrud(client) {
|
|
20
|
+
if (client == null) {
|
|
21
|
+
throw new CrudianError("db is required");
|
|
22
|
+
}
|
|
23
|
+
if (typeof client !== "object" ||
|
|
24
|
+
typeof client.execute !== "function" ||
|
|
25
|
+
typeof client.transaction !== "function") {
|
|
26
|
+
throw new CrudianError("db must be a libSQL client");
|
|
27
|
+
}
|
|
28
|
+
let active = client;
|
|
29
|
+
return createAsyncSqliteCrud(client, {
|
|
30
|
+
async run(sql, args = []) {
|
|
31
|
+
const result = await active.execute({ sql, args });
|
|
32
|
+
return { changes: Number(result.rowsAffected ?? 0) };
|
|
33
|
+
},
|
|
34
|
+
async get(sql, args = []) {
|
|
35
|
+
const result = await active.execute({ sql, args });
|
|
36
|
+
const row = result.rows[0];
|
|
37
|
+
return row == null ? undefined : normalizeRow(row);
|
|
38
|
+
},
|
|
39
|
+
async all(sql, args = []) {
|
|
40
|
+
const result = await active.execute({ sql, args });
|
|
41
|
+
return result.rows.map((row) => normalizeRow(row));
|
|
42
|
+
},
|
|
43
|
+
async transaction(fn) {
|
|
44
|
+
const tx = await client.transaction("write");
|
|
45
|
+
const prev = active;
|
|
46
|
+
active = tx;
|
|
47
|
+
try {
|
|
48
|
+
const value = await fn();
|
|
49
|
+
await tx.commit();
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
await tx.rollback();
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
active = prev;
|
|
58
|
+
tx.close();
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* libSQL adapter for @b4moss/crudian.
|
|
3
|
+
* Import via: `import { createCrud } from "@b4moss/crudian/libsql"`
|
|
4
|
+
*/
|
|
5
|
+
export { createCrud, type LibsqlCrud, type LibsqlLikeClient, type LibsqlLikeTransaction, type LibsqlExecutor, type LibsqlResultSet, } from "./crud.js";
|
|
6
|
+
export type { Row, SearchResult, WhereInput, ReadQuery, SearchQuery, CountQuery, DeleteQuery, UpdateQuery, DuplicateQuery, } from "../types.js";
|
|
7
|
+
export { CrudianError, where, WhereBuilder, type Op, type CondNode, type GroupNode, type WhereNode, } from "../index.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/libsql/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EACL,UAAU,EACV,KAAK,UAAU,EACf,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,cAAc,EACnB,KAAK,eAAe,GACrB,MAAM,WAAW,CAAA;AAClB,YAAY,EACV,GAAG,EACH,YAAY,EACZ,UAAU,EACV,SAAS,EACT,WAAW,EACX,UAAU,EACV,WAAW,EACX,WAAW,EACX,cAAc,GACf,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,YAAY,EACZ,KAAK,EACL,YAAY,EACZ,KAAK,EAAE,EACP,KAAK,QAAQ,EACb,KAAK,SAAS,EACd,KAAK,SAAS,GACf,MAAM,aAAa,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@b4moss/crudian",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "CRUD abstraction for DDD repositories",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,6 +25,11 @@
|
|
|
25
25
|
"types": "./dist/prisma/index.d.ts",
|
|
26
26
|
"import": "./dist/prisma/index.js",
|
|
27
27
|
"default": "./dist/prisma/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./libsql": {
|
|
30
|
+
"types": "./dist/libsql/index.d.ts",
|
|
31
|
+
"import": "./dist/libsql/index.js",
|
|
32
|
+
"default": "./dist/libsql/index.js"
|
|
28
33
|
}
|
|
29
34
|
},
|
|
30
35
|
"files": [
|
|
@@ -36,21 +41,26 @@
|
|
|
36
41
|
"build": "tsc -p tsconfig.json",
|
|
37
42
|
"prepublishOnly": "npm run build",
|
|
38
43
|
"prisma:generate": "prisma generate --schema prisma/schema.prisma",
|
|
39
|
-
"test": "bun run test:bun-sqlite && bun run test:drizzle && bun run test:prisma",
|
|
44
|
+
"test": "bun run test:bun-sqlite && bun run test:drizzle && bun run test:prisma && bun run test:libsql",
|
|
40
45
|
"test:bun-sqlite": "bun test src/bun-sqlite",
|
|
41
46
|
"test:drizzle": "tsx --test src/drizzle/crud.test.ts",
|
|
42
|
-
"test:prisma": "bun run prisma:generate && tsx --test src/prisma/crud.test.ts"
|
|
47
|
+
"test:prisma": "bun run prisma:generate && tsx --test src/prisma/crud.test.ts",
|
|
48
|
+
"test:libsql": "tsx --test src/libsql/crud.test.ts"
|
|
43
49
|
},
|
|
44
50
|
"engines": {
|
|
45
51
|
"node": ">=24",
|
|
46
52
|
"bun": ">=1.0.0"
|
|
47
53
|
},
|
|
48
54
|
"peerDependencies": {
|
|
55
|
+
"@libsql/client": "^0.14.0 || ^0.15.0 || ^0.17.0",
|
|
49
56
|
"@prisma/client": "^6.0.0",
|
|
50
57
|
"better-sqlite3": "^11.0.0",
|
|
51
58
|
"drizzle-orm": "^0.39.0 || ^0.40.0 || ^0.41.0 || ^0.44.0"
|
|
52
59
|
},
|
|
53
60
|
"peerDependenciesMeta": {
|
|
61
|
+
"@libsql/client": {
|
|
62
|
+
"optional": true
|
|
63
|
+
},
|
|
54
64
|
"@prisma/client": {
|
|
55
65
|
"optional": true
|
|
56
66
|
},
|
|
@@ -62,9 +72,10 @@
|
|
|
62
72
|
}
|
|
63
73
|
},
|
|
64
74
|
"devDependencies": {
|
|
75
|
+
"@libsql/client": "^0.17.4",
|
|
65
76
|
"@prisma/client": "^6.14.0",
|
|
66
77
|
"@types/better-sqlite3": "^7.6.13",
|
|
67
|
-
"better-sqlite3": "^
|
|
78
|
+
"better-sqlite3": "^13",
|
|
68
79
|
"bun-types": "^1.2.0",
|
|
69
80
|
"drizzle-orm": "^0.44.4",
|
|
70
81
|
"prisma": "^6.14.0",
|
|
@@ -78,6 +89,7 @@
|
|
|
78
89
|
"bun",
|
|
79
90
|
"drizzle",
|
|
80
91
|
"prisma",
|
|
92
|
+
"libsql",
|
|
81
93
|
"ddd"
|
|
82
94
|
],
|
|
83
95
|
"repository": {
|