@@ -32,6 +32,8 @@ import {
3232 buildFieldRef ,
3333 buildJoinPairs ,
3434 getField ,
35+ getIdFields ,
36+ getManyToManyRelation ,
3537 getRelationForeignKeyFieldPairs ,
3638 isEnum ,
3739 makeDefaultOrderBy ,
@@ -68,18 +70,18 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
6870 eb : ExpressionBuilder < any , any > ,
6971 model : string ,
7072 modelAlias : string ,
71- where : object | undefined
73+ where : boolean | object | undefined
7274 ) {
73- let result = this . true ( eb ) ;
74-
75- if ( where === undefined ) {
76- return result ;
75+ if ( where === true || where === undefined ) {
76+ return this . true ( eb ) ;
7777 }
7878
79- if ( where === null || typeof where !== 'object' ) {
80- throw new InternalError ( 'impossible null as filter' ) ;
79+ if ( where === false ) {
80+ return this . false ( eb ) ;
8181 }
8282
83+ let result = this . true ( eb ) ;
84+
8385 for ( const [ key , payload ] of Object . entries ( where ) ) {
8486 if ( payload === undefined ) {
8587 continue ;
@@ -148,7 +150,12 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
148150 }
149151
150152 // call expression builder and combine the results
151- if ( '$expr' in where && typeof where [ '$expr' ] === 'function' ) {
153+ if (
154+ typeof where === 'object' &&
155+ where !== null &&
156+ '$expr' in where &&
157+ typeof where [ '$expr' ] === 'function'
158+ ) {
152159 result = this . and ( eb , result , where [ '$expr' ] ( eb ) ) ;
153160 }
154161
@@ -356,45 +363,67 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
356363 fieldDef : FieldDef ,
357364 payload : any
358365 ) {
359- const relationModel = fieldDef . type ;
360-
361- const relationKeyPairs = getRelationForeignKeyFieldPairs (
362- this . schema ,
363- model ,
364- field
365- ) ;
366-
367366 // null check needs to be converted to fk "is null" checks
368367 if ( payload === null ) {
369368 return eb ( sql . ref ( `${ table } .${ field } ` ) , 'is' , null ) ;
370369 }
371370
371+ const relationModel = fieldDef . type ;
372+
372373 const buildPkFkWhereRefs = ( eb : ExpressionBuilder < any , any > ) => {
373- let r = this . true ( eb ) ;
374- for ( const { fk, pk } of relationKeyPairs . keyPairs ) {
375- if ( relationKeyPairs . ownedByModel ) {
376- r = this . and (
377- eb ,
378- r ,
379- eb (
380- sql . ref ( `${ table } .${ fk } ` ) ,
381- '=' ,
382- sql . ref ( `${ relationModel } .${ pk } ` )
383- )
384- ) ;
385- } else {
386- r = this . and (
387- eb ,
388- r ,
389- eb (
390- sql . ref ( `${ table } .${ pk } ` ) ,
374+ const m2m = getManyToManyRelation ( this . schema , model , field ) ;
375+ if ( m2m ) {
376+ // many-to-many relation
377+ const modelIdField = getIdFields ( this . schema , model ) [ 0 ] ! ;
378+ const relationIdField = getIdFields (
379+ this . schema ,
380+ relationModel
381+ ) [ 0 ] ! ;
382+ return eb (
383+ sql . ref ( `${ relationModel } .${ relationIdField } ` ) ,
384+ 'in' ,
385+ eb
386+ . selectFrom ( m2m . joinTable )
387+ . select ( `${ m2m . joinTable } .${ m2m . otherFkName } ` )
388+ . whereRef (
389+ sql . ref ( `${ m2m . joinTable } .${ m2m . parentFkName } ` ) ,
391390 '=' ,
392- sql . ref ( `${ relationModel } .${ fk } ` )
391+ sql . ref ( `${ table } .${ modelIdField } ` )
393392 )
394- ) ;
393+ ) ;
394+ } else {
395+ const relationKeyPairs = getRelationForeignKeyFieldPairs (
396+ this . schema ,
397+ model ,
398+ field
399+ ) ;
400+
401+ let result = this . true ( eb ) ;
402+ for ( const { fk, pk } of relationKeyPairs . keyPairs ) {
403+ if ( relationKeyPairs . ownedByModel ) {
404+ result = this . and (
405+ eb ,
406+ result ,
407+ eb (
408+ sql . ref ( `${ table } .${ fk } ` ) ,
409+ '=' ,
410+ sql . ref ( `${ relationModel } .${ pk } ` )
411+ )
412+ ) ;
413+ } else {
414+ result = this . and (
415+ eb ,
416+ result ,
417+ eb (
418+ sql . ref ( `${ table } .${ pk } ` ) ,
419+ '=' ,
420+ sql . ref ( `${ relationModel } .${ fk } ` )
421+ )
422+ ) ;
423+ }
395424 }
425+ return result ;
396426 }
397- return r ;
398427 } ;
399428
400429 let result = this . true ( eb ) ;
0 commit comments