mirror of
https://github.com/hcengineering/platform.git
synced 2025-05-12 10:25:51 +00:00
Fix lookups from array (#8458)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
parent
6c052f290b
commit
71fc1f3a8d
@ -1012,6 +1012,13 @@ abstract class PostgresAdapterBase implements DbAdapter {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private isArrayLookup (_class: Ref<Class<Doc>>, key: string): boolean {
|
||||||
|
const attr = this.hierarchy.findAttribute(_class, key)
|
||||||
|
if (attr === undefined) return false
|
||||||
|
if (attr.type._class === core.class.ArrOf) return true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
private buildJoinValue<T extends Doc>(
|
private buildJoinValue<T extends Doc>(
|
||||||
clazz: Ref<Class<T>>,
|
clazz: Ref<Class<T>>,
|
||||||
lookup: Lookup<T>,
|
lookup: Lookup<T>,
|
||||||
@ -1029,6 +1036,10 @@ abstract class PostgresAdapterBase implements DbAdapter {
|
|||||||
const _class = Array.isArray(value) ? value[0] : value
|
const _class = Array.isArray(value) ? value[0] : value
|
||||||
const nested = Array.isArray(value) ? value[1] : undefined
|
const nested = Array.isArray(value) ? value[1] : undefined
|
||||||
const domain = translateDomain(this.hierarchy.getDomain(_class))
|
const domain = translateDomain(this.hierarchy.getDomain(_class))
|
||||||
|
if (this.isArrayLookup(clazz, key)) {
|
||||||
|
this.getArrayLookup(baseDomain, key, _class, res, domain, parentKey)
|
||||||
|
continue
|
||||||
|
}
|
||||||
const tkey = domain === DOMAIN_MODEL ? key : this.transformKey(baseDomain, clazz, key)
|
const tkey = domain === DOMAIN_MODEL ? key : this.transformKey(baseDomain, clazz, key)
|
||||||
const as = `lookup_${domain}_${parentKey !== undefined ? parentKey + '_lookup_' + key : key}`.toLowerCase()
|
const as = `lookup_${domain}_${parentKey !== undefined ? parentKey + '_lookup_' + key : key}`.toLowerCase()
|
||||||
res.push({
|
res.push({
|
||||||
@ -1047,6 +1058,34 @@ abstract class PostgresAdapterBase implements DbAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getArrayLookup (
|
||||||
|
parentDomain: string,
|
||||||
|
key: string,
|
||||||
|
_class: Ref<Class<Doc>>,
|
||||||
|
result: JoinProps[],
|
||||||
|
domain: string,
|
||||||
|
parent?: string
|
||||||
|
): void {
|
||||||
|
const desc = this.hierarchy
|
||||||
|
.getDescendants(this.hierarchy.getBaseClass(_class))
|
||||||
|
.filter((it) => !this.hierarchy.isMixin(it))
|
||||||
|
const as = `reverse_lookup_${domain}_${parent !== undefined ? parent + '_lookup_' + key : key}`
|
||||||
|
const from = isDataField(domain, key)
|
||||||
|
? `ANY(SELECT jsonb_array_elements_text(${parentDomain}.data->'${key}'))`
|
||||||
|
: `ANY(${parentDomain}."${key}")`
|
||||||
|
result.push({
|
||||||
|
isReverse: true,
|
||||||
|
table: domain,
|
||||||
|
toAlias: as,
|
||||||
|
toField: '_id',
|
||||||
|
classes: desc,
|
||||||
|
path: parent !== undefined ? `${parent}.${key}` : key,
|
||||||
|
fromAlias: '',
|
||||||
|
toClass: _class,
|
||||||
|
fromField: from
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
private getReverseLookupValue (
|
private getReverseLookupValue (
|
||||||
parentDomain: string,
|
parentDomain: string,
|
||||||
lookup: ReverseLookups,
|
lookup: ReverseLookups,
|
||||||
@ -1436,7 +1475,7 @@ abstract class PostgresAdapterBase implements DbAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [
|
return [
|
||||||
`(SELECT jsonb_agg(${join.toAlias}.*) FROM ${join.table} AS ${join.toAlias} WHERE ${join.fromAlias}.${join.fromField} = ${join.toAlias}."${join.toField}" ${classsesQuery}) AS ${join.toAlias}`
|
`(SELECT jsonb_agg(${join.toAlias}.*) FROM ${join.table} AS ${join.toAlias} WHERE ${join.toAlias}."${join.toField}" = ${join.fromAlias}${join.fromAlias !== '' ? '.' : ''}${join.fromField} ${classsesQuery}) AS ${join.toAlias}`
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user