diff --git a/packages/core/src/clone.ts b/packages/core/src/clone.ts index baa6437a0e..315b9ca88f 100644 --- a/packages/core/src/clone.ts +++ b/packages/core/src/clone.ts @@ -65,6 +65,10 @@ export function clone (obj: any, as?: (doc: any, m: any) => any, needAs?: (value } } } + if (typeOf === 'Object') { + const m = needAs?.(obj) + return m !== undefined && as !== undefined ? as(result, m) : result + } return result } else { return obj diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index d24b6ea13c..3e31e09716 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -27,6 +27,7 @@ import core, { IndexingUpdateEvent, Lookup, LookupData, + Mixin, ModelDb, Ref, ReverseLookups, @@ -150,6 +151,13 @@ export class LiveQuery implements WithTx, Client { } private match (q: Query, doc: Doc, skipLookup = false): boolean { + if (this.getHierarchy().isMixin(q._class)) { + if (this.getHierarchy().hasMixin(doc, q._class)) { + doc = this.getHierarchy().as(doc, q._class) + } else { + return false + } + } if (!this.getHierarchy().isDerived(doc._class, q._class)) { // Check if it is not a mixin and not match class const mixinClass = Hierarchy.mixinClass(doc) @@ -518,20 +526,30 @@ export class LiveQuery implements WithTx, Client { return current } + private asMixin (doc: Doc, mixin: Ref>): Doc { + if (this.getHierarchy().isMixin(mixin)) { + return this.getHierarchy().as(doc, mixin) + } + return doc + } + private async getCurrentDoc ( q: Query, _id: Ref, space: Ref, docCache: Map ): Promise { - const current = await this.getDocFromCache(docCache, _id, q._class, space, q) + let current = await this.getDocFromCache(docCache, _id, q._class, space, q) if (q.result instanceof Promise) { q.result = await q.result } const pos = q.result.findDoc(_id) + if (current !== undefined) { + current = this.asMixin(current, q._class) + } if (current !== undefined && this.match(q, current)) { - q.result.updateDoc(current) + q.result.updateDoc(current, false) this.refs.updateDocuments(q, [current]) } else { if (q.options?.limit === q.result.length) { @@ -543,7 +561,6 @@ export class LiveQuery implements WithTx, Client { if (q.options?.total === true) { q.total-- } - return true } } return false @@ -597,7 +614,7 @@ export class LiveQuery implements WithTx, Client { if (q.result instanceof Promise) { q.result = await q.result } - const updatedDoc = q.result.findDoc(tx.objectId) + let updatedDoc = q.result.findDoc(tx.objectId) if (updatedDoc !== undefined) { // If query contains search we must check use fulltext if (q.query.$search != null && q.query.$search.length > 0) { @@ -608,6 +625,7 @@ export class LiveQuery implements WithTx, Client { } else { if (updatedDoc.modifiedOn < tx.modifiedOn) { await this.__updateMixinDoc(q, updatedDoc, tx) + updatedDoc = this.asMixin(updatedDoc, q._class) const updateRefresh = this.checkUpdatedDocMatch(q, q.result, updatedDoc) if (updateRefresh) { continue