@cfast/db 0.4.0 → 0.4.1
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/dist/index.d.ts +23 -0
- package/dist/index.js +5 -1
- package/llms.txt +21 -0
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -502,6 +502,29 @@ type Db = {
|
|
|
502
502
|
tables?: string[];
|
|
503
503
|
}) => Promise<void>;
|
|
504
504
|
};
|
|
505
|
+
/**
|
|
506
|
+
* Clears the per-instance `with` lookup cache so that the next query
|
|
507
|
+
* re-runs every grant lookup function.
|
|
508
|
+
*
|
|
509
|
+
* In production this is rarely needed because each request gets a fresh
|
|
510
|
+
* `Db` via `createDb()`. In tests that reuse a single `Db` across grant
|
|
511
|
+
* mutations (e.g. inserting a new friendship and then querying recipes),
|
|
512
|
+
* call this after the mutation to avoid stale lookup results.
|
|
513
|
+
*
|
|
514
|
+
* For finer-grained control, wrap each logical request in
|
|
515
|
+
* {@link runWithLookupCache} instead -- that scopes the cache via
|
|
516
|
+
* `AsyncLocalStorage` so it is automatically discarded at scope exit.
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* ```ts
|
|
520
|
+
* const db = createDb({ ... });
|
|
521
|
+
* await db.query(recipes).findMany().run(); // populates lookup cache
|
|
522
|
+
* await db.insert(friendGrants).values({ ... }).run(); // adds new grant
|
|
523
|
+
* db.clearLookupCache(); // drop stale lookups
|
|
524
|
+
* await db.query(recipes).findMany().run(); // sees new grant
|
|
525
|
+
* ```
|
|
526
|
+
*/
|
|
527
|
+
clearLookupCache: () => void;
|
|
505
528
|
};
|
|
506
529
|
/**
|
|
507
530
|
* Builder for read queries on a single table.
|
package/dist/index.js
CHANGED
|
@@ -935,6 +935,9 @@ function buildDb(config, isUnsafe, lookupCache) {
|
|
|
935
935
|
}
|
|
936
936
|
}
|
|
937
937
|
}
|
|
938
|
+
},
|
|
939
|
+
clearLookupCache() {
|
|
940
|
+
lookupCache.clear();
|
|
938
941
|
}
|
|
939
942
|
};
|
|
940
943
|
return db;
|
|
@@ -1059,7 +1062,8 @@ function createTrackingDb(real, perms) {
|
|
|
1059
1062
|
}
|
|
1060
1063
|
return createSentinel();
|
|
1061
1064
|
},
|
|
1062
|
-
cache: real.cache
|
|
1065
|
+
cache: real.cache,
|
|
1066
|
+
clearLookupCache: () => real.clearLookupCache()
|
|
1063
1067
|
};
|
|
1064
1068
|
return trackingDb;
|
|
1065
1069
|
}
|
package/llms.txt
CHANGED
|
@@ -532,6 +532,27 @@ prefer the scoped cache over the `Db`-owned fallback. Pass an explicit
|
|
|
532
532
|
`LookupCache` instance as the second argument if you want to share a cache
|
|
533
533
|
across multiple sibling `runWithLookupCache` calls.
|
|
534
534
|
|
|
535
|
+
### db.clearLookupCache(): void
|
|
536
|
+
|
|
537
|
+
Clears the per-instance cross-table `with` lookup cache on the `Db` instance. This is the imperative counterpart to `runWithLookupCache()` -- instead of scoping a fresh cache via ALS, it resets the instance-owned cache in place.
|
|
538
|
+
|
|
539
|
+
The primary use case is tests that reuse a single `Db` across grant inserts. After modifying grants, call `db.clearLookupCache()` so subsequent queries re-run `with` lookups against the updated data:
|
|
540
|
+
|
|
541
|
+
```typescript
|
|
542
|
+
const db = createDb({ d1, schema, grants, user, cache: false });
|
|
543
|
+
|
|
544
|
+
// Insert a new friend-grant.
|
|
545
|
+
await db.unsafe().insert(friendGrants).values({ grantee: "u1", target: "u2" }).run();
|
|
546
|
+
|
|
547
|
+
// Without this, the next query would use the stale cached lookup.
|
|
548
|
+
db.clearLookupCache();
|
|
549
|
+
|
|
550
|
+
const visible = await db.query(recipes).findMany().run();
|
|
551
|
+
expect(visible).toContainEqual(expect.objectContaining({ authorId: "u2" }));
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
Prefer `runWithLookupCache()` in production code (ALS-scoped, automatic cleanup). Use `clearLookupCache()` when you need a quick manual reset in tests or long-lived workers that don't use the ALS pattern.
|
|
555
|
+
|
|
535
556
|
## Integration
|
|
536
557
|
|
|
537
558
|
- **@cfast/permissions** -- `grants` come from `resolveGrants(permissions, user.roles)`. Permission WHERE clauses are defined via `grant()` in your permissions config.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfast/db",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Permission-aware Drizzle queries for Cloudflare D1",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cfast",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@cfast/permissions": ">=0.3.0 <0.
|
|
37
|
+
"@cfast/permissions": ">=0.3.0 <0.6.0",
|
|
38
38
|
"drizzle-orm": ">=0.35"
|
|
39
39
|
},
|
|
40
40
|
"peerDependenciesMeta": {
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"tsup": "^8",
|
|
52
52
|
"typescript": "^5.7",
|
|
53
53
|
"vitest": "^4.1.0",
|
|
54
|
-
"@cfast/permissions": "0.
|
|
54
|
+
"@cfast/permissions": "0.5.1"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "tsup src/index.ts --format esm --dts",
|