@@ -88,6 +88,7 @@ import {
8888 DBCollectionHelper ,
8989 type DBDoc ,
9090 doFetchTypes ,
91+ escape ,
9192 filterProjection ,
9293 getDBClient ,
9394 inferType ,
@@ -661,7 +662,7 @@ abstract class PostgresAdapterBase implements DbAdapter {
661662 { } ,
662663 async ( ) => {
663664 try {
664- const domain = translateDomain ( options ?. domain ?? this . hierarchy . getDomain ( _class ) )
665+ const domain = translateDomain ( this . hierarchy . getDomain ( _class ) )
665666 const sqlChunks : string [ ] = [ ]
666667
667668 const joins = this . buildJoins < T > ( _class , options )
@@ -685,7 +686,7 @@ abstract class PostgresAdapterBase implements DbAdapter {
685686 sqlChunks . push ( this . buildOrder ( _class , domain , options . sort , joins ) )
686687 }
687688 if ( options ?. limit !== undefined ) {
688- sqlChunks . push ( `LIMIT ${ options . limit } ` )
689+ sqlChunks . push ( `LIMIT ${ escape ( options . limit ) } ` )
689690 }
690691
691692 return ( await this . mgr . retry ( ctx . id , async ( connection ) => {
@@ -715,11 +716,7 @@ abstract class PostgresAdapterBase implements DbAdapter {
715716 fquery = finalSql
716717
717718 const result = await connection . execute ( finalSql , vars . getValues ( ) )
718- if (
719- options ?. lookup === undefined &&
720- options ?. domainLookup === undefined &&
721- options ?. associations === undefined
722- ) {
719+ if ( options ?. lookup === undefined && options ?. associations === undefined ) {
723720 return toFindResult (
724721 result . map ( ( p ) => parseDocWithProjection ( p , domain , projection ) ) ,
725722 total
@@ -752,49 +749,35 @@ abstract class PostgresAdapterBase implements DbAdapter {
752749 ) : Projection < T > | undefined {
753750 if ( projection === undefined ) return
754751
752+ const res : Projection < T > = { }
755753 if ( ! this . hierarchy . isMixin ( _class ) ) {
756- return projection
754+ for ( const key in projection ) {
755+ ; ( res as any ) [ escape ( key ) ] = escape ( projection [ key ] )
756+ }
757+ return res
757758 }
758759
759- projection = { ...projection }
760760 for ( const key in projection ) {
761- if ( key . includes ( '.' ) ) continue
762- try {
763- const attr = this . hierarchy . findAttribute ( _class , key )
764- if ( attr !== undefined && this . hierarchy . isMixin ( attr . attributeOf ) ) {
765- const newKey = `${ attr . attributeOf } .${ attr . name } ` as keyof Projection < T >
766- projection [ newKey ] = projection [ key ]
767-
768- // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
769- delete projection [ key ]
761+ if ( key . includes ( '.' ) ) {
762+ ; ( res as any ) [ escape ( key ) ] = escape ( projection [ key ] )
763+ } else {
764+ try {
765+ const attr = this . hierarchy . findAttribute ( _class , key )
766+ if ( attr !== undefined && this . hierarchy . isMixin ( attr . attributeOf ) ) {
767+ const newKey = `${ attr . attributeOf } .${ attr . name } ` as keyof Projection < T >
768+ res [ newKey ] = escape ( projection [ key ] )
769+ }
770+ } catch ( err : any ) {
771+ // ignore, if
770772 }
771- } catch ( err : any ) {
772- // ignore, if
773773 }
774774 }
775775
776- return projection
776+ return res
777777 }
778778
779779 private buildJoins < T extends Doc > ( _class : Ref < Class < T > > , options : ServerFindOptions < T > | undefined ) : JoinProps [ ] {
780780 const joins = this . buildJoin ( _class , options ?. lookup )
781- if ( options ?. domainLookup !== undefined ) {
782- const baseDomain = translateDomain ( this . hierarchy . getDomain ( _class ) )
783-
784- const domain = translateDomain ( options . domainLookup . domain )
785- const key = options . domainLookup . field
786- const as = `lookup_${ domain } _${ key } `
787- joins . push ( {
788- isReverse : false ,
789- table : domain ,
790- path : options . domainLookup . field ,
791- toAlias : as ,
792- toField : '_id' ,
793- fromField : key ,
794- fromAlias : baseDomain ,
795- toClass : undefined
796- } )
797- }
798781 return joins
799782 }
800783
@@ -1054,7 +1037,8 @@ abstract class PostgresAdapterBase implements DbAdapter {
10541037 parentAlias ?: string
10551038 ) : void {
10561039 const baseDomain = parentAlias ?? translateDomain ( this . hierarchy . getDomain ( clazz ) )
1057- for ( const key in lookup ) {
1040+ for ( const _key in lookup ) {
1041+ const key = escape ( _key )
10581042 if ( key === '_id' ) {
10591043 this . getReverseLookupValue ( baseDomain , lookup , res , parentKey )
10601044 continue
@@ -1195,7 +1179,8 @@ abstract class PostgresAdapterBase implements DbAdapter {
11951179 if ( options ?. skipClass !== true ) {
11961180 query . _class = this . fillClass ( _class , query ) as any
11971181 }
1198- for ( const key in query ) {
1182+ for ( const _key in query ) {
1183+ const key = escape ( _key )
11991184 if ( options ?. skipSpace === true && key === 'space' ) {
12001185 continue
12011186 }
@@ -1526,7 +1511,7 @@ abstract class PostgresAdapterBase implements DbAdapter {
15261511 getAssociationsProjections ( vars : ValuesVariables , baseDomain : string , associations : AssociationQuery [ ] ) : string [ ] {
15271512 const res : string [ ] = [ ]
15281513 for ( const association of associations ) {
1529- const _id = association [ 0 ]
1514+ const _id = escape ( association [ 0 ] )
15301515 const assoc = this . modelDb . findObject ( _id )
15311516 if ( assoc === undefined ) {
15321517 continue
@@ -1547,7 +1532,7 @@ abstract class PostgresAdapterBase implements DbAdapter {
15471532 AND relation."workspaceId" = ${ wsId }
15481533 WHERE relation."${ keyA } " = ${ translateDomain ( baseDomain ) } ."_id"
15491534 AND relation.association = '${ _id } '
1550- AND assoc."workspaceId" = ${ wsId } ) AS assoc_${ tagetDomain } _${ association [ 0 ] } `
1535+ AND assoc."workspaceId" = ${ wsId } ) AS assoc_${ tagetDomain } _${ _id } `
15511536 )
15521537 }
15531538 return res
@@ -1581,7 +1566,8 @@ abstract class PostgresAdapterBase implements DbAdapter {
15811566 if ( projection . _class === undefined ) {
15821567 res . push ( `${ baseDomain } ."_class" AS "_class"` )
15831568 }
1584- for ( const key in projection ) {
1569+ for ( const _key in projection ) {
1570+ const key = escape ( _key )
15851571 if ( isDataField ( baseDomain , key ) ) {
15861572 if ( ! dataAdded ) {
15871573 res . push ( `${ baseDomain } .data as data` )
0 commit comments