From 7ddb8ca4c254354296f1314dbc6a6a8d6d778d92 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Wed, 11 Dec 2024 22:57:32 +0500 Subject: [PATCH 1/4] Fix query mixins Signed-off-by: Denis Bykhov --- packages/core/src/clone.ts | 4 ++++ packages/query/src/index.ts | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/core/src/clone.ts b/packages/core/src/clone.ts index baa6437a0e0..315b9ca88fa 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 d24b6ea13cd..68d7f85ea8a 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,7 @@ export class LiveQuery implements WithTx, Client { } private match (q: Query, doc: Doc, skipLookup = false): boolean { + doc = this.asMixin(doc, q._class) 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 +520,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) { @@ -597,7 +609,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 +620,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 From d334d57f7842d03785ec856d006cfe677d382082 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Wed, 11 Dec 2024 23:45:57 +0500 Subject: [PATCH 2/4] Fix Signed-off-by: Denis Bykhov --- packages/query/src/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 68d7f85ea8a..f2ad29e595c 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -151,7 +151,13 @@ export class LiveQuery implements WithTx, Client { } private match (q: Query, doc: Doc, skipLookup = false): boolean { - doc = this.asMixin(doc, q._class) + 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) From 2b60b24759916de1abe6991e70e7847b826164d6 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Thu, 12 Dec 2024 01:46:20 +0500 Subject: [PATCH 3/4] Fix Signed-off-by: Denis Bykhov --- packages/query/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index f2ad29e595c..5a136a253b0 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -561,7 +561,6 @@ export class LiveQuery implements WithTx, Client { if (q.options?.total === true) { q.total-- } - return true } } return false @@ -682,6 +681,7 @@ export class LiveQuery implements WithTx, Client { } const updatedDoc = q.result.findDoc(tx.objectId) if (updatedDoc !== undefined) { + debugger // If query contains search we must check use fulltext if (q.query.$search != null && q.query.$search.length > 0) { const searchRefresh = await this.checkSearch(q, tx.objectId) From 659d192109d7568d8656641ee7f2977e2e16c426 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Thu, 12 Dec 2024 09:57:39 +0500 Subject: [PATCH 4/4] Remove debugger Signed-off-by: Denis Bykhov --- packages/query/src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 5a136a253b0..3e31e09716e 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -681,7 +681,6 @@ export class LiveQuery implements WithTx, Client { } const updatedDoc = q.result.findDoc(tx.objectId) if (updatedDoc !== undefined) { - debugger // If query contains search we must check use fulltext if (q.query.$search != null && q.query.$search.length > 0) { const searchRefresh = await this.checkSearch(q, tx.objectId)