@tejika/db 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.
- package/LICENSE +21 -0
- package/README.md +27 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/open.d.ts +17 -0
- package/lib/open.js +46 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Paul Le Cam
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @tejika/db
|
|
2
|
+
|
|
3
|
+
Open local [hozon](https://github.com/TairuFramework/hozon) databases for CLI
|
|
4
|
+
tools and daemons. Node only: it uses `node:sqlite` through
|
|
5
|
+
`@hozon/node-sqlite`.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @tejika/db
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
- `openLocalDatabase` — resolve an app's database file, create its parent
|
|
12
|
+
directory, register the given stores and run their migrations eagerly, so
|
|
13
|
+
version and migration errors surface at open. On failure the handle is closed
|
|
14
|
+
and the original error is rethrown.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { logStoreDefinition } from '@hozon/store-log'
|
|
18
|
+
import { openLocalDatabase } from '@tejika/db'
|
|
19
|
+
|
|
20
|
+
const db = await openLocalDatabase({ app: 'myapp', stores: [logStoreDefinition] })
|
|
21
|
+
await db.close()
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The file is `getDatabasePath(app, name)` from `@tejika/env` unless `path` is given
|
|
25
|
+
(`':memory:'` is supported); `<APP>_DATABASE_PATH` overrides the default location.
|
|
26
|
+
|
|
27
|
+
Node only (`node:sqlite`): Electron main processes must mark `node:sqlite` external.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type OpenLocalDatabaseParams, openLocalDatabase } from './open.js';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { openLocalDatabase } from './open.js';
|
package/lib/open.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { HozonDB, type HozonDBParams, type StoreDefinition } from '@hozon/db';
|
|
2
|
+
import { type SQLitePragmas } from '@hozon/node-sqlite';
|
|
3
|
+
export type OpenLocalDatabaseParams = {
|
|
4
|
+
app: string;
|
|
5
|
+
name?: string;
|
|
6
|
+
path?: string;
|
|
7
|
+
stores: Array<StoreDefinition<unknown, unknown>>;
|
|
8
|
+
tablePrefix?: string;
|
|
9
|
+
logger?: HozonDBParams['logger'];
|
|
10
|
+
pragmas?: SQLitePragmas;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Open a local SQLite database for an app, register the given stores and run
|
|
14
|
+
* their migrations eagerly so version and migration errors surface at open.
|
|
15
|
+
* On failure the handle is closed before the original error is rethrown.
|
|
16
|
+
*/
|
|
17
|
+
export declare function openLocalDatabase(params: OpenLocalDatabaseParams): Promise<HozonDB>;
|
package/lib/open.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { mkdir } from 'node:fs/promises';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { HozonDB } from '@hozon/db';
|
|
4
|
+
import { NodeSQLiteAdapter } from '@hozon/node-sqlite';
|
|
5
|
+
import { getDatabasePath } from '@tejika/env';
|
|
6
|
+
/**
|
|
7
|
+
* Open a local SQLite database for an app, register the given stores and run
|
|
8
|
+
* their migrations eagerly so version and migration errors surface at open.
|
|
9
|
+
* On failure the handle is closed before the original error is rethrown.
|
|
10
|
+
*/ export async function openLocalDatabase(params) {
|
|
11
|
+
const path = params.path ?? getDatabasePath(params.app, params.name);
|
|
12
|
+
if (path !== ':memory:') {
|
|
13
|
+
await mkdir(dirname(path), {
|
|
14
|
+
recursive: true
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const adapter = new NodeSQLiteAdapter({
|
|
18
|
+
database: path,
|
|
19
|
+
pragmas: params.pragmas
|
|
20
|
+
});
|
|
21
|
+
let db;
|
|
22
|
+
try {
|
|
23
|
+
const dbParams = {
|
|
24
|
+
adapter
|
|
25
|
+
};
|
|
26
|
+
if (params.logger != null) {
|
|
27
|
+
dbParams.logger = params.logger;
|
|
28
|
+
}
|
|
29
|
+
if (params.tablePrefix != null) {
|
|
30
|
+
dbParams.tablePrefix = params.tablePrefix;
|
|
31
|
+
}
|
|
32
|
+
db = new HozonDB(dbParams);
|
|
33
|
+
for (const store of params.stores){
|
|
34
|
+
db.register(store);
|
|
35
|
+
}
|
|
36
|
+
await db.migrate();
|
|
37
|
+
return db;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
try {
|
|
40
|
+
await (db ?? adapter).close();
|
|
41
|
+
} catch {
|
|
42
|
+
// Swallow close failures: the original error is what the caller needs.
|
|
43
|
+
}
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tejika/db",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Open local hozon databases for CLI tools and daemons",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"database",
|
|
7
|
+
"sqlite",
|
|
8
|
+
"hozon",
|
|
9
|
+
"local"
|
|
10
|
+
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/TairuFramework/tejika.git",
|
|
14
|
+
"directory": "packages/db"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"type": "module",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./lib/index.d.ts",
|
|
22
|
+
"default": "./lib/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"main": "lib/index.js",
|
|
26
|
+
"types": "lib/index.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
"lib/*",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@hozon/db": "^0.1.0",
|
|
33
|
+
"@hozon/node-sqlite": "^0.1.0",
|
|
34
|
+
"@tejika/env": "^0.5.3"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@hozon/store-log": "^0.1.0",
|
|
38
|
+
"@hozon/store-telemetry": "^0.1.0",
|
|
39
|
+
"@types/node": "^26.6.4"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=24"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
|
|
49
|
+
"build:clean": "del-cli lib",
|
|
50
|
+
"build:js": "del-cli \"lib/**/*.js\" && swc src -d ./lib --config-file ../../node_modules/@kigu/dev/swc.json --strip-leading-paths",
|
|
51
|
+
"build:types": "del-cli \"lib/**/*.d.ts\" \"lib/**/*.d.ts.map\" && tsc --emitDeclarationOnly --skipLibCheck",
|
|
52
|
+
"build:types:ci": "tsc --emitDeclarationOnly --skipLibCheck --declarationMap false",
|
|
53
|
+
"test": "pnpm run test:types && pnpm run test:unit",
|
|
54
|
+
"test:types": "tsc --noEmit --skipLibCheck -p tsconfig.test.json",
|
|
55
|
+
"test:unit": "vitest run"
|
|
56
|
+
}
|
|
57
|
+
}
|