@@ -8,12 +8,14 @@ import { request } from 'graphql-request';
88import type { RelationsListWithNodes } from '../utils/convert-relations.js' ;
99import type { RelationTypeIdInfo } from '../utils/get-relation-type-ids.js' ;
1010import { buildRelationsSelection } from '../utils/relation-query-helpers.js' ;
11+ import type { SpaceSelection } from './internal/space-selection.js' ;
12+ import { normalizeSpaceSelection } from './internal/space-selection.js' ;
13+ import type { SpaceSelectionInput } from './types.js' ;
1114
12- export type FindManyPublicParams < S extends Schema . Schema . AnyNoContext > = {
15+ export type FindManyPublicParams < S extends Schema . Schema . AnyNoContext > = SpaceSelectionInput & {
1316 filter ?: Entity . EntityFilter < Schema . Schema . Type < S > > | undefined ;
1417 // TODO: restrict multi-level nesting to the actual relation keys
1518 include ?: Entity . EntityInclude < S > | undefined ;
16- space : string ;
1719 first ?: number | undefined ;
1820 offset ?: number | undefined ;
1921 orderBy ?:
@@ -25,34 +27,73 @@ export type FindManyPublicParams<S extends Schema.Schema.AnyNoContext> = {
2527 backlinksTotalCountsTypeId1 ?: string | undefined ;
2628} ;
2729
28- const buildEntitiesQuery = ( relationInfoLevel1 : RelationTypeIdInfo [ ] , useOrderBy : boolean ) => {
29- const level1Relations = buildRelationsSelection ( relationInfoLevel1 ) ;
30+ const buildEntitiesQuery = (
31+ relationInfoLevel1 : RelationTypeIdInfo [ ] ,
32+ useOrderBy : boolean ,
33+ spaceSelection : SpaceSelection ,
34+ ) => {
35+ const level1Relations = buildRelationsSelection ( relationInfoLevel1 , spaceSelection . mode ) ;
3036
3137 const queryName = useOrderBy ? 'entitiesOrderedByProperty' : 'entities' ;
32- const orderByParams = useOrderBy ? '$propertyId: UUID!, $sortDirection: SortOrder!, ' : '' ;
38+ const variableDefinitions = [
39+ spaceSelection . mode === 'single'
40+ ? '$spaceId: UUID!'
41+ : spaceSelection . mode === 'many'
42+ ? '$spaceIds: [UUID!]!'
43+ : undefined ,
44+ '$typeIds: [UUID!]!' ,
45+ useOrderBy ? '$propertyId: UUID!' : undefined ,
46+ useOrderBy ? '$sortDirection: SortOrder!' : undefined ,
47+ '$first: Int' ,
48+ '$filter: EntityFilter!' ,
49+ '$offset: Int' ,
50+ '$backlinksTotalCountsTypeId1: UUID' ,
51+ '$backlinksTotalCountsTypeId1Present: Boolean!' ,
52+ ]
53+ . filter ( Boolean )
54+ . join ( ', ' ) ;
55+
3356 const orderByArgs = useOrderBy ? 'propertyId: $propertyId\n sortDirection: $sortDirection\n ' : '' ;
57+ const entitySpaceFilter =
58+ spaceSelection . mode === 'single'
59+ ? 'spaceIds: {in: [$spaceId]},'
60+ : spaceSelection . mode === 'many'
61+ ? 'spaceIds: {in: $spaceIds},'
62+ : '' ;
63+ const valuesListFilter =
64+ spaceSelection . mode === 'single'
65+ ? '(filter: { spaceId: { is: $spaceId } })'
66+ : spaceSelection . mode === 'many'
67+ ? '(filter: { spaceId: { in: $spaceIds } })'
68+ : '' ;
69+ const backlinksSpaceFilter =
70+ spaceSelection . mode === 'single'
71+ ? 'spaceId: {is: $spaceId}, '
72+ : spaceSelection . mode === 'many'
73+ ? 'spaceId: {in: $spaceIds}, '
74+ : '' ;
3475
3576 return `
36- query ${ queryName } ($spaceId: UUID!, $typeIds: [UUID!]!, ${ orderByParams } $first: Int, $filter: EntityFilter!, $offset: Int, $backlinksTotalCountsTypeId1: UUID, $backlinksTotalCountsTypeId1Present: Boolean! ) {
77+ query ${ queryName } (${ variableDefinitions } ) {
3778 entities: ${ queryName } (
3879 ${ orderByArgs } filter: { and: [{
3980 relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $typeIds}}},
40- spaceIds: {in: [$spaceId]},
81+ ${ entitySpaceFilter }
4182 }, $filter]}
4283 first: $first
4384 offset: $offset
4485 ) {
4586 id
4687 name
47- valuesList(filter: {spaceId: {is: $spaceId}}) {
88+ valuesList${ valuesListFilter } {
4889 propertyId
4990 string
5091 boolean
5192 number
5293 time
5394 point
5495 }
55- backlinksTotalCountsTypeId1: backlinks(filter: { spaceId: {is: $spaceId}, fromEntity: { typeIds: { is: [$backlinksTotalCountsTypeId1] } }}) @include(if: $backlinksTotalCountsTypeId1Present) {
96+ backlinksTotalCountsTypeId1: backlinks(filter: { ${ backlinksSpaceFilter } fromEntity: { typeIds: { is: [$backlinksTotalCountsTypeId1] } }}) @include(if: $backlinksTotalCountsTypeId1Present) {
5697 totalCount
5798 }
5899 ${ level1Relations }
@@ -150,7 +191,16 @@ export const findManyPublic = async <S extends Schema.Schema.AnyNoContext>(
150191 type : S ,
151192 params ?: FindManyPublicParams < S > ,
152193) => {
153- const { filter, include, space, first = 100 , offset = 0 , orderBy, backlinksTotalCountsTypeId1 } = params ?? { } ;
194+ const {
195+ filter,
196+ include,
197+ space,
198+ spaces,
199+ first = 100 ,
200+ offset = 0 ,
201+ orderBy,
202+ backlinksTotalCountsTypeId1,
203+ } = params ?? { } ;
154204
155205 // constructing the relation type ids for the query
156206 const relationTypeIds = Utils . getRelationTypeIds ( type , include ) ;
@@ -187,18 +237,26 @@ export const findManyPublic = async <S extends Schema.Schema.AnyNoContext>(
187237 }
188238
189239 // Build the query dynamically with aliases for each relation type ID
190- const queryDocument = buildEntitiesQuery ( relationTypeIds , Boolean ( orderBy ) ) ;
240+ const spaceSelection = normalizeSpaceSelection ( space , spaces ) ;
241+
242+ // Build the query dynamically with aliases for each relation type ID
243+ const queryDocument = buildEntitiesQuery ( relationTypeIds , Boolean ( orderBy ) , spaceSelection ) ;
191244
192245 const filterParams = filter ? Utils . translateFilterToGraphql ( filter , type ) : { } ;
193246
194247 const queryVariables : Record < string , unknown > = {
195- spaceId : space ,
196248 typeIds,
197249 first,
198250 filter : filterParams ,
199251 offset,
200252 } ;
201253
254+ if ( spaceSelection . mode === 'single' ) {
255+ queryVariables . spaceId = spaceSelection . spaceId ;
256+ } else if ( spaceSelection . mode === 'many' ) {
257+ queryVariables . spaceIds = spaceSelection . spaceIds ;
258+ }
259+
202260 if ( orderByPropertyId && sortDirection ) {
203261 queryVariables . propertyId = orderByPropertyId ;
204262 queryVariables . sortDirection = sortDirection ;
0 commit comments