Skip to content

Commit baef5c2

Browse files
authored
Minor fixes (#9285)
Signed-off-by: Denis Bykhov <[email protected]>
1 parent 4839c3b commit baef5c2

File tree

5 files changed

+44
-78
lines changed

5 files changed

+44
-78
lines changed

server/core/src/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,11 @@ import { type StorageAdapter } from './storage'
6868
import { type PlatformQueueProducer, type QueueTopic, type PlatformQueue } from './queue'
6969

7070
export interface ServerFindOptions<T extends Doc> extends FindOptions<T> {
71-
domain?: Domain // Allow to find for Doc's in specified domain only.
7271
prefix?: string
7372

7473
skipClass?: boolean
7574
skipSpace?: boolean
7675

77-
domainLookup?: {
78-
field: string
79-
domain: Domain
80-
}
81-
8276
// using for join query security
8377
allowedSpaces?: Ref<Space>[]
8478

server/middleware/src/domainFind.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class DomainFindMiddleware extends BaseMiddleware implements Middleware {
6262
return this.next?.findAll(ctx, _class, query, options) ?? emptyFindResult
6363
}
6464
const p = options?.prefix ?? 'client'
65-
const domain = options?.domain ?? this.context.hierarchy.getDomain(_class)
65+
const domain = this.context.hierarchy.getDomain(_class)
6666
if (domain === DOMAIN_MODEL) {
6767
return Promise.resolve(this.context.modelDb.findAllSync(_class, query, options))
6868
}

server/mongo/src/storage.ts

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -543,13 +543,9 @@ abstract class MongoAdapterBase implements DbAdapter {
543543
lookup: Lookup<T> | undefined,
544544
object: any,
545545
parent?: string,
546-
parentObject?: any,
547-
domainLookup?: {
548-
field: string
549-
domain: Domain
550-
}
546+
parentObject?: any
551547
): void {
552-
if (lookup === undefined && domainLookup === undefined) return
548+
if (lookup === undefined) return
553549
for (const key in lookup) {
554550
if (key === '_id') {
555551
this.fillReverseLookup(clazz, lookup, object, parent, parentObject)
@@ -567,14 +563,6 @@ abstract class MongoAdapterBase implements DbAdapter {
567563
this.fillLookup(value, object, key, fullKey, targetObject)
568564
}
569565
}
570-
if (domainLookup !== undefined) {
571-
if (object.$lookup === undefined) {
572-
object.$lookup = {}
573-
}
574-
object.$lookup._id = object['dl_' + domainLookup.field + '_lookup'][0]
575-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
576-
delete object['dl_' + domainLookup.field + '_lookup']
577-
}
578566
}
579567

580568
private fillReverseLookup<T extends Doc>(
@@ -654,18 +642,9 @@ abstract class MongoAdapterBase implements DbAdapter {
654642
const pipeline: any[] = []
655643
const tquery = this.translateQuery(clazz, query, options)
656644

657-
const slowPipeline = isLookupQuery(query) || isLookupSort(options?.sort) || options.domainLookup !== undefined
645+
const slowPipeline = isLookupQuery(query) || isLookupSort(options?.sort)
658646
const steps = this.getLookups(clazz, options?.lookup)
659647

660-
if (options.domainLookup !== undefined) {
661-
steps.push({
662-
from: options.domainLookup.domain,
663-
localField: options.domainLookup.field,
664-
foreignField: '_id',
665-
as: 'dl_' + options.domainLookup.field + '_lookup'
666-
})
667-
}
668-
669648
if (options.associations !== undefined && options.associations.length > 0) {
670649
const assoc = this.getAssociations(options.associations)
671650
steps.push(...assoc)
@@ -731,7 +710,7 @@ abstract class MongoAdapterBase implements DbAdapter {
731710
}
732711
for (const row of result) {
733712
ctx.withSync('fill-lookup', {}, (ctx) => {
734-
this.fillLookupValue(ctx, clazz, options?.lookup, row, undefined, undefined, options.domainLookup)
713+
this.fillLookupValue(ctx, clazz, options?.lookup, row)
735714
})
736715
if (row.$lookup !== undefined) {
737716
for (const [, v] of Object.entries(row.$lookup)) {
@@ -901,13 +880,12 @@ abstract class MongoAdapterBase implements DbAdapter {
901880
return addOperation(ctx, 'find-all', {}, async () => {
902881
const st = platformNow()
903882
let result: FindResult<T>
904-
const domain = options?.domain ?? this.hierarchy.getDomain(_class)
883+
const domain = this.hierarchy.getDomain(_class)
905884
if (
906885
options?.lookup != null ||
907886
options?.associations != null ||
908887
this.isEnumSort(_class, options) ||
909-
this.isRulesSort(options) ||
910-
options?.domainLookup !== undefined
888+
this.isRulesSort(options)
911889
) {
912890
return await this.findWithPipeline(ctx, domain, _class, query, options ?? {}, stTime)
913891
}

server/postgres/src/storage.ts

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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`)

server/postgres/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,11 @@ export interface JoinProps {
657657
toClass?: Ref<Class<Doc>>
658658
classes?: Ref<Class<Doc>>[] // filter by classes
659659
}
660+
661+
export function escape<T> (str: T): T {
662+
if (typeof str === 'string') {
663+
// Remove all characters except a-z, A-Z, 0-9 and _ .
664+
return str.replace(/[^a-zA-Z0-9_.]/g, '') as T
665+
}
666+
return str
667+
}

0 commit comments

Comments
 (0)