mirror of
https://github.com/hcengineering/platform.git
synced 2024-12-23 11:31:57 +03:00
Fix query update when refresh (#1006)
Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
parent
c88fa75208
commit
4c60b40eb3
@ -23,9 +23,13 @@ import core, {
|
|||||||
findProperty,
|
findProperty,
|
||||||
FindResult,
|
FindResult,
|
||||||
getObjectValue,
|
getObjectValue,
|
||||||
Hierarchy, Lookup,
|
Hierarchy,
|
||||||
|
Lookup,
|
||||||
LookupData,
|
LookupData,
|
||||||
ModelDb, Ref, resultSort, ReverseLookups,
|
ModelDb,
|
||||||
|
Ref,
|
||||||
|
resultSort,
|
||||||
|
ReverseLookups,
|
||||||
SortingQuery,
|
SortingQuery,
|
||||||
Tx,
|
Tx,
|
||||||
TxBulkWrite,
|
TxBulkWrite,
|
||||||
@ -174,7 +178,10 @@ export class LiveQuery extends TxProcessor implements Client {
|
|||||||
} else {
|
} else {
|
||||||
if (this.getHierarchy().isDerived(tx.mixin, q._class)) {
|
if (this.getHierarchy().isDerived(tx.mixin, q._class)) {
|
||||||
// Mixin potentially added to object we doesn't have in out results
|
// Mixin potentially added to object we doesn't have in out results
|
||||||
await this.refresh(q)
|
const doc = await this.findOne(q._class, { _id: tx.objectId }, q.options)
|
||||||
|
if (doc !== undefined) {
|
||||||
|
await this.handleDocAdd(q, doc)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -238,6 +245,15 @@ export class LiveQuery extends TxProcessor implements Client {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const updatedDoc = q.result[pos]
|
const updatedDoc = q.result[pos]
|
||||||
|
if (updatedDoc.modifiedOn > tx.modifiedOn) return
|
||||||
|
if (updatedDoc.modifiedOn === tx.modifiedOn) {
|
||||||
|
const current = await this.findOne(q._class, { _id: updatedDoc._id })
|
||||||
|
if (current !== undefined) {
|
||||||
|
q.result[pos] = current
|
||||||
|
} else {
|
||||||
|
q.result.splice(pos, 1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
await this.__updateDoc(q, updatedDoc, tx)
|
await this.__updateDoc(q, updatedDoc, tx)
|
||||||
if (!this.match(q, updatedDoc)) {
|
if (!this.match(q, updatedDoc)) {
|
||||||
q.result.splice(pos, 1)
|
q.result.splice(pos, 1)
|
||||||
@ -245,6 +261,7 @@ export class LiveQuery extends TxProcessor implements Client {
|
|||||||
q.result[pos] = updatedDoc
|
q.result[pos] = updatedDoc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
this.sort(q, tx)
|
this.sort(q, tx)
|
||||||
await this.callback(q.result[pos], q)
|
await this.callback(q.result[pos], q)
|
||||||
} else if (this.matchQuery(q, tx)) {
|
} else if (this.matchQuery(q, tx)) {
|
||||||
@ -310,9 +327,9 @@ export class LiveQuery extends TxProcessor implements Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async refresh (q: Query): Promise<void> {
|
private async refresh (q: Query): Promise<void> {
|
||||||
const res = await this.client.findAll(q._class, q.query, q.options)
|
q.result = this.client.findAll(q._class, q.query, q.options)
|
||||||
q.result = res
|
q.result = await q.result
|
||||||
q.callback(this.clone(res))
|
q.callback(this.clone(q.result))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if query is partially matched.
|
// Check if query is partially matched.
|
||||||
@ -331,7 +348,7 @@ export class LiveQuery extends TxProcessor implements Client {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getLookupValue<T extends Doc> (doc: T, lookup: Lookup<T>, result: LookupData<T>): Promise<void> {
|
private async getLookupValue<T extends Doc>(doc: T, lookup: Lookup<T>, result: LookupData<T>): Promise<void> {
|
||||||
for (const key in lookup) {
|
for (const key in lookup) {
|
||||||
if (key === '_id') {
|
if (key === '_id') {
|
||||||
await this.getReverseLookupValue(doc, lookup, result)
|
await this.getReverseLookupValue(doc, lookup, result)
|
||||||
@ -355,7 +372,11 @@ export class LiveQuery extends TxProcessor implements Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getReverseLookupValue<T extends Doc> (doc: T, lookup: ReverseLookups, result: LookupData<T>): Promise<void> {
|
private async getReverseLookupValue<T extends Doc>(
|
||||||
|
doc: T,
|
||||||
|
lookup: ReverseLookups,
|
||||||
|
result: LookupData<T>
|
||||||
|
): Promise<void> {
|
||||||
for (const key in lookup._id) {
|
for (const key in lookup._id) {
|
||||||
const value = lookup._id[key]
|
const value = lookup._id[key]
|
||||||
const objects = await this.findAll(value, { attachedTo: doc._id })
|
const objects = await this.findAll(value, { attachedTo: doc._id })
|
||||||
@ -363,7 +384,7 @@ export class LiveQuery extends TxProcessor implements Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async lookup<T extends Doc> (doc: T, lookup: Lookup<T>): Promise<void> {
|
private async lookup<T extends Doc>(doc: T, lookup: Lookup<T>): Promise<void> {
|
||||||
const result: LookupData<Doc> = {}
|
const result: LookupData<Doc> = {}
|
||||||
await this.getLookupValue(doc, lookup, result)
|
await this.getLookupValue(doc, lookup, result)
|
||||||
;(doc as WithLookup<Doc>).$lookup = result
|
;(doc as WithLookup<Doc>).$lookup = result
|
||||||
|
Loading…
Reference in New Issue
Block a user