@vultro/docstore-postgres 0.0.0-stage → 0.0.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.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Postgres 驱动入口。
3
+ *
4
+ * 驱动依赖安装在 `@vultro/docstore-postgres` 内,运行时包 / 测试不直接
5
+ * 声明 `pg`。这样只有真正选择 Postgres 文档存储的项目才会引入驱动。
6
+ */
7
+ export { Client } from "pg";
package/dist/driver.js ADDED
@@ -0,0 +1 @@
1
+ export { Client } from "pg";
@@ -0,0 +1,8 @@
1
+ import { type DocStoreBackend } from "@vultro/core/docstore";
2
+ /**
3
+ * Postgres 文档存储选择器(纯配置)。
4
+ *
5
+ * 实现由 `@vultro/docstore-postgres/runtime` 注册;连接由应用注入 `pg`
6
+ * client(Cloudflare 自定义入口、Bun、Node 都走同一条路径)。
7
+ */
8
+ export declare function pgStore(): DocStoreBackend;
package/dist/index.js ADDED
@@ -0,0 +1,30 @@
1
+ import { resolveDocStoreCreator, resolveDocStoreDatabaseCreator } from "@vultro/core/docstore";
2
+ // src/index.ts
3
+ /**
4
+ * Postgres 文档存储选择器(纯配置)。
5
+ *
6
+ * 实现由 `@vultro/docstore-postgres/runtime` 注册;连接由应用注入 `pg`
7
+ * client(Cloudflare 自定义入口、Bun、Node 都走同一条路径)。
8
+ */
9
+ function pgStore() {
10
+ return {
11
+ name: "postgres",
12
+ runtimes: [
13
+ "cloudflare",
14
+ "cloud",
15
+ "bun",
16
+ "node"
17
+ ],
18
+ module: "@vultro/docstore-postgres",
19
+ exportName: "pgStore",
20
+ create(runtime, context) {
21
+ return resolveDocStoreCreator("postgres")(runtime, context);
22
+ },
23
+ createDatabase(runtime, context) {
24
+ const create = resolveDocStoreDatabaseCreator("postgres");
25
+ if (!create) throw new Error("postgres docstore database adapter is not registered");
26
+ return create(runtime, context);
27
+ }
28
+ };
29
+ }
30
+ export { pgStore };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * ============================================================================
3
+ * Postgres 文档存储适配器
4
+ * ============================================================================
5
+ *
6
+ * 真正的 SQL 客户端由应用选择
7
+ * (例如 `pg` 或 `postgres`)。这里定义一个最小的异步 client 接口,并把
8
+ * D1 风格的 SQL 适配到 Postgres:
9
+ *
10
+ * - `?` 占位符 → `$1..$n`;
11
+ * - `INTEGER PRIMARY KEY AUTOINCREMENT` → `BIGSERIAL PRIMARY KEY`;
12
+ * - `REAL` → `DOUBLE PRECISION`;
13
+ * - `INSERT OR IGNORE/REPLACE` → `INSERT ... ON CONFLICT DO NOTHING`。
14
+ *
15
+ * 复用 `AsyncCachedDocumentStore` 的文档 / 搜索 / 向量索引逻辑,避免两套存储
16
+ * 语义分叉。应用只需提供 Postgres client:
17
+ *
18
+ * ```ts
19
+ * const runtime = createRuntime({
20
+ * ...
21
+ * postgres: { client: new Client({ connectionString: env.POSTGRES_URL }) },
22
+ * });
23
+ * ```
24
+ */
25
+ import type { D1DatabaseLike, D1PreparedStatementLike } from "@vultro/core/d1";
26
+ import { AsyncCachedDocumentStore } from "@vultro/core";
27
+ import type { SchemaInfo } from "@vultro/core/schema-info";
28
+ /** Postgres SQL client 最小接口;`pg` / `postgres` 都可适配。 */
29
+ export interface PostgresClientLike {
30
+ query<T = Record<string, unknown>>(text: string, values?: unknown[]): Promise<{
31
+ rows: T[];
32
+ rowCount?: number | null;
33
+ }>;
34
+ transaction?<T>(callback: (client: PostgresClientLike) => Promise<T>): Promise<T>;
35
+ }
36
+ /** 把 D1/SQLite 风格 SQL 转成 Postgres 可执行 SQL。 */
37
+ export declare function translatePostgresSql(text: string): string;
38
+ /** 把 Postgres client 包装成 D1DatabaseLike,供 AsyncCachedDocumentStore 复用。 */
39
+ export declare class PostgresD1Database implements D1DatabaseLike {
40
+ private readonly client;
41
+ constructor(client: PostgresClientLike);
42
+ prepare(sql: string): D1PreparedStatementLike;
43
+ rows<T>(sql: string, params: unknown[]): Promise<T[]>;
44
+ execute(sql: string, params: unknown[]): Promise<void>;
45
+ batch(statements: D1PreparedStatementLike[]): Promise<unknown>;
46
+ }
47
+ /** 创建 Postgres 文档存储。 */
48
+ export declare function createPostgresDocumentStore(schema: SchemaInfo, client: PostgresClientLike): AsyncCachedDocumentStore;
@@ -0,0 +1,104 @@
1
+ import { AsyncCachedDocumentStore } from "@vultro/core";
2
+ // src/postgres.ts
3
+ /** 把 D1/SQLite 风格 SQL 转成 Postgres 可执行 SQL。 */
4
+ function translatePostgresSql(text) {
5
+ const pragma = /^\s*PRAGMA\s+table_info\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)\s*;?\s*$/i.exec(text);
6
+ if (pragma) return `SELECT column_name AS name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = '${pragma[1]}'`;
7
+ let sql = text.replace(/INTEGER\s+PRIMARY\s+KEY\s+AUTOINCREMENT/gi, "BIGSERIAL PRIMARY KEY").replace(/\bINTEGER\b/g, "BIGINT").replace(/\bREAL\b/g, "DOUBLE PRECISION");
8
+ if (/^\s*INSERT\s+OR\s+(REPLACE|IGNORE)\s+INTO/i.test(sql)) {
9
+ sql = sql.replace(/^\s*INSERT\s+OR\s+(REPLACE|IGNORE)\s+INTO/i, "INSERT INTO");
10
+ if (!/\bON\s+CONFLICT\b/i.test(sql)) sql = `${sql.trimEnd()} ON CONFLICT DO NOTHING`;
11
+ }
12
+ let result = "";
13
+ let index = 0;
14
+ let inString = false;
15
+ for (let i = 0; i < sql.length; i++) {
16
+ const char = sql[i];
17
+ if (char === "'") {
18
+ if (inString && sql[i + 1] === "'") {
19
+ result += "''";
20
+ i++;
21
+ continue;
22
+ }
23
+ inString = !inString;
24
+ result += char;
25
+ continue;
26
+ }
27
+ if (!inString && char === "?") {
28
+ index += 1;
29
+ result += `$${index}`;
30
+ continue;
31
+ }
32
+ result += char;
33
+ }
34
+ return result;
35
+ }
36
+ var PostgresPreparedStatement = class PostgresPreparedStatement {
37
+ database;
38
+ sql;
39
+ params;
40
+ constructor(database, sql, params = []) {
41
+ this.database = database;
42
+ this.sql = sql;
43
+ this.params = params;
44
+ }
45
+ bind(...values) {
46
+ return new PostgresPreparedStatement(this.database, this.sql, values);
47
+ }
48
+ async first() {
49
+ return (await this.database.rows(this.sql, this.params))[0] ?? null;
50
+ }
51
+ async all() {
52
+ return { results: await this.database.rows(this.sql, this.params) };
53
+ }
54
+ async run() {
55
+ await this.database.execute(this.sql, this.params);
56
+ return { success: true };
57
+ }
58
+ /** batch 内部使用。 */
59
+ raw() {
60
+ return {
61
+ sql: this.sql,
62
+ params: this.params
63
+ };
64
+ }
65
+ };
66
+ /** 把 Postgres client 包装成 D1DatabaseLike,供 AsyncCachedDocumentStore 复用。 */
67
+ var PostgresD1Database = class {
68
+ client;
69
+ constructor(client) {
70
+ this.client = client;
71
+ }
72
+ prepare(sql) {
73
+ return new PostgresPreparedStatement(this, translatePostgresSql(sql));
74
+ }
75
+ async rows(sql, params) {
76
+ return (await this.client.query(sql, params)).rows ?? [];
77
+ }
78
+ async execute(sql, params) {
79
+ await this.client.query(sql, params);
80
+ }
81
+ async batch(statements) {
82
+ const run = async (client) => {
83
+ for (const statement of statements) {
84
+ const { sql, params } = statement.raw();
85
+ await client.query(sql, params);
86
+ }
87
+ };
88
+ if (this.client.transaction) return this.client.transaction(run);
89
+ await this.client.query("BEGIN");
90
+ try {
91
+ await run(this.client);
92
+ await this.client.query("COMMIT");
93
+ } catch (error) {
94
+ await this.client.query("ROLLBACK");
95
+ throw error;
96
+ }
97
+ return { success: true };
98
+ }
99
+ };
100
+ /** 创建 Postgres 文档存储。 */
101
+ function createPostgresDocumentStore(schema, client) {
102
+ return new AsyncCachedDocumentStore(schema, new PostgresD1Database(client));
103
+ }
104
+ export { PostgresD1Database, createPostgresDocumentStore, translatePostgresSql };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { registerDocStore, registerDocStoreDatabase } from "@vultro/core/docstore";
2
+ import { PostgresD1Database, createPostgresDocumentStore } from "./postgres.js";
3
+ // src/runtime.ts
4
+ /** Postgres 文档存储实现(pg client)。 */
5
+ registerDocStore("postgres", (_runtime, context) => {
6
+ if (!context.postgres) throw new Error("pgStore requires a Postgres client");
7
+ return createPostgresDocumentStore(context.schema, context.postgres);
8
+ });
9
+ /** Postgres client → D1DatabaseLike,供运行时内部服务复用。 */
10
+ registerDocStoreDatabase("postgres", (_runtime, context) => {
11
+ if (!context.postgres) throw new Error("pgStore requires a Postgres client");
12
+ return new PostgresD1Database(context.postgres);
13
+ });
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
2
  "name": "@vultro/docstore-postgres",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "description": "Postgres document store backend for Vultro",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js",
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./driver": {
13
+ "types": "./dist/driver.d.ts",
14
+ "import": "./dist/driver.js",
15
+ "default": "./dist/driver.js"
16
+ },
17
+ "./runtime": {
18
+ "types": "./dist/runtime.d.ts",
19
+ "import": "./dist/runtime.js",
20
+ "default": "./dist/runtime.js"
21
+ }
22
+ },
23
+ "dependencies": {
24
+ "@vultro/core": "0.0.1",
25
+ "pg": "^8.23.1"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "types": "./dist/index.d.ts"
31
+ }
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.