@@ -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