@mldong/jeeflow 1.6.4 → 1.6.5

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/dist/persist.d.ts CHANGED
@@ -38,6 +38,8 @@ export declare class SqliteDynamicTableWriter implements DynamicTableWriter {
38
38
  /** 列匹配(issues/20):默认宽松——驼峰↔下划线归一匹配(表单字段 companyName ↔ 表列 company_name);
39
39
  * 需要精确控制列名的集成方显式开启严格模式(忽略大小写精确匹配) */
40
40
  strictColumnMatch: boolean;
41
+ /** 主键生成器(issues/21):非自增主键表(雪花/应用生成)插入时生成主键值,入参表名 */
42
+ primaryKeyGenerator?: (tableName: string) => unknown;
41
43
  constructor(db: DatabaseSync);
42
44
  private tableColumns;
43
45
  filterColumns(tableName: string, columns: string[]): string[];
package/dist/persist.js CHANGED
@@ -41,6 +41,8 @@ export class SqliteDynamicTableWriter {
41
41
  /** 列匹配(issues/20):默认宽松——驼峰↔下划线归一匹配(表单字段 companyName ↔ 表列 company_name);
42
42
  * 需要精确控制列名的集成方显式开启严格模式(忽略大小写精确匹配) */
43
43
  strictColumnMatch = false;
44
+ /** 主键生成器(issues/21):非自增主键表(雪花/应用生成)插入时生成主键值,入参表名 */
45
+ primaryKeyGenerator;
44
46
  constructor(db) {
45
47
  this.db = db;
46
48
  }
@@ -48,11 +50,15 @@ export class SqliteDynamicTableWriter {
48
50
  const cached = this.cache.get(tableName);
49
51
  if (cached)
50
52
  return cached;
51
- // PRAGMA 不支持占位符——表名已过安全校验
53
+ // PRAGMA 不支持占位符——表名已过安全校验;INTEGER PRIMARY KEY 为 rowid 别名(自增)
52
54
  const rows = this.db.prepare(`PRAGMA table_info(${tableName})`).all();
53
55
  if (rows.length === 0)
54
56
  throw new Error(`persist: table ${tableName} not found`);
55
- const cols = rows.map(r => r.name.toUpperCase());
57
+ const cols = rows.map(r => ({
58
+ name: r.name.toUpperCase(),
59
+ primaryKey: r.pk === 1,
60
+ autoIncrement: r.pk === 1 && r.type.trim().toUpperCase() === 'INTEGER',
61
+ }));
56
62
  this.cache.set(tableName, cols);
57
63
  return cols;
58
64
  }
@@ -67,12 +73,21 @@ export class SqliteDynamicTableWriter {
67
73
  const names = [];
68
74
  const values = [];
69
75
  // 保持插入顺序稳定(对象键无序,按表列顺序取);写入用表列原名(issues/20)
70
- for (const col of cols) {
71
- const key = this.findDataKey(data, col);
72
- if (key === '')
76
+ for (const m of cols) {
77
+ const key = this.findDataKey(data, m.name);
78
+ if (key !== '') {
79
+ names.push(m.name);
80
+ values.push(data[key]);
73
81
  continue;
74
- names.push(col);
75
- values.push(data[key]);
82
+ }
83
+ // 主键生成(issues/21):非自增主键表且 data 无主键值 → 调生成器;未配置 → 清晰报错
84
+ if (m.primaryKey && !m.autoIncrement) {
85
+ if (!this.primaryKeyGenerator) {
86
+ throw new Error(`persist: table ${tableName} primary key ${m.name} is not auto-increment and no primary key generator configured (set primaryKeyGenerator, e.g. snowflake)`);
87
+ }
88
+ names.push(m.name);
89
+ values.push(this.primaryKeyGenerator(tableName));
90
+ }
76
91
  }
77
92
  if (names.length === 0)
78
93
  throw new Error(`persist: no matching columns for ${tableName}`);
@@ -83,13 +98,13 @@ export class SqliteDynamicTableWriter {
83
98
  }
84
99
  /** 列匹配(issues/20):严格=忽略大小写精确;宽松(默认)=驼峰↔下划线归一匹配 */
85
100
  findColumn(cols, key) {
86
- for (const col of cols) {
101
+ for (const m of cols) {
87
102
  if (this.strictColumnMatch) {
88
- if (col.toUpperCase() === key.toUpperCase())
89
- return col;
103
+ if (m.name.toUpperCase() === key.toUpperCase())
104
+ return m.name;
90
105
  }
91
- else if (normalizeColumn(col) === normalizeColumn(key)) {
92
- return col;
106
+ else if (normalizeColumn(m.name) === normalizeColumn(key)) {
107
+ return m.name;
93
108
  }
94
109
  }
95
110
  return '';
@@ -97,8 +112,13 @@ export class SqliteDynamicTableWriter {
97
112
  /** 在 data 中找匹配指定表列的 key(宽松模式驼峰 key 匹配下划线列) */
98
113
  findDataKey(data, col) {
99
114
  for (const k of Object.keys(data)) {
100
- if (this.findColumn([col], k) !== '')
115
+ if (this.strictColumnMatch) {
116
+ if (col.toUpperCase() === k.toUpperCase())
117
+ return k;
118
+ }
119
+ else if (normalizeColumn(col) === normalizeColumn(k)) {
101
120
  return k;
121
+ }
102
122
  }
103
123
  return '';
104
124
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mldong/jeeflow",
3
- "version": "1.6.4",
3
+ "version": "1.6.5",
4
4
  "description": "jeeflow workflow engine — Node.js / TypeScript implementation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",