fix query create lookup (#2993)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2023-04-16 17:12:46 +06:00 committed by GitHub
parent 458ba29ccf
commit 6d98f9e6b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -116,7 +116,7 @@ export class LiveQuery extends TxProcessor implements Client {
} }
} }
private match (q: Query, doc: Doc): boolean { private match (q: Query, doc: Doc, skipLookup = false): boolean {
if (!this.getHierarchy().isDerived(doc._class, q._class)) { if (!this.getHierarchy().isDerived(doc._class, q._class)) {
// Check if it is not a mixin and not match class // Check if it is not a mixin and not match class
const mixinClass = Hierarchy.mixinClass(doc) const mixinClass = Hierarchy.mixinClass(doc)
@ -127,6 +127,7 @@ export class LiveQuery extends TxProcessor implements Client {
const query = q.query const query = q.query
for (const key in query) { for (const key in query) {
if (key === '$search') continue if (key === '$search') continue
if (skipLookup && key.startsWith('$lookup')) continue
const value = (query as any)[key] const value = (query as any)[key]
const result = findProperty([doc], key, value) const result = findProperty([doc], key, value)
if (result.length === 0) { if (result.length === 0) {
@ -743,25 +744,31 @@ export class LiveQuery extends TxProcessor implements Client {
} }
private async handleDocAdd (q: Query, doc: Doc, handleLookup = true): Promise<void> { private async handleDocAdd (q: Query, doc: Doc, handleLookup = true): Promise<void> {
if (this.match(q, doc)) { if (this.match(q, doc, q.options?.lookup !== undefined)) {
let needPush = true
if (q.result instanceof Promise) { if (q.result instanceof Promise) {
q.result = await q.result q.result = await q.result
} }
if (q.options?.lookup !== undefined && handleLookup) { if (q.options?.lookup !== undefined && handleLookup) {
await this.lookup(q._class, doc, q.options.lookup) await this.lookup(q._class, doc, q.options.lookup)
const matched = this.match(q, doc)
if (!matched) needPush = false
} }
if (needPush) {
// We could already have document inside results, if query is created during processing of document create transaction and not yet handled on client. // We could already have document inside results, if query is created during processing of document create transaction and not yet handled on client.
const pos = q.result.findIndex((p) => p._id === doc._id) const pos = q.result.findIndex((p) => p._id === doc._id)
if (pos >= 0) { if (pos >= 0) {
// No need to update, document already in results. // No need to update, document already in results.
return needPush = false
} }
}
if (needPush) {
// If query contains search we must check use fulltext // If query contains search we must check use fulltext
if (q.query.$search != null && q.query.$search.length > 0) { if (q.query.$search != null && q.query.$search.length > 0) {
const match = await this.client.findOne(q._class, { $search: q.query.$search, _id: doc._id }, q.options) const match = await this.client.findOne(q._class, { $search: q.query.$search, _id: doc._id }, q.options)
if (match === undefined) return if (match === undefined) return
} }
q.result.push(doc) q.result.push(doc)
q.total++ q.total++
@ -777,6 +784,7 @@ export class LiveQuery extends TxProcessor implements Client {
await this.callback(q) await this.callback(q)
} }
} }
}
await this.handleDocAddLookup(q, doc) await this.handleDocAddLookup(q, doc)
} }