chanjs 1.0.45 → 1.0.46

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.
@@ -105,6 +105,43 @@ async update({query, params} = {}) {
105
105
  }
106
106
 
107
107
 
108
+ /**
109
+ * @description 批量更新多条记录(基于事务保证原子性)
110
+ * @param {Array<{query: Object, params: Object}>} updates - 更新操作数组,每个元素包含查询条件和更新内容
111
+ * @returns {Promise<{ affectedRows: number[] }>} 返回每个操作影响的行数数组
112
+ */
113
+ async updateMany(updates = []) {
114
+ // 参数合法性校验
115
+ if (!Array.isArray(updates)) {
116
+ throw new Error('参数必须为数组格式');
117
+ }
118
+
119
+ // 获取事务对象
120
+ const trx = await this.knex.transaction();
121
+
122
+ try {
123
+ const affectedRows = [];
124
+ // 循环处理每个更新操作
125
+ for (const { query, params } of updates) {
126
+ // 执行单个更新操作,使用事务对象替换原有knex实例
127
+ const result = await trx(this.model)
128
+ .where(query)
129
+ .update(params);
130
+ affectedRows.push(result);
131
+ }
132
+ // 提交事务
133
+ await trx.commit();
134
+ // 返回影响行数数组(与入参顺序一致)
135
+ return { affectedRows };
136
+ } catch (err) {
137
+ // 回滚事务
138
+ await trx.rollback();
139
+ // 错误向上抛出,由调用者处理
140
+ throw err;
141
+ }
142
+ }
143
+
144
+
108
145
 
109
146
 
110
147
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chanjs",
3
- "version": "1.0.45",
3
+ "version": "1.0.46",
4
4
  "description": "chanjs基于express 纯js研发的轻量级mvc框架。",
5
5
  "main": "index.js",
6
6
  "module": "index.js",