Skip to content

Commit a596f46

Browse files
authored
UBERF-7247: Fix queryFind for mixins on server (#5803)
Signed-off-by: Andrey Sobolev <[email protected]>
1 parent 74e8948 commit a596f46

File tree

3 files changed

+99
-13
lines changed

3 files changed

+99
-13
lines changed

packages/query/src/__tests__/minmodel.ts

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,20 @@
1313
// limitations under the License.
1414
//
1515

16-
import type { Account, Arr, Class, Data, Doc, Domain, Mixin, Obj, Ref, TxCreateDoc, TxCUD } from '@hcengineering/core'
16+
import type {
17+
Account,
18+
Arr,
19+
Class,
20+
Data,
21+
Doc,
22+
Domain,
23+
Mixin,
24+
Obj,
25+
Ref,
26+
Space,
27+
TxCreateDoc,
28+
TxCUD
29+
} from '@hcengineering/core'
1730
import core, { AccountRole, AttachedDoc, ClassifierKind, DOMAIN_MODEL, DOMAIN_TX, TxFactory } from '@hcengineering/core'
1831
import type { IntlString, Plugin } from '@hcengineering/platform'
1932
import { plugin } from '@hcengineering/platform'
@@ -54,16 +67,26 @@ export interface AttachedComment extends AttachedDoc {
5467
message: string
5568
}
5669

70+
interface TestProject extends Space {
71+
prjName: string
72+
}
73+
74+
interface TestProjectMixin extends TestProject {
75+
someField?: string
76+
}
77+
5778
/**
5879
* @public
5980
*/
6081
export const test = plugin('test' as Plugin, {
6182
mixin: {
62-
TestMixin: '' as Ref<Mixin<TestMixin>>
83+
TestMixin: '' as Ref<Mixin<TestMixin>>,
84+
TestProjectMixin: '' as Ref<Mixin<TestProjectMixin>>
6385
},
6486
class: {
6587
TestComment: '' as Ref<Class<AttachedComment>>,
66-
ParticipantsHolder: '' as Ref<Class<ParticipantsHolder>>
88+
ParticipantsHolder: '' as Ref<Class<ParticipantsHolder>>,
89+
TestProject: '' as Ref<Class<TestProject>>
6790
}
6891
})
6992

@@ -89,20 +112,28 @@ export function genMinModel (): TxCUD<Doc>[] {
89112
createClass(core.class.Doc, { label: 'Doc' as IntlString, extends: core.class.Obj, kind: ClassifierKind.CLASS })
90113
)
91114
txes.push(
92-
createClass(core.class.AttachedDoc, {
93-
label: 'AttachedDoc' as IntlString,
115+
createClass(core.class.Class, {
116+
label: 'Class' as IntlString,
94117
extends: core.class.Doc,
95-
kind: ClassifierKind.MIXIN
118+
kind: ClassifierKind.CLASS,
119+
domain: DOMAIN_MODEL
96120
})
97121
)
98122
txes.push(
99-
createClass(core.class.Class, {
100-
label: 'Class' as IntlString,
101-
extends: core.class.Doc,
123+
createClass(core.class.Mixin, {
124+
label: 'Mixin' as IntlString,
125+
extends: core.class.Class,
102126
kind: ClassifierKind.CLASS,
103127
domain: DOMAIN_MODEL
104128
})
105129
)
130+
txes.push(
131+
createClass(core.class.AttachedDoc, {
132+
label: 'AttachedDoc' as IntlString,
133+
extends: core.class.Doc,
134+
kind: ClassifierKind.MIXIN
135+
})
136+
)
106137
txes.push(
107138
createClass(core.class.Space, {
108139
label: 'Space' as IntlString,
@@ -164,6 +195,13 @@ export function genMinModel (): TxCUD<Doc>[] {
164195
kind: ClassifierKind.CLASS
165196
})
166197
)
198+
txes.push(
199+
createClass(core.class.TxMixin, {
200+
label: 'TxMixin' as IntlString,
201+
extends: core.class.TxCUD,
202+
kind: ClassifierKind.CLASS
203+
})
204+
)
167205

168206
txes.push(
169207
createClass(test.mixin.TestMixin, {
@@ -173,6 +211,23 @@ export function genMinModel (): TxCUD<Doc>[] {
173211
})
174212
)
175213

214+
txes.push(
215+
createClass(test.class.TestProject, {
216+
label: 'TestProject' as IntlString,
217+
extends: core.class.Space,
218+
kind: ClassifierKind.CLASS,
219+
domain: DOMAIN_TEST
220+
})
221+
)
222+
223+
txes.push(
224+
createClass(test.mixin.TestProjectMixin, {
225+
label: 'TestProjectMixin' as IntlString,
226+
extends: test.class.TestProject,
227+
kind: ClassifierKind.MIXIN
228+
})
229+
)
230+
176231
txes.push(
177232
createClass(test.class.TestComment, {
178233
label: 'TestComment' as IntlString,

packages/query/src/__tests__/query.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,4 +942,31 @@ describe('query', () => {
942942
const result = await resolveP
943943
expect(result.length).toEqual(2)
944944
})
945+
946+
it('check query mixin projection', async () => {
947+
const { liveQuery, factory } = await getClient()
948+
949+
let projects = await liveQuery.queryFind(test.mixin.TestProjectMixin, {}, { projection: { _id: 1 } })
950+
expect(projects.length).toEqual(0)
951+
const project = await factory.createDoc(test.class.TestProject, core.space.Space, {
952+
archived: false,
953+
description: '',
954+
members: [],
955+
private: false,
956+
prjName: 'test project',
957+
name: 'qwe'
958+
})
959+
960+
projects = await liveQuery.queryFind(test.mixin.TestProjectMixin, {}, { projection: { _id: 1 } })
961+
expect(projects.length).toEqual(0)
962+
await factory.createMixin(project, test.class.TestProject, core.space.Space, test.mixin.TestProjectMixin, {
963+
someField: 'qwe'
964+
})
965+
// We need to process all events before we could do query again
966+
await new Promise<void>((resolve) => {
967+
setTimeout(resolve, 100)
968+
})
969+
projects = await liveQuery.queryFind(test.mixin.TestProjectMixin, {}, { projection: { _id: 1 } })
970+
expect(projects.length).toEqual(1)
971+
})
945972
})

server/core/src/server/storage.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,13 @@ export class TServerStorage implements ServerStorage {
146146
findOne: async (_class, query, options) => {
147147
return (
148148
await metrics.with('query', {}, async (ctx) => {
149-
return await this.findAll(ctx, _class, query, { ...options, limit: 1 })
149+
const results = await this.findAll(ctx, _class, query, { ...options, limit: 1 })
150+
return toFindResult(
151+
results.map((v) => {
152+
return this.hierarchy.updateLookupMixin(_class, v, options)
153+
}),
154+
results.total
155+
)
150156
})
151157
)[0]
152158
},
@@ -232,9 +238,7 @@ export class TServerStorage implements ServerStorage {
232238
const r = await ctx.with('adapter-tx', { domain: lastDomain }, async (ctx) => await adapter.tx(ctx, ...part))
233239

234240
// Update server live queries.
235-
for (const t of part) {
236-
await this.liveQuery.tx(t)
237-
}
241+
await this.liveQuery.tx(...part)
238242
if (Array.isArray(r)) {
239243
result.push(...r)
240244
} else {

0 commit comments

Comments
 (0)