Skip to content

Commit bcc842e

Browse files
committed
fix(utils-uni): 优化数据库查询逻辑以支持事务模式下的更新操作
在事务模式下,更新操作只能通过 doc(id) 进行,因此修改 _endHost 方法以根据不同的 操作类型(query、create、update、remove、count)应用不同的查询条件构建逻辑。 同时,限制 limit 条件仅在 query 操作中生效,确保其他操作如 count、create、update、remove 不会受到 limit 和 skip 的影响。
1 parent 475b795 commit bcc842e

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

packages/utils-uni/src/database/_db.class.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,16 @@ export class Db<D1, S1 extends DbSelect<D1> = {}, D2 extends AnyObject = {}, W2
327327
return returnAggRef;
328328
}
329329

330-
private _endHost() {
330+
private _endHost(action: 'query' | 'create' | 'update' | 'remove' | 'count') {
331331
if (this._hasWhere) {
332-
// @ts-ignore
333-
this._host = this._host.where(_mapCommandRaw(this._where));
332+
// 事务模式更新只能用 doc(id)
333+
if (action === 'update' && this._isTransaction) {
334+
// @ts-ignore
335+
this._host = this._host.doc(this._where._id);
336+
} else {
337+
// @ts-ignore
338+
this._host = this._host.where(_mapCommandRaw(this._where));
339+
}
334340
}
335341

336342
if (this._hasSelect) {
@@ -349,9 +355,9 @@ export class Db<D1, S1 extends DbSelect<D1> = {}, D2 extends AnyObject = {}, W2
349355
if (this._hasSkip) this._host = this._host.skip(this._skip);
350356

351357
// @ts-ignore
352-
if (this._hasLimit) this._host = this._host.limit(this._limit);
358+
if (this._hasLimit && action === 'query') this._host = this._host.limit(this._limit);
353359
// @ts-ignore
354-
else if (this._hasWhereId) this._host = this._host.limit(1);
360+
else if (this._hasWhereId && action === 'query') this._host = this._host.limit(1);
355361
}
356362

357363
/**
@@ -371,7 +377,7 @@ export class Db<D1, S1 extends DbSelect<D1> = {}, D2 extends AnyObject = {}, W2
371377
}
372378
// 单表查询
373379
else {
374-
this._endHost();
380+
this._endHost('query');
375381
res = await this._host.get();
376382
}
377383

@@ -411,7 +417,7 @@ export class Db<D1, S1 extends DbSelect<D1> = {}, D2 extends AnyObject = {}, W2
411417
if (this._hasSkip) throw new Error('db.count() 方法不支持 skip 条件');
412418
if (this._hasLimit) throw new Error('db.count() 方法不支持 limit 条件');
413419

414-
this._endHost();
420+
this._endHost('count');
415421
const res = await this._host.count();
416422
const { total } = parseDatabaseOutput<{ total: number }>(res);
417423
return total;
@@ -430,7 +436,7 @@ export class Db<D1, S1 extends DbSelect<D1> = {}, D2 extends AnyObject = {}, W2
430436
if (this._hasSkip) throw new Error('db.create() 方法不支持 skip 条件');
431437
if (this._hasLimit) throw new Error('db.create() 方法不支持 limit 条件');
432438

433-
this._endHost();
439+
this._endHost('create');
434440
const res = await this._host.add(data);
435441
const { id } = parseDatabaseOutput<{ id: string }>(res);
436442
return id;
@@ -451,7 +457,7 @@ export class Db<D1, S1 extends DbSelect<D1> = {}, D2 extends AnyObject = {}, W2
451457

452458
if (this._isTransaction && !this._hasWhereId) throw new Error('事务模式下 db.update() 的 where 条件必须是 _id');
453459

454-
this._endHost();
460+
this._endHost('update');
455461
const res = await this._host.update(_mapCommandRaw(data));
456462
const { updated } = parseDatabaseOutput<{ updated: number }>(res);
457463
return updated;
@@ -471,7 +477,7 @@ export class Db<D1, S1 extends DbSelect<D1> = {}, D2 extends AnyObject = {}, W2
471477

472478
if (this._isTransaction && !this._hasWhereId) throw new Error('事务模式下 db.remove() 的 where 条件必须是 _id');
473479

474-
this._endHost();
480+
this._endHost('remove');
475481
const res = await this._host.remove();
476482
const { deleted } = parseDatabaseOutput<{ deleted: number }>(res);
477483
return deleted;

0 commit comments

Comments
 (0)