@adep/runtime 0.3.11 → 0.3.13
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;
|
|
@@ -64,7 +64,13 @@ export declare function appendChange(driver: ProjectDriver, table: string, op: C
|
|
|
64
64
|
/**
|
|
65
65
|
* 以 SAVEPOINT 包裹「写 + 变更流追加」,保证原子性。
|
|
66
66
|
* SAVEPOINT 可安全嵌套在外层事务(cloud.db.transaction / 链事务)内,无冲突。
|
|
67
|
-
* 回调为 async(DB-007 驱动 async
|
|
67
|
+
* 回调为 async(DB-007 驱动 async 化)。
|
|
68
|
+
*
|
|
69
|
+
* CORE-482(482 批取证):「SAVEPOINT / RELEASE 语句本身无方言差异」不成立——SQLite
|
|
70
|
+
* 允许事务块外裸 SAVEPOINT(隐式开启事务),PG 报 `SAVEPOINT can only be used in
|
|
71
|
+
* transaction blocks`。PG 分支改 BEGIN/COMMIT 包裹(嵌套在外层事务时 PG 不支持
|
|
72
|
+
* 事务内再 BEGIN——运维面 owned 写不经 cloud.db.transaction 包装,见调点;如后续
|
|
73
|
+
* 出现嵌套调用面需改保存点级联计数,先在此留痕)。
|
|
68
74
|
*/
|
|
69
75
|
export declare function runOwnedWrite(driver: ProjectDriver, fn: (d: ProjectDriver) => Promise<void>): Promise<void>;
|
|
70
76
|
/**
|
|
@@ -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 注释)。 */
|
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;
|
|
@@ -540,6 +540,17 @@ async function appendChange(driver, table, op, id, ownerKey, before, after) {
|
|
|
540
540
|
);
|
|
541
541
|
}
|
|
542
542
|
async function runOwnedWrite(driver, fn) {
|
|
543
|
+
if (driver.engine === "pg") {
|
|
544
|
+
await driver.run("BEGIN");
|
|
545
|
+
try {
|
|
546
|
+
await fn(driver);
|
|
547
|
+
await driver.run("COMMIT");
|
|
548
|
+
} catch (error) {
|
|
549
|
+
await driver.run("ROLLBACK");
|
|
550
|
+
throw error;
|
|
551
|
+
}
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
543
554
|
await driver.run("SAVEPOINT _adep_change");
|
|
544
555
|
try {
|
|
545
556
|
await fn(driver);
|
|
@@ -724,6 +735,9 @@ function compileWhere(wheres, dialect, params) {
|
|
|
724
735
|
if (value === null) {
|
|
725
736
|
if (where.operator === "=") return `${quoted} IS NULL`;
|
|
726
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
|
+
);
|
|
727
741
|
}
|
|
728
742
|
const mark = pushParam(params, dialect, value);
|
|
729
743
|
return `${quoted} ${where.operator} ${mark}`;
|
|
@@ -798,6 +812,44 @@ function compileInsertMany(table, rows, dialect) {
|
|
|
798
812
|
params
|
|
799
813
|
};
|
|
800
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
|
+
}
|
|
801
853
|
function compileUpdate(table, wheres, values, dialect) {
|
|
802
854
|
const entries = Object.entries(values);
|
|
803
855
|
if (entries.length === 0) {
|
|
@@ -938,6 +990,17 @@ function createTableBuilder(table, driver, dialect, options = {}) {
|
|
|
938
990
|
const compiled = compileInsertMany(state.table, rows, dialect);
|
|
939
991
|
await driver.run(compiled.sql, compiled.params);
|
|
940
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
|
+
},
|
|
941
1004
|
async update(values) {
|
|
942
1005
|
guardWrite();
|
|
943
1006
|
assertHasWhere(state);
|
|
@@ -3272,8 +3335,9 @@ function createProjectDriverStateBackend(driver) {
|
|
|
3272
3335
|
let ensured = false;
|
|
3273
3336
|
const ensureTable = async () => {
|
|
3274
3337
|
if (ensured) return;
|
|
3338
|
+
const tsType = driver.engine === "pg" ? "BIGINT" : "INTEGER";
|
|
3275
3339
|
await driver.run(
|
|
3276
|
-
`CREATE TABLE IF NOT EXISTS ${STATE_OBJECTS_TABLE} ("type" TEXT NOT NULL, "id" TEXT NOT NULL, "state" TEXT NOT NULL, "alarm_at"
|
|
3340
|
+
`CREATE TABLE IF NOT EXISTS ${STATE_OBJECTS_TABLE} ("type" TEXT NOT NULL, "id" TEXT NOT NULL, "state" TEXT NOT NULL, "alarm_at" ${tsType}, "updated_at" ${tsType} NOT NULL, PRIMARY KEY ("type", "id"))`
|
|
3277
3341
|
);
|
|
3278
3342
|
ensured = true;
|
|
3279
3343
|
};
|
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.13",
|
|
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",
|