@kybernesis/brain-storage-sqlite 0.1.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.
@@ -0,0 +1,12 @@
1
+ import Database from 'better-sqlite3';
2
+ import type { TenantContext } from '@kybernesis/brain-contracts';
3
+ export type DbKind = 'timeline' | 'entityGraph' | 'vectors' | 'sleep' | 'idempotency';
4
+ /**
5
+ * Lazy-open a better-sqlite3 connection for the given tenant + db kind.
6
+ * Pool keyed by `${slug}:${kind}`. Sets WAL + synchronous=NORMAL + foreign_keys=ON.
7
+ * Schema init is the caller's responsibility (each module ensures its own tables).
8
+ */
9
+ export declare function getDb(t: TenantContext, kind: DbKind): Database.Database;
10
+ export declare function closeWorkspace(slug: string): void;
11
+ export declare function closeAll(): void;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAGtC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAEjE,MAAM,MAAM,MAAM,GAAG,UAAU,GAAG,aAAa,GAAG,SAAS,GAAG,OAAO,GAAG,aAAa,CAAC;AAgBtF;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,GAAG,QAAQ,CAAC,QAAQ,CAevE;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAOjD;AAED,wBAAgB,QAAQ,IAAI,IAAI,CAK/B"}
package/dist/index.js ADDED
@@ -0,0 +1,56 @@
1
+ import Database from 'better-sqlite3';
2
+ import { mkdirSync } from 'node:fs';
3
+ import { dirname } from 'node:path';
4
+ const pool = new Map();
5
+ function resolveDbPath(t, kind) {
6
+ switch (kind) {
7
+ case 'timeline': return t.paths.timelineDbPath;
8
+ case 'entityGraph': return t.paths.entityGraphDbPath;
9
+ case 'vectors': return t.paths.vectorsDbPath;
10
+ case 'sleep': return t.paths.sleepDbPath;
11
+ case 'idempotency':
12
+ if (!t.paths.idempotencyDbPath)
13
+ throw new Error(`TenantContext '${t.slug}' has no idempotencyDbPath`);
14
+ return t.paths.idempotencyDbPath;
15
+ }
16
+ }
17
+ /**
18
+ * Lazy-open a better-sqlite3 connection for the given tenant + db kind.
19
+ * Pool keyed by `${slug}:${kind}`. Sets WAL + synchronous=NORMAL + foreign_keys=ON.
20
+ * Schema init is the caller's responsibility (each module ensures its own tables).
21
+ */
22
+ export function getDb(t, kind) {
23
+ const key = `${t.slug}:${kind}`;
24
+ const existing = pool.get(key);
25
+ if (existing)
26
+ return existing;
27
+ const p = resolveDbPath(t, kind);
28
+ mkdirSync(dirname(p), { recursive: true });
29
+ const db = new Database(p);
30
+ db.pragma('journal_mode = WAL');
31
+ db.pragma('synchronous = NORMAL');
32
+ db.pragma('foreign_keys = ON');
33
+ pool.set(key, db);
34
+ return db;
35
+ }
36
+ export function closeWorkspace(slug) {
37
+ for (const [key, db] of pool) {
38
+ if (key.startsWith(`${slug}:`)) {
39
+ try {
40
+ db.close();
41
+ }
42
+ catch { /* ignore */ }
43
+ pool.delete(key);
44
+ }
45
+ }
46
+ }
47
+ export function closeAll() {
48
+ for (const [, db] of pool) {
49
+ try {
50
+ db.close();
51
+ }
52
+ catch { /* ignore */ }
53
+ }
54
+ pool.clear();
55
+ }
56
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAKpC,MAAM,IAAI,GAAG,IAAI,GAAG,EAA6B,CAAC;AAElD,SAAS,aAAa,CAAC,CAAgB,EAAE,IAA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,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,KAAK,MAAM,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;QAC7B,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC;YAC/B,IAAI,CAAC;gBAAC,EAAE,CAAC,KAAK,EAAE,CAAC;YAAC,CAAC;YAAC,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC;YAC1C,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;IACH,CAAC;AACH,CAAC;AAED,MAAM,UAAU,QAAQ;IACtB,KAAK,MAAM,CAAC,EAAE,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;QAC1B,IAAI,CAAC;YAAC,EAAE,CAAC,KAAK,EAAE,CAAC;QAAC,CAAC;QAAC,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC;IAC5C,CAAC;IACD,IAAI,CAAC,KAAK,EAAE,CAAC;AACf,CAAC"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@kybernesis/brain-storage-sqlite",
3
+ "version": "0.1.0",
4
+ "description": "better-sqlite3 StructuredStore implementation for brain-core",
5
+ "license": "MIT",
6
+ "author": "David Cruwys (AppyDave)",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/KybernesisAI/cortex.git",
10
+ "directory": "packages/brain-storage-sqlite"
11
+ },
12
+ "homepage": "https://github.com/KybernesisAI/cortex/tree/main/packages/brain-storage-sqlite#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/KybernesisAI/cortex/issues"
15
+ },
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "README.md"
28
+ ],
29
+ "dependencies": {
30
+ "@kybernesis/brain-contracts": "0.1.0"
31
+ },
32
+ "peerDependencies": {
33
+ "better-sqlite3": ">=9.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/better-sqlite3": "^7.6.0",
37
+ "better-sqlite3": "^12.0.0",
38
+ "@kybernesis/brain-testkit": "0.1.0"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "scripts": {
44
+ "build": "tsc -b",
45
+ "clean": "tsc -b --clean",
46
+ "typecheck": "tsc -b"
47
+ }
48
+ }