@@ -2,7 +2,7 @@ import { invariant, isPlainObject } from '@zenstackhq/common-helpers';
22import type { Expression , ExpressionBuilder , ExpressionWrapper , SqlBool , ValueNode } from 'kysely' ;
33import { expressionBuilder , sql , type SelectQueryBuilder } from 'kysely' ;
44import { match , P } from 'ts-pattern' ;
5- import type { BuiltinType , DataSourceProviderType , FieldDef , GetModels , SchemaDef } from '../../../schema' ;
5+ import type { BuiltinType , DataSourceProviderType , FieldDef , GetModels , ModelDef , SchemaDef } from '../../../schema' ;
66import { enumerate } from '../../../utils/enumerate' ;
77import type { OrArray } from '../../../utils/type-utils' ;
88import { AGGREGATE_OPERATORS , DELEGATE_JOINED_FIELD_PREFIX , LOGICAL_COMBINATORS } from '../../constants' ;
@@ -963,6 +963,31 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
963963 return result ;
964964 }
965965
966+ protected buildModelSelect (
967+ eb : ExpressionBuilder < any , any > ,
968+ model : GetModels < Schema > ,
969+ subQueryAlias : string ,
970+ payload : true | FindArgs < Schema , GetModels < Schema > , true > ,
971+ selectAllFields : boolean ,
972+ ) {
973+ let subQuery = this . buildSelectModel ( eb , model , subQueryAlias ) ;
974+
975+ if ( selectAllFields ) {
976+ subQuery = this . buildSelectAllFields (
977+ model ,
978+ subQuery ,
979+ typeof payload === 'object' ? payload ?. omit : undefined ,
980+ subQueryAlias ,
981+ ) ;
982+ }
983+
984+ if ( payload && typeof payload === 'object' ) {
985+ subQuery = this . buildFilterSortTake ( model , payload , subQuery , subQueryAlias ) ;
986+ }
987+
988+ return subQuery ;
989+ }
990+
966991 buildSelectField (
967992 query : SelectQueryBuilder < any , any , any > ,
968993 model : string ,
@@ -1115,6 +1140,35 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
11151140 return buildFieldRef ( this . schema , model , field , this . options , eb , modelAlias , inlineComputedField ) ;
11161141 }
11171142
1143+ protected canJoinWithoutNestedSelect (
1144+ modelDef : ModelDef ,
1145+ payload : boolean | FindArgs < Schema , GetModels < Schema > , true > ,
1146+ ) {
1147+ if ( modelDef . computedFields ) {
1148+ // computed fields requires explicit select
1149+ return false ;
1150+ }
1151+
1152+ if ( modelDef . baseModel || modelDef . isDelegate ) {
1153+ // delegate models require upward/downward joins
1154+ return false ;
1155+ }
1156+
1157+ if (
1158+ typeof payload === 'object' &&
1159+ ( payload . orderBy ||
1160+ payload . skip !== undefined ||
1161+ payload . take !== undefined ||
1162+ payload . cursor ||
1163+ ( payload as any ) . distinct )
1164+ ) {
1165+ // ordering/pagination/distinct needs to be handled before joining
1166+ return false ;
1167+ }
1168+
1169+ return true ;
1170+ }
1171+
11181172 // #endregion
11191173
11201174 // #region abstract methods
0 commit comments