@@ -18,7 +18,7 @@ import * as uuid from 'uuid';
1818import type { BuiltinType , Expression , FieldDef } from '../../../schema' ;
1919import { ExpressionUtils , type GetModels , type ModelDef , type SchemaDef } from '../../../schema' ;
2020import type { AnyKysely } from '../../../utils/kysely-utils' ;
21- import { extractFields , fieldsToSelectObject } from '../../../utils/object-utils' ;
21+ import { extractFields , fieldsToSelectObject , isEmptyObject } from '../../../utils/object-utils' ;
2222import { NUMERIC_FIELD_TYPES } from '../../constants' ;
2323import { TransactionIsolationLevel , type ClientContract , type CRUD } from '../../contract' ;
2424import type { FindArgs , SelectIncludeOmit , WhereInput } from '../../crud-types' ;
@@ -1152,7 +1152,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
11521152
11531153 const parentWhere = await this . buildUpdateParentRelationFilter ( kysely , fromRelation ) ;
11541154
1155- let combinedWhere : Record < string , unknown > = where ?? { } ;
1155+ let combinedWhere : Record < string , unknown > = where ?? true ;
11561156 if ( Object . keys ( parentWhere ) . length > 0 ) {
11571157 combinedWhere = Object . keys ( combinedWhere ) . length > 0 ? { AND : [ parentWhere , combinedWhere ] } : parentWhere ;
11581158 }
@@ -1574,7 +1574,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
15741574 }
15751575
15761576 const parentWhere = await this . buildUpdateParentRelationFilter ( kysely , fromRelation ) ;
1577- let combinedWhere : WhereInput < Schema , GetModels < Schema > , any , false > = where ?? { } ;
1577+ let combinedWhere : WhereInput < Schema , GetModels < Schema > , any , false > = where ?? true ;
15781578 if ( Object . keys ( parentWhere ) . length > 0 ) {
15791579 combinedWhere = Object . keys ( combinedWhere ) . length > 0 ? { AND : [ parentWhere , combinedWhere ] } : parentWhere ;
15801580 }
@@ -1890,12 +1890,12 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
18901890 }
18911891
18921892 case 'delete' : {
1893- await this . deleteRelation ( kysely , fieldModel , value , fromRelationContext , true ) ;
1893+ await this . deleteRelation ( kysely , fieldModel , value , fromRelationContext , true , true ) ;
18941894 break ;
18951895 }
18961896
18971897 case 'deleteMany' : {
1898- await this . deleteRelation ( kysely , fieldModel , value , fromRelationContext , false ) ;
1898+ await this . deleteRelation ( kysely , fieldModel , value , fromRelationContext , false , false ) ;
18991899 break ;
19001900 }
19011901
@@ -2031,6 +2031,9 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
20312031 } else {
20322032 disconnectConditions = [ true ] ;
20332033 }
2034+ } else if ( isEmptyObject ( data ) ) {
2035+ // empty object means true
2036+ disconnectConditions = [ true ] ;
20342037 } else {
20352038 disconnectConditions = this . normalizeRelationManipulationInput ( model , data ) ;
20362039
@@ -2235,23 +2238,35 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
22352238 model : GetModels < Schema > ,
22362239 data : any ,
22372240 fromRelation : FromRelationContext ,
2241+ uniqueDelete : boolean ,
22382242 throwForNotFound : boolean ,
22392243 ) {
22402244 let deleteConditions : any [ ] = [ ] ;
2241- let expectedDeleteCount : number ;
2245+ let expectedDeleteCount = - 1 ; // -1 means no expected delete count check
22422246 if ( typeof data === 'boolean' ) {
22432247 if ( data === false ) {
22442248 return ;
22452249 } else {
22462250 deleteConditions = [ true ] ;
2251+ if ( uniqueDelete ) {
2252+ expectedDeleteCount = 1 ;
2253+ }
2254+ }
2255+ } else if ( isEmptyObject ( data ) ) {
2256+ // empty object means true
2257+ deleteConditions = [ true ] ;
2258+ if ( uniqueDelete ) {
22472259 expectedDeleteCount = 1 ;
22482260 }
22492261 } else {
22502262 deleteConditions = this . normalizeRelationManipulationInput ( model , data ) ;
22512263 if ( deleteConditions . length === 0 ) {
22522264 return ;
22532265 }
2254- expectedDeleteCount = deleteConditions . length ;
2266+ if ( uniqueDelete ) {
2267+ // each delete condition is one unique match
2268+ expectedDeleteCount = deleteConditions . length ;
2269+ }
22552270 }
22562271
22572272 let deleteResult : Awaited < ReturnType < typeof this . delete > > ;
@@ -2319,7 +2334,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
23192334 }
23202335
23212336 // validate result
2322- if ( throwForNotFound && expectedDeleteCount > ( deleteResult . numAffectedRows ?? 0 ) ) {
2337+ if ( throwForNotFound && expectedDeleteCount >= 0 && expectedDeleteCount > ( deleteResult . numAffectedRows ?? 0 ) ) {
23232338 // some entities were not deleted
23242339 throw createNotFoundError ( deleteFromModel ) ;
23252340 }
0 commit comments