@cordis-web/plugin-db 0.0.0-stage → 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,27 @@
1
+ import { DatabaseSync } from 'node:sqlite';
2
+ import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
3
+ import { Service, type Context } from 'cordis';
4
+ import type { DbConfig } from '../shared/index.js';
5
+ /**
6
+ * 数据库服务:基于 node:sqlite + drizzle-orm(sqlite-proxy 适配)。
7
+ * 业务插件通过 ctx.db.db 使用 drizzle 查询,通过 ctx.db.exec() 执行 DDL。
8
+ */
9
+ export declare class DbService extends Service {
10
+ connection: DatabaseSync;
11
+ db: SqliteRemoteDatabase;
12
+ constructor(ctx: Context, config?: DbConfig);
13
+ /** 将 drizzle sqlite-proxy 调用适配到 node:sqlite */
14
+ private createProxy;
15
+ /** 执行原生 SQL(用于 CREATE TABLE 等 DDL) */
16
+ exec(sql: string): void;
17
+ }
18
+ export declare const name = "db";
19
+ /** 无需注入其他服务 */
20
+ export declare const inject: [];
21
+ export declare function apply(ctx: Context, config?: DbConfig): void;
22
+ declare module 'cordis' {
23
+ interface Context {
24
+ db: DbService;
25
+ }
26
+ }
27
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,OAAO,KAAK,EAAuB,oBAAoB,EAAE,MAAM,0BAA0B,CAAA;AACzF,OAAO,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,QAAQ,CAAA;AAC9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAElD;;;GAGG;AACH,qBAAa,SAAU,SAAQ,OAAO;IACpC,UAAU,EAAG,YAAY,CAAA;IACzB,EAAE,EAAG,oBAAoB,CAAA;IAEzB,YAAY,GAAG,EAAE,OAAO,EAAE,MAAM,GAAE,QAAa,EAS9C;IAED,+CAA+C;IAC/C,OAAO,CAAC,WAAW;IAiBnB,sCAAsC;IACtC,IAAI,CAAC,GAAG,EAAE,MAAM,QAEf;CACF;AAED,eAAO,MAAM,IAAI,OAAO,CAAA;AAExB,eAAe;AACf,eAAO,MAAM,MAAM,EAAE,EAAO,CAAA;AAE5B,wBAAgB,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,GAAE,QAAa,QAExD;AAED,OAAO,QAAQ,QAAQ,CAAC;IACtB,UAAU,OAAO;QACf,EAAE,EAAE,SAAS,CAAA;KACd;CACF"}
@@ -0,0 +1,50 @@
1
+ import { mkdirSync } from 'node:fs';
2
+ import { dirname } from 'node:path';
3
+ import { DatabaseSync } from 'node:sqlite';
4
+ import { drizzle } from 'drizzle-orm/sqlite-proxy';
5
+ import { Service } from 'cordis';
6
+ /**
7
+ * 数据库服务:基于 node:sqlite + drizzle-orm(sqlite-proxy 适配)。
8
+ * 业务插件通过 ctx.db.db 使用 drizzle 查询,通过 ctx.db.exec() 执行 DDL。
9
+ */
10
+ export class DbService extends Service {
11
+ connection;
12
+ db;
13
+ constructor(ctx, config = {}) {
14
+ super(ctx, 'db');
15
+ const path = config.path ?? process.env.CORDIS_WEB_DB ?? 'data/app.db';
16
+ if (path !== ':memory:') {
17
+ mkdirSync(dirname(path), { recursive: true });
18
+ }
19
+ this.connection = new DatabaseSync(path);
20
+ this.connection.exec('PRAGMA foreign_keys = ON');
21
+ this.db = drizzle(this.createProxy());
22
+ }
23
+ /** 将 drizzle sqlite-proxy 调用适配到 node:sqlite */
24
+ createProxy() {
25
+ const connection = this.connection;
26
+ return async (query, params, method) => {
27
+ const stmt = connection.prepare(query);
28
+ if (method === 'run') {
29
+ stmt.run(...params);
30
+ return { rows: [] };
31
+ }
32
+ const rows = method === 'get'
33
+ ? [stmt.get(...params)].filter(Boolean)
34
+ : stmt.all(...params);
35
+ const columns = stmt.columns().map((column) => column.name);
36
+ return { rows: rows.map((row) => columns.map((column) => row[column])) };
37
+ };
38
+ }
39
+ /** 执行原生 SQL(用于 CREATE TABLE 等 DDL) */
40
+ exec(sql) {
41
+ this.connection.exec(sql);
42
+ }
43
+ }
44
+ export const name = 'db';
45
+ /** 无需注入其他服务 */
46
+ export const inject = [];
47
+ export function apply(ctx, config = {}) {
48
+ ctx.use(DbService, config);
49
+ }
50
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAElD,OAAO,EAAE,OAAO,EAAgB,MAAM,QAAQ,CAAA;AAG9C;;;GAGG;AACH,MAAM,OAAO,SAAU,SAAQ,OAAO;IACpC,UAAU,CAAe;IACzB,EAAE,CAAuB;IAEzB,YAAY,GAAY,EAAE,MAAM,GAAa,EAAE;QAC7C,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;QAChB,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,aAAa,IAAI,aAAa,CAAA;QACtE,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;YACxB,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;QAC/C,CAAC;QACD,IAAI,CAAC,UAAU,GAAG,IAAI,YAAY,CAAC,IAAI,CAAC,CAAA;QACxC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAA;QAChD,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,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AAE;IACvD,GAAG,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAA;AAC5B,CAAC"}
@@ -0,0 +1,9 @@
1
+ export interface DbConfig {
2
+ /**
3
+ * SQLite 数据库文件路径。
4
+ * - ':memory:' 使用内存库
5
+ * - 默认读取环境变量 CORDIS_WEB_DB,否则为 data/app.db
6
+ */
7
+ path?: string;
8
+ }
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/shared/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,QAAQ;IACvB;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;CACd"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/shared/index.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "@cordis-web/plugin-db",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "exports": {
9
+ "./shared": {
10
+ "types": "./dist/shared/index.d.ts",
11
+ "default": "./dist/shared/index.js"
12
+ },
13
+ "./server": {
14
+ "types": "./dist/server/index.d.ts",
15
+ "default": "./dist/server/index.js"
16
+ }
17
+ },
18
+ "scripts": {
19
+ "build": "tsc -b",
20
+ "dev": "tsc -b --watch"
21
+ },
22
+ "dependencies": {
23
+ "cordis": "^4.0.0-rc.13",
24
+ "drizzle-orm": "^0.45.4"
25
+ }
26
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.