Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix query mixins #7434

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/src/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import core, {
IndexingUpdateEvent,
Lookup,
LookupData,
Mixin,
ModelDb,
Ref,
ReverseLookups,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -518,20 +526,30 @@ export class LiveQuery implements WithTx, Client {
return current
}

private asMixin (doc: Doc, mixin: Ref<Mixin<Doc>>): Doc {
if (this.getHierarchy().isMixin(mixin)) {
return this.getHierarchy().as(doc, mixin)
}
return doc
}

private async getCurrentDoc (
q: Query,
_id: Ref<Doc>,
space: Ref<Space>,
docCache: Map<string, Doc>
): Promise<boolean> {
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) {
Expand All @@ -543,7 +561,6 @@ export class LiveQuery implements WithTx, Client {
if (q.options?.total === true) {
q.total--
}
return true
}
}
return false
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -663,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)
Expand Down
Loading