@adep/runtime 0.3.12 → 0.3.14
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.
|
@@ -7,9 +7,10 @@
|
|
|
7
7
|
* 三处已知差异(任务单):
|
|
8
8
|
* 1. 占位符:SQLite `?` vs PG `$1`;
|
|
9
9
|
* 2. 自增主键返回:SQLite 经 last_insert_rowid(驱动层取回,无需 SQL 尾巴)vs PG `RETURNING id`;
|
|
10
|
-
* 3. Upsert 语法:SQLite `ON CONFLICT
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* 3. Upsert 语法:SQLite 与 PG 的 `INSERT ... ON CONFLICT (cols) DO UPDATE SET ...` 同形
|
|
11
|
+
* (值全部参数绑定后,两方言逐字一致);冲突子句统一走 `upsertConflictClause` 缝。
|
|
12
|
+
* PG 运行时驱动属 M6,本文件只把差异**文字**留位(`lastInsertIdClause`),
|
|
13
|
+
* 不实现 PG 连接;该缝当前不被公共 API 调用,由单测直接断言方言缝,防止死代码漂移。
|
|
13
14
|
*/
|
|
14
15
|
export interface QueryDialect {
|
|
15
16
|
readonly name: 'sqlite' | 'postgres';
|
|
@@ -19,8 +20,12 @@ export interface QueryDialect {
|
|
|
19
20
|
quote(identifier: string): string;
|
|
20
21
|
/** INSERT 后取回自增主键的 SQL 尾巴(SQLite 空串 + 驱动 lastInsertRowid;PG `RETURNING id`)。 */
|
|
21
22
|
lastInsertIdClause(): string;
|
|
22
|
-
/**
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Upsert 冲突子句(DB-010 起 builder 有公开 `upsert()` 方法)。
|
|
25
|
+
* `doUpdateSet` 为编译好的 `DO UPDATE SET` 赋值串(null/undefined = DO NOTHING)。
|
|
26
|
+
* 值已在 builder 侧参数绑定,本层只拼列名与关键字(标识符转义同 quote)。
|
|
27
|
+
*/
|
|
28
|
+
upsertConflictClause(columns: readonly string[], doUpdateSet?: string | null): string;
|
|
24
29
|
}
|
|
25
30
|
/** SQLite 方言(M1 唯一运行时方言,DB-000 硬约束 1)。 */
|
|
26
31
|
export declare const sqliteDialect: QueryDialect;
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* 4. 单语句(由编译形状天然保证:永远只产出一条语句);
|
|
11
11
|
* 5. 禁跨库 / 禁系统表 / 禁 ATTACH(`query` 逃生口由 guards 承担)。
|
|
12
12
|
*/
|
|
13
|
-
import type { CloudDbTable, SqlOperator, SqlValue } from '@adep/types';
|
|
13
|
+
import type { CloudDbTable, SqlOperator, SqlValue, UpsertOptions } from '@adep/types';
|
|
14
14
|
import type { ProjectDriver } from '../../db/project-driver';
|
|
15
15
|
import type { QueryDialect } from './dialect';
|
|
16
16
|
export interface WhereClause {
|
|
@@ -40,6 +40,19 @@ export declare function compileSelect(state: QueryState, dialect: QueryDialect):
|
|
|
40
40
|
export declare function compileCount(state: QueryState, dialect: QueryDialect): CompiledQuery;
|
|
41
41
|
export declare function compileInsert(table: string, row: Record<string, SqlValue>, dialect: QueryDialect): CompiledQuery;
|
|
42
42
|
export declare function compileInsertMany(table: string, rows: ReadonlyArray<Record<string, SqlValue>>, dialect: QueryDialect): CompiledQuery;
|
|
43
|
+
export interface UpsertPlan {
|
|
44
|
+
target: readonly string[];
|
|
45
|
+
/** 冲突时的更新列(值取自 row);空数组 = DO NOTHING。 */
|
|
46
|
+
update: readonly string[];
|
|
47
|
+
}
|
|
48
|
+
/** 把 `UpsertOptions`(update 缺省语义)解析成确定的更新列集合,并做全部形状校验。 */
|
|
49
|
+
export declare function resolveUpsertPlan(row: Record<string, SqlValue>, options: UpsertOptions): UpsertPlan;
|
|
50
|
+
/**
|
|
51
|
+
* 编译 `INSERT ... ON CONFLICT (target) DO UPDATE SET ...`。
|
|
52
|
+
* 值全部参数绑定(VALUES 先入 params,DO UPDATE SET 的赋值随后),
|
|
53
|
+
* SQLite `?` 与 PG `$n` 均按入参顺序编号,两方言产出的 SQL 逐字同形。
|
|
54
|
+
*/
|
|
55
|
+
export declare function compileUpsert(table: string, row: Record<string, SqlValue>, options: UpsertOptions, dialect: QueryDialect): CompiledQuery;
|
|
43
56
|
export declare function compileUpdate(table: string, wheres: readonly WhereClause[], values: Record<string, SqlValue>, dialect: QueryDialect): CompiledQuery;
|
|
44
57
|
export declare function compileDelete(table: string, wheres: readonly WhereClause[], dialect: QueryDialect): CompiledQuery;
|
|
45
58
|
/** 写操作守卫:事务进行中时,禁止通过**事务外**句柄写同一项目库(契约 §cloud-db 注释)。 */
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ChainSpec } from '@adep/types';
|
|
1
2
|
import type { CapabilityBundle } from '@adep/types';
|
|
2
3
|
import type { ProjectDriver } from '../../db/project-driver';
|
|
3
4
|
export interface CreateDbCapabilityOptions {
|
|
@@ -8,6 +9,8 @@ export interface CreateDbCapabilityOptions {
|
|
|
8
9
|
*/
|
|
9
10
|
beforeWrite?: (driver: ProjectDriver) => Promise<void>;
|
|
10
11
|
}
|
|
12
|
+
/** 链式能力的装配规格:FN worker 只按方法名清单构造客户端,不感知 db 细节。 */
|
|
13
|
+
export declare const CLOUD_DB_SPEC: ChainSpec;
|
|
11
14
|
/**
|
|
12
15
|
* 按驱动产出一组 `cloud.db` 能力。驱动已绑定项目库(连接层隔离,DB-001);
|
|
13
16
|
* `projectId` 在装配期绑定,函数侧不可感知。内部无共享可变状态污染跨项目调用。
|
package/dist/index.js
CHANGED
|
@@ -401,14 +401,14 @@ var sqliteDialect = {
|
|
|
401
401
|
placeholder: () => "?",
|
|
402
402
|
quote: (identifier) => `"${identifier.replace(/"/g, '""')}"`,
|
|
403
403
|
lastInsertIdClause: () => "",
|
|
404
|
-
upsertConflictClause: (columns) => ` ON CONFLICT (${columns.map((column) => `"${column.replace(/"/g, '""')}"`).join(", ")}) DO NOTHING
|
|
404
|
+
upsertConflictClause: (columns, doUpdateSet) => ` ON CONFLICT (${columns.map((column) => `"${column.replace(/"/g, '""')}"`).join(", ")})` + (doUpdateSet ? ` DO UPDATE SET ${doUpdateSet}` : " DO NOTHING")
|
|
405
405
|
};
|
|
406
406
|
var postgresDialect = {
|
|
407
407
|
name: "postgres",
|
|
408
408
|
placeholder: (index) => `$${index + 1}`,
|
|
409
409
|
quote: (identifier) => `"${identifier.replace(/"/g, '""')}"`,
|
|
410
410
|
lastInsertIdClause: () => " RETURNING id",
|
|
411
|
-
upsertConflictClause: (columns) => ` ON CONFLICT (${columns.map((column) => `"${column.replace(/"/g, '""')}"`).join(", ")}) DO NOTHING
|
|
411
|
+
upsertConflictClause: (columns, doUpdateSet) => ` ON CONFLICT (${columns.map((column) => `"${column.replace(/"/g, '""')}"`).join(", ")})` + (doUpdateSet ? ` DO UPDATE SET ${doUpdateSet}` : " DO NOTHING")
|
|
412
412
|
};
|
|
413
413
|
function dialectFor(driver) {
|
|
414
414
|
return driver.engine === "pg" ? postgresDialect : sqliteDialect;
|
|
@@ -735,6 +735,9 @@ function compileWhere(wheres, dialect, params) {
|
|
|
735
735
|
if (value === null) {
|
|
736
736
|
if (where.operator === "=") return `${quoted} IS NULL`;
|
|
737
737
|
if (where.operator === "!=") return `${quoted} IS NOT NULL`;
|
|
738
|
+
throw unsafeOperation(
|
|
739
|
+
`NULL \u53EA\u80FD\u4E0E = / != \u6BD4\u8F83\uFF08${where.operator} NULL \u6052\u4E3A UNKNOWN\uFF0C\u4F1A\u9759\u9ED8\u8FD4\u56DE\u7A7A\u96C6\uFF09\uFF1A\u5217 ${where.column}`
|
|
740
|
+
);
|
|
738
741
|
}
|
|
739
742
|
const mark = pushParam(params, dialect, value);
|
|
740
743
|
return `${quoted} ${where.operator} ${mark}`;
|
|
@@ -809,6 +812,44 @@ function compileInsertMany(table, rows, dialect) {
|
|
|
809
812
|
params
|
|
810
813
|
};
|
|
811
814
|
}
|
|
815
|
+
function resolveUpsertPlan(row, options) {
|
|
816
|
+
const target = [...options.target];
|
|
817
|
+
if (target.length === 0) {
|
|
818
|
+
throw unsafeOperation("upsert \u7684 target \u5FC5\u987B\u81F3\u5C11\u4E00\u5217\uFF08\u51B2\u7A81\u5224\u5B9A\u65E0\u5217\u5219\u65E0\u4ECE\u8C08\u8D77\uFF09");
|
|
819
|
+
}
|
|
820
|
+
target.forEach(assertUserColumn);
|
|
821
|
+
const targetSet = new Set(target);
|
|
822
|
+
const rowColumns = new Set(Object.keys(row));
|
|
823
|
+
if (rowColumns.size === 0) {
|
|
824
|
+
throw unsafeOperation("upsert \u81F3\u5C11\u9700\u8981\u4E00\u4E2A\u5B57\u6BB5");
|
|
825
|
+
}
|
|
826
|
+
const update = options.update === void 0 ? [...rowColumns].filter((name) => !targetSet.has(name)) : [...options.update];
|
|
827
|
+
update.forEach(assertUserColumn);
|
|
828
|
+
const overlapping = update.filter((name) => targetSet.has(name));
|
|
829
|
+
if (overlapping.length > 0) {
|
|
830
|
+
throw unsafeOperation(`upsert \u7684 update \u5217\u4E0D\u80FD\u4E0E target \u91CD\u53E0\uFF08\u51B2\u7A81\u884C\u4E3B\u952E\u4E0D\u6539\uFF09\uFF1A${overlapping.join(", ")}`);
|
|
831
|
+
}
|
|
832
|
+
const missing = update.filter((name) => !rowColumns.has(name));
|
|
833
|
+
if (missing.length > 0) {
|
|
834
|
+
throw unsafeOperation(`upsert \u7684 update \u5217\u5FC5\u987B\u51FA\u73B0\u5728 row \u4E2D\uFF08\u503C\u53D6\u81EA row\uFF09\uFF1A${missing.join(", ")}`);
|
|
835
|
+
}
|
|
836
|
+
return { target, update };
|
|
837
|
+
}
|
|
838
|
+
function compileUpsert(table, row, options, dialect) {
|
|
839
|
+
const { target, update } = resolveUpsertPlan(row, options);
|
|
840
|
+
const params = [];
|
|
841
|
+
const entries = Object.entries(row);
|
|
842
|
+
const columns = entries.map(([name]) => {
|
|
843
|
+
assertUserColumn(name);
|
|
844
|
+
return dialect.quote(name);
|
|
845
|
+
}).join(", ");
|
|
846
|
+
const marks = entries.map(([, value]) => pushParam(params, dialect, value));
|
|
847
|
+
const doUpdateSet = update.length === 0 ? null : update.map((name) => `${dialect.quote(name)} = ${pushParam(params, dialect, row[name])}`).join(", ");
|
|
848
|
+
return {
|
|
849
|
+
sql: `INSERT INTO ${dialect.quote(table)} (${columns}) VALUES (${marks.join(", ")})${dialect.upsertConflictClause(target, doUpdateSet)}`,
|
|
850
|
+
params
|
|
851
|
+
};
|
|
852
|
+
}
|
|
812
853
|
function compileUpdate(table, wheres, values, dialect) {
|
|
813
854
|
const entries = Object.entries(values);
|
|
814
855
|
if (entries.length === 0) {
|
|
@@ -949,6 +990,17 @@ function createTableBuilder(table, driver, dialect, options = {}) {
|
|
|
949
990
|
const compiled = compileInsertMany(state.table, rows, dialect);
|
|
950
991
|
await driver.run(compiled.sql, compiled.params);
|
|
951
992
|
},
|
|
993
|
+
async upsert(row, conflict) {
|
|
994
|
+
guardWrite();
|
|
995
|
+
Object.keys(row).forEach(assertUserColumn);
|
|
996
|
+
if (await resolveOwned(state, driver)) {
|
|
997
|
+
throw unsafeOperation(
|
|
998
|
+
"upsert \u4E0D\u652F\u6301 owned \u8868\uFF1A\u53D8\u66F4\u6D41\u65E0\u6CD5\u8868\u8FBE\u300C\u63D2\u5165\u8FD8\u662F\u66F4\u65B0\u300D\u7684\u4E8C\u4E49\uFF08before/after \u987B\u786E\u5B9A\uFF09\uFF0C\u8BF7\u6539\u7528 insert / update"
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
1001
|
+
const compiled = compileUpsert(state.table, row, conflict, dialect);
|
|
1002
|
+
await driver.run(compiled.sql, compiled.params);
|
|
1003
|
+
},
|
|
952
1004
|
async update(values) {
|
|
953
1005
|
guardWrite();
|
|
954
1006
|
assertHasWhere(state);
|
|
@@ -1087,10 +1139,14 @@ function createCloudDb(driver, options = {}) {
|
|
|
1087
1139
|
var CLOUD_DB_SPEC = {
|
|
1088
1140
|
kind: "cloud-db",
|
|
1089
1141
|
rootMethod: "table",
|
|
1090
|
-
|
|
1091
|
-
|
|
1142
|
+
// 规格清单必须与构建器公开方法面一致(防漂移门 spec-drift.test.ts 双向咬合):
|
|
1143
|
+
// - owned:DB-006 的 owned 声明(step);
|
|
1144
|
+
// - execute:BUG-4 统一执行入口(direct,宿主侧 DB_RPC.execute 分支早已在);
|
|
1145
|
+
// - upsert:DB-010 终端(漏登记过一次,真机 500,见 spec-drift.test.ts 头注释)。
|
|
1146
|
+
stepMethods: ["select", "where", "orderBy", "limit", "offset", "owned"],
|
|
1147
|
+
terminalMethods: ["get", "first", "count", "insert", "insertMany", "update", "delete", "upsert"],
|
|
1092
1148
|
// CF-013:writeQuery 是 D1 shim 的写路径(INSERT/UPDATE/DELETE),与 query 并列直通方法。
|
|
1093
|
-
directMethods: ["query", "writeQuery", "changes"],
|
|
1149
|
+
directMethods: ["query", "writeQuery", "execute", "changes"],
|
|
1094
1150
|
transactionMethod: "transaction"
|
|
1095
1151
|
};
|
|
1096
1152
|
var WRITE_TERMINALS = /* @__PURE__ */ new Set(["insert", "insertMany", "update", "delete"]);
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
},
|
|
8
8
|
"homepage": "https://adep.top",
|
|
9
9
|
"bugs": "https://gitcode.com/yunqiang_wu/ai-yunke/issues",
|
|
10
|
-
"version": "0.3.
|
|
10
|
+
"version": "0.3.14",
|
|
11
11
|
"private": false,
|
|
12
12
|
"type": "module",
|
|
13
13
|
"sideEffects": false,
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@adep/types": "0.4.
|
|
35
|
+
"@adep/types": "0.4.4"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"test": "vitest run",
|