LiveQuery lookup fix (#1161)

Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
Denis Bykhov 2022-03-18 10:49:56 +06:00 committed by GitHub
parent 3ee999fb7e
commit 773022cfae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -247,7 +247,7 @@ export class LiveQuery extends TxProcessor implements Client {
const updatedDoc = q.result[pos] const updatedDoc = q.result[pos]
if (updatedDoc.modifiedOn > tx.modifiedOn) return if (updatedDoc.modifiedOn > tx.modifiedOn) return
if (updatedDoc.modifiedOn === tx.modifiedOn) { if (updatedDoc.modifiedOn === tx.modifiedOn) {
const current = await this.findOne(q._class, { _id: updatedDoc._id }) const current = await this.findOne(q._class, { _id: updatedDoc._id }, q.options)
if (current !== undefined) { if (current !== undefined) {
q.result[pos] = current q.result[pos] = current
} else { } else {
@ -614,7 +614,21 @@ export class LiveQuery extends TxProcessor implements Client {
if (q.options !== undefined) { if (q.options !== undefined) {
const lookup = (q.options.lookup as any)?.[key] const lookup = (q.options.lookup as any)?.[key]
if (lookup !== undefined) { if (lookup !== undefined) {
;(updatedDoc.$lookup as any)[key] = await this.client.findOne(lookup, { _id: ops[key] }) const lookupClass = getLookupClass(lookup)
const nestedLookup = getNestedLookup(lookup)
if (Array.isArray(ops[key])) {
;(updatedDoc.$lookup as any)[key] = await this.client.findAll(
lookupClass,
{ _id: { $in: ops[key] } },
{ lookup: nestedLookup }
)
} else {
;(updatedDoc.$lookup as any)[key] = await this.client.findOne(
lookupClass,
{ _id: ops[key] },
{ lookup: nestedLookup }
)
}
} }
} }
} else { } else {
@ -624,7 +638,24 @@ export class LiveQuery extends TxProcessor implements Client {
if (q.options !== undefined) { if (q.options !== undefined) {
const lookup = (q.options.lookup as any)?.[pkey] const lookup = (q.options.lookup as any)?.[pkey]
if (lookup !== undefined) { if (lookup !== undefined) {
;(updatedDoc.$lookup as any)[pkey].push(await this.client.findOne(lookup, { _id: (pops as any)[pkey] as Ref<Doc> })) const lookupClass = getLookupClass(lookup)
const nestedLookup = getNestedLookup(lookup)
const pp = updatedDoc.$lookup as any
if (pp[pkey] === undefined) {
pp[pkey] = []
}
if (Array.isArray((pops as any)[pkey])) {
const pushData = await this.client.findAll(
lookupClass,
{ _id: { $in: (pops as any)[pkey] } },
{ lookup: nestedLookup }
)
pp[pkey].push(...pushData)
} else {
pp[pkey].push(
await this.client.findOne(lookupClass, { _id: (pops as any)[pkey] }, { lookup: nestedLookup })
)
}
} }
} }
} }
@ -634,8 +665,16 @@ export class LiveQuery extends TxProcessor implements Client {
if (q.options !== undefined) { if (q.options !== undefined) {
const lookup = (q.options.lookup as any)?.[pkey] const lookup = (q.options.lookup as any)?.[pkey]
if (lookup !== undefined) { if (lookup !== undefined) {
const pid = (pops as any)[pkey] as Ref<Doc> const pid = (pops as any)[pkey]
;(updatedDoc.$lookup as any)[pkey] = ((updatedDoc.$lookup as any)[pkey]).filter((it: Doc) => it._id !== pid) const pp = updatedDoc.$lookup as any
if (pp[pkey] === undefined) {
pp[pkey] = []
}
if (Array.isArray(pid)) {
pp[pkey] = pp[pkey].filter((it: Doc) => !pid.includes(it._id))
} else {
pp[pkey] = pp[pkey].filter((it: Doc) => it._id !== pid)
}
} }
} }
} }
@ -680,3 +719,11 @@ export class LiveQuery extends TxProcessor implements Client {
} }
} }
} }
function getNestedLookup (lookup: Ref<Class<Doc>> | [Ref<Class<Doc>>, Lookup<Doc>]): Lookup<Doc> | undefined {
return Array.isArray(lookup) ? lookup[1] : undefined
}
function getLookupClass (lookup: Ref<Class<Doc>> | [Ref<Class<Doc>>, Lookup<Doc>]): Ref<Class<Doc>> {
return Array.isArray(lookup) ? lookup[0] : lookup
}