@cfast/db 0.2.0 → 0.4.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 +15 -15
- package/dist/index.d.ts +434 -26
- package/dist/index.js +351 -46
- package/llms.txt +342 -12
- package/package.json +7 -5
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ The two-phase design (inspect `.permissions`, then `.run()`) exists because perm
|
|
|
26
26
|
|
|
27
27
|
3. **Introspection** — Logging, debugging, and admin dashboards can inspect what an operation requires without executing it.
|
|
28
28
|
|
|
29
|
-
The cost is one extra level of indirection (`.run(
|
|
29
|
+
The cost is one extra level of indirection (`.run()` instead of `await db.query(...)`) and a slightly larger API surface. We think this is worth it because permission bugs are hard to detect and easy to ship.
|
|
30
30
|
|
|
31
31
|
### Why `Record<string, unknown>` for params instead of type-safe placeholders?
|
|
32
32
|
|
|
@@ -156,7 +156,7 @@ const allVisible = db.query(posts).findMany();
|
|
|
156
156
|
allVisible.permissions;
|
|
157
157
|
// → [{ action: "read", table: posts }]
|
|
158
158
|
|
|
159
|
-
await allVisible.run(
|
|
159
|
+
await allVisible.run();
|
|
160
160
|
// Anonymous user → SELECT * FROM posts WHERE published = 1
|
|
161
161
|
// Editor user → SELECT * FROM posts (no permission filter)
|
|
162
162
|
// Admin user → SELECT * FROM posts (manage grants have no filter)
|
|
@@ -206,7 +206,7 @@ const createPost = db.insert(posts).values({
|
|
|
206
206
|
createPost.permissions;
|
|
207
207
|
// → [{ action: "create", table: posts }]
|
|
208
208
|
|
|
209
|
-
await createPost.run(
|
|
209
|
+
await createPost.run();
|
|
210
210
|
// Checks: does this user's role have a "create" grant on posts?
|
|
211
211
|
// If yes → INSERT INTO posts ...
|
|
212
212
|
// If no → throws ForbiddenError
|
|
@@ -219,7 +219,7 @@ const createPost = db.insert(posts)
|
|
|
219
219
|
.values({ title: "Hello", authorId: currentUser.id })
|
|
220
220
|
.returning();
|
|
221
221
|
|
|
222
|
-
const inserted = await createPost.run(
|
|
222
|
+
const inserted = await createPost.run();
|
|
223
223
|
// inserted: the full inserted row
|
|
224
224
|
```
|
|
225
225
|
|
|
@@ -237,7 +237,7 @@ const publishPost = db.update(posts)
|
|
|
237
237
|
publishPost.permissions;
|
|
238
238
|
// → [{ action: "update", table: posts }]
|
|
239
239
|
|
|
240
|
-
await publishPost.run(
|
|
240
|
+
await publishPost.run();
|
|
241
241
|
```
|
|
242
242
|
|
|
243
243
|
**Row-level permission injection for updates:** If the user's "update" grant has a `where` clause (e.g., `where: (post, user) => eq(post.authorId, user.id)`), it's AND'd with the user-supplied condition:
|
|
@@ -261,7 +261,7 @@ const updated = await db.update(posts)
|
|
|
261
261
|
.set({ published: true })
|
|
262
262
|
.where(eq(posts.id, "abc-123"))
|
|
263
263
|
.returning()
|
|
264
|
-
.run(
|
|
264
|
+
.run();
|
|
265
265
|
```
|
|
266
266
|
|
|
267
267
|
---
|
|
@@ -275,7 +275,7 @@ const removePost = db.delete(posts)
|
|
|
275
275
|
removePost.permissions;
|
|
276
276
|
// → [{ action: "delete", table: posts }]
|
|
277
277
|
|
|
278
|
-
await removePost.run(
|
|
278
|
+
await removePost.run();
|
|
279
279
|
```
|
|
280
280
|
|
|
281
281
|
Same row-level WHERE injection as `update()`. Same silent-no-match behavior.
|
|
@@ -292,7 +292,7 @@ const op = db.unsafe().delete(posts).where(eq(posts.id, "abc-123"));
|
|
|
292
292
|
op.permissions;
|
|
293
293
|
// → [] (empty — no permissions required)
|
|
294
294
|
|
|
295
|
-
await op.run(
|
|
295
|
+
await op.run();
|
|
296
296
|
// Executes immediately, no permission check, no permission WHERE injection
|
|
297
297
|
```
|
|
298
298
|
|
|
@@ -329,7 +329,7 @@ const publishWorkflow = compose(
|
|
|
329
329
|
publishWorkflow.permissions;
|
|
330
330
|
// → [{ action: "update", table: posts }, { action: "create", table: auditLogs }]
|
|
331
331
|
|
|
332
|
-
await publishWorkflow.run(
|
|
332
|
+
await publishWorkflow.run();
|
|
333
333
|
```
|
|
334
334
|
|
|
335
335
|
| Parameter | Type | Description |
|
|
@@ -370,7 +370,7 @@ const batchOp = db.batch([
|
|
|
370
370
|
batchOp.permissions;
|
|
371
371
|
// → [{ action: "create", table: posts }, { action: "create", table: auditLogs }]
|
|
372
372
|
|
|
373
|
-
await batchOp.run(
|
|
373
|
+
await batchOp.run();
|
|
374
374
|
```
|
|
375
375
|
|
|
376
376
|
**Implementation detail:** `batch()` runs operations sequentially via their individual `.run()` methods, not via D1's native batch API. See [Design Decisions](#why-does-batch-run-operations-sequentially) for why.
|
|
@@ -422,7 +422,7 @@ The hash uses a fast 32-bit string hash (djb2 variant). This is not cryptographi
|
|
|
422
422
|
Every mutation builder receives an `onMutate` callback. After a successful insert/update/delete, it bumps the table's version counter. Any subsequent read generates a cache key with the new version, causing a cache miss.
|
|
423
423
|
|
|
424
424
|
```typescript
|
|
425
|
-
await db.insert(posts).values({ title: "New" }).run(
|
|
425
|
+
await db.insert(posts).values({ title: "New" }).run();
|
|
426
426
|
// → table version for "posts" incremented
|
|
427
427
|
// → all cached "posts" queries will miss on next read
|
|
428
428
|
```
|
|
@@ -536,7 +536,7 @@ export async function loader({ context }) {
|
|
|
536
536
|
});
|
|
537
537
|
|
|
538
538
|
// Read — permission filter applied automatically
|
|
539
|
-
const visiblePosts = await db.query(posts).findMany().run(
|
|
539
|
+
const visiblePosts = await db.query(posts).findMany().run();
|
|
540
540
|
|
|
541
541
|
// Inspect permissions without executing
|
|
542
542
|
const deleteOp = db.delete(posts).where(eq(posts.id, "abc"));
|
|
@@ -578,7 +578,7 @@ export async function action({ context, request }) {
|
|
|
578
578
|
// → [{ action: "update", table: posts }, { action: "create", table: auditLogs }]
|
|
579
579
|
|
|
580
580
|
// Execute — each sub-operation checks its own permissions
|
|
581
|
-
await publishWorkflow.run(
|
|
581
|
+
await publishWorkflow.run();
|
|
582
582
|
|
|
583
583
|
return { ok: true };
|
|
584
584
|
}
|
|
@@ -677,10 +677,10 @@ Use `db.unsafe()` when inserting into system tables (like `audit_logs`) that no
|
|
|
677
677
|
|
|
678
678
|
```typescript
|
|
679
679
|
// Good: audit log bypasses permission checks
|
|
680
|
-
await db.unsafe().insert(auditLogs).values({ ... }).run(
|
|
680
|
+
await db.unsafe().insert(auditLogs).values({ ... }).run();
|
|
681
681
|
|
|
682
682
|
// Bad: requires a "create" grant on audit_logs for the current user's role
|
|
683
|
-
await db.insert(auditLogs).values({ ... }).run(
|
|
683
|
+
await db.insert(auditLogs).values({ ... }).run();
|
|
684
684
|
```
|
|
685
685
|
|
|
686
686
|
`git grep '.unsafe()'` finds every permission bypass in your codebase.
|