@junejs/db 0.0.33-dev.0 → 0.0.33-dev.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 +2 -0
- package/dist/index.js +2 -0
- package/dist/scope.d.ts +18 -0
- package/dist/scope.js +59 -0
- package/package.json +11 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { RequestScope, blob, currentLocale, db, ensureScope, kv, registerSqlTagger, requestLocal, runInScope, setRequestLocale } from "./scope.js";
|
|
2
|
+
export { type RequestScope, blob, currentLocale, db, ensureScope, kv, registerSqlTagger, requestLocal, runInScope, setRequestLocale };
|
package/dist/index.js
ADDED
package/dist/scope.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { JuneBlob, JuneDb, JuneKv, Resources } from "@junejs/core/resources";
|
|
2
|
+
//#region src/scope.d.ts
|
|
3
|
+
type RequestScope = {
|
|
4
|
+
resources: Resources;
|
|
5
|
+
locals?: Map<symbol, unknown>;
|
|
6
|
+
locale?: string;
|
|
7
|
+
};
|
|
8
|
+
declare function ensureScope(): Promise<void>;
|
|
9
|
+
declare function runInScope<T>(scope: RequestScope, fn: () => T): T;
|
|
10
|
+
declare function requestLocal<T>(key: symbol, make: () => T): T;
|
|
11
|
+
declare function setRequestLocale(locale: string): void;
|
|
12
|
+
declare function currentLocale(): string | undefined;
|
|
13
|
+
declare function registerSqlTagger(tag: (sql: string) => void): void;
|
|
14
|
+
declare const db: JuneDb;
|
|
15
|
+
declare const kv: JuneKv;
|
|
16
|
+
declare const blob: JuneBlob;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { RequestScope, blob, currentLocale, db, ensureScope, kv, registerSqlTagger, requestLocal, runInScope, setRequestLocale };
|
package/dist/scope.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/scope.ts
|
|
2
|
+
let als = null;
|
|
3
|
+
let ensuring = null;
|
|
4
|
+
async function ensureScope() {
|
|
5
|
+
if (als) return;
|
|
6
|
+
ensuring ??= (async () => {
|
|
7
|
+
try {
|
|
8
|
+
als = new (await (import("node:async_hooks"))).AsyncLocalStorage();
|
|
9
|
+
} catch {}
|
|
10
|
+
})();
|
|
11
|
+
await ensuring;
|
|
12
|
+
}
|
|
13
|
+
function runInScope(scope, fn) {
|
|
14
|
+
return als ? als.run(scope, fn) : fn();
|
|
15
|
+
}
|
|
16
|
+
function requestLocal(key, make) {
|
|
17
|
+
const store = als?.getStore();
|
|
18
|
+
if (!store) throw new Error("June: request-scoped state was used outside a request scope. It is only available while a request is being handled (a route loader/view or an action).");
|
|
19
|
+
const locals = store.locals ??= /* @__PURE__ */ new Map();
|
|
20
|
+
if (!locals.has(key)) locals.set(key, make());
|
|
21
|
+
return locals.get(key);
|
|
22
|
+
}
|
|
23
|
+
function setRequestLocale(locale) {
|
|
24
|
+
const store = als?.getStore();
|
|
25
|
+
if (store) store.locale = locale;
|
|
26
|
+
}
|
|
27
|
+
function currentLocale() {
|
|
28
|
+
return als?.getStore()?.locale;
|
|
29
|
+
}
|
|
30
|
+
function pick(name) {
|
|
31
|
+
const store = als?.getStore();
|
|
32
|
+
if (!store) throw new Error(`June: \`${name}\` was used outside a request scope. Resources are only available while a request is being handled (a route loader/view or an action).`);
|
|
33
|
+
const resource = store.resources[name];
|
|
34
|
+
if (!resource) throw new Error(`June: no \`${name}\` resource is declared. Add \`resources: { ${name}: … }\` to june.config.ts (e.g. \`db: sqlite()\`).`);
|
|
35
|
+
return resource;
|
|
36
|
+
}
|
|
37
|
+
function ambient(name) {
|
|
38
|
+
return new Proxy({}, { get(_t, prop) {
|
|
39
|
+
const resource = pick(name);
|
|
40
|
+
const value = resource[prop];
|
|
41
|
+
return typeof value === "function" ? value.bind(resource) : value;
|
|
42
|
+
} });
|
|
43
|
+
}
|
|
44
|
+
let sqlTagger = null;
|
|
45
|
+
function registerSqlTagger(tag) {
|
|
46
|
+
sqlTagger = tag;
|
|
47
|
+
}
|
|
48
|
+
const db = new Proxy({}, { get(_t, prop) {
|
|
49
|
+
const handle = pick("db");
|
|
50
|
+
const value = handle[prop];
|
|
51
|
+
if (typeof value !== "function") return value;
|
|
52
|
+
const fn = value.bind(handle);
|
|
53
|
+
if (sqlTagger && (prop === "query" || prop === "get" || prop === "run")) return (sql, params) => (sqlTagger(sql), fn(sql, params));
|
|
54
|
+
return fn;
|
|
55
|
+
} });
|
|
56
|
+
const kv = ambient("kv");
|
|
57
|
+
const blob = ambient("blob");
|
|
58
|
+
//#endregion
|
|
59
|
+
export { blob, currentLocale, db, ensureScope, kv, registerSqlTagger, requestLocal, runInScope, setRequestLocale };
|
package/package.json
CHANGED
|
@@ -1,22 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@junejs/db",
|
|
3
|
-
"version": "0.0.33-dev.
|
|
3
|
+
"version": "0.0.33-dev.1",
|
|
4
4
|
"description": "June — the agent-native React framework. Ambient data resources (db/kv/blob).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://june.build",
|
|
8
8
|
"exports": {
|
|
9
|
-
".":
|
|
9
|
+
".": {
|
|
10
|
+
"source": "./src/index.ts",
|
|
11
|
+
"bun": "./src/index.ts",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
10
15
|
},
|
|
11
16
|
"files": [
|
|
17
|
+
"dist",
|
|
12
18
|
"src"
|
|
13
19
|
],
|
|
14
20
|
"scripts": {
|
|
15
21
|
"test": "bun test",
|
|
16
|
-
"typecheck": "tsc --noEmit"
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"build": "tsdown"
|
|
17
24
|
},
|
|
18
25
|
"dependencies": {
|
|
19
|
-
"@junejs/core": ">=0.1.0-dev.
|
|
26
|
+
"@junejs/core": ">=0.1.0-dev.4"
|
|
20
27
|
},
|
|
21
28
|
"repository": {
|
|
22
29
|
"type": "git",
|