2021-11-26 11:05:18 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2021 Anticrm Platform Contributors.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
import core, {
|
|
|
|
createClient,
|
|
|
|
Doc,
|
|
|
|
generateId,
|
|
|
|
Ref,
|
|
|
|
SortingOrder,
|
|
|
|
Space,
|
|
|
|
Tx,
|
|
|
|
TxCreateDoc,
|
|
|
|
TxOperations,
|
|
|
|
WithLookup
|
|
|
|
} from '@anticrm/core'
|
2021-11-26 11:05:18 +00:00
|
|
|
import { LiveQuery } from '..'
|
|
|
|
import { connect } from './connection'
|
2022-03-23 09:03:41 +00:00
|
|
|
import { AttachedComment, genMinModel, ParticipantsHolder, test } from './minmodel'
|
2021-11-26 11:05:18 +00:00
|
|
|
|
|
|
|
interface Channel extends Space {
|
|
|
|
x: number
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
async function getClient (): Promise<{ liveQuery: LiveQuery, factory: TxOperations }> {
|
2021-11-26 11:05:18 +00:00
|
|
|
const storage = await createClient(connect)
|
|
|
|
const liveQuery = new LiveQuery(storage)
|
|
|
|
storage.notify = (tx: Tx) => {
|
2022-04-29 05:27:17 +00:00
|
|
|
liveQuery.tx(tx).catch((err) => console.log(err))
|
2021-11-26 11:05:18 +00:00
|
|
|
}
|
|
|
|
return { liveQuery, factory: new TxOperations(storage, core.account.System) }
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('query', () => {
|
|
|
|
it('findAll', async () => {
|
|
|
|
const { liveQuery } = await getClient()
|
|
|
|
const result = await liveQuery.findAll<Space>(core.class.Space, {})
|
|
|
|
expect(result).toHaveLength(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('query with param', async () => {
|
|
|
|
const { liveQuery } = await getClient()
|
|
|
|
|
|
|
|
let expectedLength = 0
|
|
|
|
const txes = genMinModel()
|
|
|
|
for (let i = 0; i < txes.length; i++) {
|
|
|
|
if (liveQuery.getHierarchy().isDerived((txes[i] as TxCreateDoc<Doc>).objectClass, core.class.Space)) {
|
|
|
|
expectedLength++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:03:41 +00:00
|
|
|
const result = await new Promise((resolve) => {
|
2021-11-26 11:05:18 +00:00
|
|
|
liveQuery.query<Space>(core.class.Space, { private: false }, (result) => {
|
2022-03-23 09:03:41 +00:00
|
|
|
resolve(result)
|
2021-11-26 11:05:18 +00:00
|
|
|
})
|
|
|
|
})
|
2022-03-23 09:03:41 +00:00
|
|
|
expect(result).toHaveLength(expectedLength)
|
2021-11-26 11:05:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('query should be live', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
let expectedLength = 0
|
|
|
|
const txes = genMinModel()
|
|
|
|
for (let i = 0; i < txes.length; i++) {
|
|
|
|
if (liveQuery.getHierarchy().isDerived((txes[i] as TxCreateDoc<Doc>).objectClass, core.class.Space)) {
|
|
|
|
expectedLength++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let attempt = 0
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<Space>(core.class.Space, { private: false }, (result) => {
|
|
|
|
expect(result).toHaveLength(expectedLength + attempt)
|
|
|
|
if (attempt > 0) {
|
|
|
|
expect((result[expectedLength + attempt - 1] as any).x).toBe(attempt)
|
|
|
|
}
|
|
|
|
if (attempt++ === 3) {
|
|
|
|
// check underlying storage received all data.
|
|
|
|
liveQuery
|
|
|
|
.findAll<Space>(core.class.Space, { private: false })
|
|
|
|
.then((result) => {
|
|
|
|
expect(result).toHaveLength(expectedLength + attempt - 1)
|
|
|
|
resolve(null)
|
|
|
|
})
|
|
|
|
.catch((err) => expect(err).toBeUndefined())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
await factory.createDoc(core.class.Account, core.space.Model, {
|
|
|
|
email: 'user1@site.com'
|
|
|
|
})
|
|
|
|
await factory.createDoc<Channel>(core.class.Space, core.space.Model, {
|
|
|
|
private: true,
|
|
|
|
name: '#0',
|
|
|
|
description: '',
|
|
|
|
members: [],
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
x: 0
|
|
|
|
})
|
|
|
|
await factory.createDoc<Channel>(core.class.Space, core.space.Model, {
|
|
|
|
private: false,
|
|
|
|
name: '#1',
|
|
|
|
description: '',
|
|
|
|
members: [],
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
x: 1
|
|
|
|
})
|
|
|
|
await factory.createDoc<Channel>(core.class.Space, core.space.Model, {
|
|
|
|
private: false,
|
|
|
|
name: '#2',
|
|
|
|
description: '',
|
|
|
|
members: [],
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
x: 2
|
|
|
|
})
|
|
|
|
await factory.createDoc<Channel>(core.class.Space, core.space.Model, {
|
|
|
|
private: false,
|
|
|
|
name: '#3',
|
|
|
|
description: '',
|
|
|
|
members: [],
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
x: 3
|
|
|
|
})
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('unsubscribe query', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
let expectedLength = 0
|
|
|
|
const txes = genMinModel()
|
|
|
|
for (let i = 0; i < txes.length; i++) {
|
|
|
|
if (liveQuery.getHierarchy().isDerived((txes[i] as TxCreateDoc<Doc>).objectClass, core.class.Space)) {
|
|
|
|
expectedLength++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsubscribe = liveQuery.query<Space>(core.class.Space, { private: false }, (result) => {
|
|
|
|
expect(result).toHaveLength(expectedLength)
|
|
|
|
})
|
|
|
|
|
|
|
|
unsubscribe()
|
|
|
|
|
|
|
|
await factory.createDoc(core.class.Space, core.space.Model, {
|
|
|
|
private: false,
|
|
|
|
name: '#1',
|
|
|
|
description: '',
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
members: []
|
|
|
|
})
|
|
|
|
await factory.createDoc(core.class.Space, core.space.Model, {
|
|
|
|
private: false,
|
|
|
|
name: '#2',
|
|
|
|
description: '',
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
members: []
|
|
|
|
})
|
|
|
|
await factory.createDoc(core.class.Space, core.space.Model, {
|
|
|
|
private: false,
|
|
|
|
name: '#3',
|
|
|
|
description: '',
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
members: []
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('query against core client', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
const expectedLength = 2
|
|
|
|
let attempt = 0
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<Space>(core.class.Space, { private: false }, (result) => {
|
|
|
|
expect(result).toHaveLength(expectedLength + attempt)
|
|
|
|
if (attempt > 0) {
|
|
|
|
expect((result[expectedLength + attempt - 1] as any).x).toBe(attempt)
|
|
|
|
}
|
|
|
|
if (attempt++ === 1) resolve(null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
await factory.createDoc<Channel>(core.class.Space, core.space.Model, {
|
|
|
|
x: 1,
|
|
|
|
private: false,
|
|
|
|
name: '#1',
|
|
|
|
description: '',
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
members: []
|
|
|
|
})
|
|
|
|
await factory.createDoc<Channel>(core.class.Space, core.space.Model, {
|
|
|
|
x: 2,
|
|
|
|
private: false,
|
|
|
|
name: '#2',
|
|
|
|
description: '',
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
members: []
|
|
|
|
})
|
|
|
|
await factory.createDoc<Channel>(core.class.Space, core.space.Model, {
|
|
|
|
x: 3,
|
|
|
|
private: false,
|
|
|
|
name: '#3',
|
|
|
|
description: '',
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
members: []
|
|
|
|
})
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('limit and sorting', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
const limit = 1
|
2022-01-31 09:06:30 +00:00
|
|
|
let attempt = 0
|
|
|
|
let descAttempt = 0
|
2021-11-26 11:05:18 +00:00
|
|
|
|
|
|
|
const pp1 = new Promise((resolve) => {
|
|
|
|
liveQuery.query<Space>(
|
|
|
|
core.class.Space,
|
|
|
|
{ private: true },
|
|
|
|
(result) => {
|
2022-01-31 09:06:30 +00:00
|
|
|
if (result.length > 0) {
|
2021-11-26 11:05:18 +00:00
|
|
|
expect(result.length).toEqual(limit)
|
|
|
|
expect(result[0].name).toMatch('0')
|
2022-01-31 09:06:30 +00:00
|
|
|
attempt++
|
2021-11-26 11:05:18 +00:00
|
|
|
}
|
2022-01-31 09:06:30 +00:00
|
|
|
if (attempt === 1) resolve(null)
|
2021-11-26 11:05:18 +00:00
|
|
|
},
|
|
|
|
{ limit: limit, sort: { name: SortingOrder.Ascending } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
const pp2 = new Promise((resolve) => {
|
|
|
|
liveQuery.query<Space>(
|
|
|
|
core.class.Space,
|
|
|
|
{ private: true },
|
|
|
|
(result) => {
|
2022-01-31 09:06:30 +00:00
|
|
|
if (result.length > 0) {
|
2021-11-26 11:05:18 +00:00
|
|
|
expect(result.length).toEqual(limit)
|
2022-01-31 09:06:30 +00:00
|
|
|
expect(result[0].name).toMatch(descAttempt.toString())
|
|
|
|
descAttempt++
|
2021-11-26 11:05:18 +00:00
|
|
|
}
|
2022-01-31 09:06:30 +00:00
|
|
|
if (descAttempt === 10) resolve(null)
|
2021-11-26 11:05:18 +00:00
|
|
|
},
|
|
|
|
{ limit: limit, sort: { name: SortingOrder.Descending } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
await factory.createDoc(core.class.Space, core.space.Model, {
|
|
|
|
private: true,
|
|
|
|
name: i.toString(),
|
|
|
|
description: '',
|
2021-12-21 09:08:22 +00:00
|
|
|
archived: false,
|
2021-11-26 11:05:18 +00:00
|
|
|
members: []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await Promise.all([pp1, pp2])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('remove', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
const expectedLength = 2
|
|
|
|
let attempt = 0
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<Space>(core.class.Space, { private: false }, (result) => {
|
|
|
|
expect(result).toHaveLength(expectedLength - attempt)
|
|
|
|
if (attempt++ === expectedLength) resolve(null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const spaces = await liveQuery.findAll(core.class.Space, {})
|
|
|
|
for (const space of spaces) {
|
|
|
|
await factory.removeDoc(space._class, space.space, space._id)
|
|
|
|
}
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('remove with limit', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
const expectedLength = 2
|
|
|
|
let attempt = 0
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<Space>(
|
|
|
|
core.class.Space,
|
|
|
|
{ private: false },
|
|
|
|
(result) => {
|
|
|
|
expect(result).toHaveLength(attempt++ === expectedLength ? 0 : 1)
|
|
|
|
if (attempt === expectedLength) resolve(null)
|
|
|
|
},
|
|
|
|
{ limit: 1 }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
const spaces = await liveQuery.findAll(core.class.Space, {})
|
|
|
|
for (const space of spaces) {
|
|
|
|
await factory.removeDoc(space._class, space.space, space._id)
|
|
|
|
}
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('update', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
const spaces = await liveQuery.findAll(core.class.Space, {})
|
|
|
|
let attempt = 0
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<Space>(
|
|
|
|
core.class.Space,
|
|
|
|
{ private: false },
|
|
|
|
(result) => {
|
|
|
|
if (attempt > 0) {
|
|
|
|
expect(result[attempt - 1].name === attempt.toString())
|
|
|
|
expect(result[attempt - 1].members.length === 1)
|
|
|
|
if (attempt === spaces.length) resolve(null)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ sort: { private: SortingOrder.Ascending } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
for (const space of spaces) {
|
|
|
|
attempt++
|
|
|
|
await factory.updateDoc(space._class, space.space, space._id, {
|
|
|
|
name: attempt.toString(),
|
|
|
|
$push: { members: core.account.System }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('update with no match query', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
const spaces = await liveQuery.findAll(core.class.Space, {})
|
|
|
|
let attempt = 0
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<Space>(
|
|
|
|
core.class.Space,
|
|
|
|
{ private: false },
|
|
|
|
(result) => {
|
|
|
|
if (attempt > 0) {
|
|
|
|
expect(result.length === spaces.length - attempt)
|
|
|
|
if (attempt === spaces.length) resolve(null)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ sort: { private: SortingOrder.Ascending } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
for (const space of spaces) {
|
|
|
|
attempt++
|
|
|
|
await factory.updateDoc(space._class, space.space, space._id, {
|
|
|
|
private: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
2022-01-31 09:06:30 +00:00
|
|
|
it('lookup query add doc', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
const futureSpace: Space = {
|
|
|
|
_id: generateId(),
|
|
|
|
_class: core.class.Space,
|
|
|
|
private: false,
|
|
|
|
members: [],
|
|
|
|
space: core.space.Model,
|
|
|
|
name: 'new space',
|
|
|
|
description: '',
|
|
|
|
archived: false,
|
|
|
|
modifiedBy: core.account.System,
|
|
|
|
modifiedOn: 0
|
|
|
|
}
|
2022-04-29 05:27:17 +00:00
|
|
|
const comment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
futureSpace._id,
|
|
|
|
futureSpace._id,
|
|
|
|
core.class.Space,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'test'
|
|
|
|
}
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
let attempt = 0
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<AttachedComment>(
|
|
|
|
test.class.TestComment,
|
|
|
|
{ _id: comment },
|
|
|
|
(result) => {
|
|
|
|
const comment = result[0]
|
|
|
|
if (comment !== undefined) {
|
|
|
|
if (attempt > 0) {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect(comment.$lookup?.space?._id).toEqual(futureSpace._id)
|
2022-01-31 09:06:30 +00:00
|
|
|
resolve(null)
|
|
|
|
} else {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect(comment.$lookup?.space).toBeUndefined()
|
2022-01-31 09:06:30 +00:00
|
|
|
attempt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ lookup: { space: core.class.Space } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
await factory.createDoc(
|
|
|
|
core.class.Space,
|
|
|
|
futureSpace.space,
|
|
|
|
{
|
|
|
|
...futureSpace
|
|
|
|
},
|
|
|
|
futureSpace._id
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('lookup nested query add doc', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
const futureSpace: Space = {
|
|
|
|
_id: generateId(),
|
|
|
|
_class: core.class.Space,
|
|
|
|
private: false,
|
|
|
|
members: [],
|
|
|
|
space: core.space.Model,
|
|
|
|
name: 'new space',
|
|
|
|
description: '',
|
|
|
|
archived: false,
|
|
|
|
modifiedBy: core.account.System,
|
|
|
|
modifiedOn: 0
|
|
|
|
}
|
2022-04-29 05:27:17 +00:00
|
|
|
const comment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
futureSpace._id,
|
|
|
|
futureSpace._id,
|
|
|
|
core.class.Space,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'test'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const childComment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
futureSpace._id,
|
|
|
|
comment,
|
|
|
|
test.class.TestComment,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'child'
|
|
|
|
}
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
let attempt = 0
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<AttachedComment>(
|
|
|
|
test.class.TestComment,
|
|
|
|
{ _id: childComment },
|
|
|
|
(result) => {
|
|
|
|
const comment = result[0]
|
|
|
|
if (comment !== undefined) {
|
2022-06-06 05:42:51 +00:00
|
|
|
if (attempt++ > 0) {
|
2022-04-29 05:27:17 +00:00
|
|
|
expect((comment.$lookup?.attachedTo as WithLookup<AttachedComment>)?.$lookup?.space?._id).toEqual(
|
|
|
|
futureSpace._id
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
resolve(null)
|
|
|
|
} else {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect((comment.$lookup?.attachedTo as WithLookup<AttachedComment>)?.$lookup?.space).toBeUndefined()
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ lookup: { attachedTo: [test.class.TestComment, { space: core.class.Space }] } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
await factory.createDoc(
|
|
|
|
core.class.Space,
|
|
|
|
futureSpace.space,
|
|
|
|
{
|
|
|
|
...futureSpace
|
|
|
|
},
|
|
|
|
futureSpace._id
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('lookup reverse query add doc', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
const spaces = await liveQuery.findAll(core.class.Space, {})
|
2022-04-29 05:27:17 +00:00
|
|
|
const parentComment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
spaces[0]._id,
|
|
|
|
spaces[0]._id,
|
|
|
|
spaces[0]._class,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'test'
|
|
|
|
}
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
let attempt = 0
|
|
|
|
const childLength = 3
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<AttachedComment>(
|
|
|
|
test.class.TestComment,
|
|
|
|
{ _id: parentComment },
|
|
|
|
(result) => {
|
|
|
|
const comment = result[0]
|
|
|
|
if (comment !== undefined) {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect((comment.$lookup as any)?.comments).toHaveLength(attempt++)
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
if (attempt === childLength) {
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ lookup: { _id: { comments: test.class.TestComment } } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let index = 0; index < childLength; index++) {
|
2022-04-29 05:27:17 +00:00
|
|
|
await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
spaces[0]._id,
|
|
|
|
parentComment,
|
|
|
|
test.class.TestComment,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: index.toString()
|
|
|
|
}
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('lookup query remove doc', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
const futureSpace = await factory.createDoc(core.class.Space, core.space.Model, {
|
|
|
|
name: 'new space',
|
|
|
|
description: '',
|
|
|
|
archived: false,
|
|
|
|
private: false,
|
|
|
|
members: []
|
|
|
|
})
|
2022-04-29 05:27:17 +00:00
|
|
|
const comment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
futureSpace,
|
|
|
|
futureSpace,
|
|
|
|
core.class.Space,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'test'
|
|
|
|
}
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
let attempt = 0
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<AttachedComment>(
|
|
|
|
test.class.TestComment,
|
|
|
|
{ _id: comment },
|
|
|
|
(result) => {
|
|
|
|
const comment = result[0]
|
|
|
|
if (comment !== undefined) {
|
|
|
|
if (attempt > 0) {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect(comment.$lookup?.space).toBeUndefined()
|
2022-01-31 09:06:30 +00:00
|
|
|
resolve(null)
|
|
|
|
} else {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect((comment.$lookup?.space as Doc)?._id).toEqual(futureSpace)
|
2022-01-31 09:06:30 +00:00
|
|
|
attempt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ lookup: { space: core.class.Space } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
await factory.removeDoc(core.class.Space, core.space.Model, futureSpace)
|
|
|
|
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('lookup nested query remove doc', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
const futureSpace = await factory.createDoc(core.class.Space, core.space.Model, {
|
|
|
|
name: 'new space',
|
|
|
|
description: '',
|
|
|
|
archived: false,
|
|
|
|
private: false,
|
|
|
|
members: []
|
|
|
|
})
|
2022-04-29 05:27:17 +00:00
|
|
|
const comment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
futureSpace,
|
|
|
|
futureSpace,
|
|
|
|
core.class.Space,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'test'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const childComment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
futureSpace,
|
|
|
|
comment,
|
|
|
|
test.class.TestComment,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'child'
|
|
|
|
}
|
|
|
|
)
|
2022-06-06 05:42:51 +00:00
|
|
|
let attempt = -1
|
2022-01-31 09:06:30 +00:00
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<AttachedComment>(
|
|
|
|
test.class.TestComment,
|
|
|
|
{ _id: childComment },
|
|
|
|
(result) => {
|
2022-06-06 05:42:51 +00:00
|
|
|
attempt++
|
2022-01-31 09:06:30 +00:00
|
|
|
const comment = result[0]
|
|
|
|
if (comment !== undefined) {
|
|
|
|
if (attempt > 0) {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect((comment.$lookup?.attachedTo as WithLookup<AttachedComment>)?.$lookup?.space).toBeUndefined()
|
2022-01-31 09:06:30 +00:00
|
|
|
resolve(null)
|
|
|
|
} else {
|
2022-04-29 05:27:17 +00:00
|
|
|
expect(
|
|
|
|
((comment.$lookup?.attachedTo as WithLookup<AttachedComment>)?.$lookup?.space as Doc)?._id
|
|
|
|
).toEqual(futureSpace)
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ lookup: { attachedTo: [test.class.TestComment, { space: core.class.Space }] } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
await factory.removeDoc(core.class.Space, core.space.Model, futureSpace)
|
|
|
|
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('lookup reverse query remove doc', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
const spaces = await liveQuery.findAll(core.class.Space, {})
|
|
|
|
const comments = await liveQuery.findAll(test.class.TestComment, {})
|
|
|
|
expect(comments).toHaveLength(0)
|
2022-04-29 05:27:17 +00:00
|
|
|
const parentComment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
spaces[0]._id,
|
|
|
|
spaces[0]._id,
|
|
|
|
spaces[0]._class,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'test'
|
|
|
|
}
|
|
|
|
)
|
2022-06-06 05:42:51 +00:00
|
|
|
let attempt = -1
|
2022-01-31 09:06:30 +00:00
|
|
|
const childLength = 3
|
|
|
|
const childs: Ref<AttachedComment>[] = []
|
|
|
|
for (let index = 0; index < childLength; index++) {
|
2022-04-29 05:27:17 +00:00
|
|
|
childs.push(
|
|
|
|
await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
spaces[0]._id,
|
|
|
|
parentComment,
|
|
|
|
test.class.TestComment,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: index.toString()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<AttachedComment>(
|
|
|
|
test.class.TestComment,
|
|
|
|
{ _id: parentComment },
|
|
|
|
(result) => {
|
2022-06-06 05:42:51 +00:00
|
|
|
attempt++
|
2022-01-31 09:06:30 +00:00
|
|
|
const comment = result[0]
|
|
|
|
if (comment !== undefined) {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect((comment.$lookup as any)?.comments).toHaveLength(childLength - attempt)
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
if (attempt === childLength) {
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ lookup: { _id: { comments: test.class.TestComment } } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
for (const child of childs) {
|
2022-04-29 05:27:17 +00:00
|
|
|
await factory.removeCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
spaces[0]._id,
|
|
|
|
child,
|
|
|
|
parentComment,
|
|
|
|
test.class.TestComment,
|
|
|
|
'comments'
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('lookup query update doc', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
let attempt = 0
|
|
|
|
const futureSpace = await factory.createDoc(core.class.Space, core.space.Model, {
|
|
|
|
name: '0',
|
|
|
|
description: '',
|
|
|
|
archived: false,
|
|
|
|
private: false,
|
|
|
|
members: []
|
|
|
|
})
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
const comment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
futureSpace,
|
|
|
|
futureSpace,
|
|
|
|
core.class.Space,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'test'
|
|
|
|
}
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<AttachedComment>(
|
|
|
|
test.class.TestComment,
|
|
|
|
{ _id: comment },
|
|
|
|
(result) => {
|
|
|
|
const comment = result[0]
|
|
|
|
if (comment !== undefined) {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect((comment.$lookup?.space as Space).name).toEqual(attempt.toString())
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
if (attempt > 0) {
|
|
|
|
resolve(null)
|
|
|
|
} else {
|
|
|
|
attempt++
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ lookup: { space: core.class.Space } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
await factory.updateDoc(core.class.Space, core.space.Model, futureSpace, {
|
|
|
|
name: '1'
|
|
|
|
})
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('lookup nested query update doc', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
2022-06-06 05:42:51 +00:00
|
|
|
let attempt = -1
|
2022-01-31 09:06:30 +00:00
|
|
|
const futureSpace = await factory.createDoc(core.class.Space, core.space.Model, {
|
|
|
|
name: '0',
|
|
|
|
description: '',
|
|
|
|
archived: false,
|
|
|
|
private: false,
|
|
|
|
members: []
|
|
|
|
})
|
2022-04-29 05:27:17 +00:00
|
|
|
const comment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
futureSpace,
|
|
|
|
futureSpace,
|
|
|
|
core.class.Space,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'test'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const childComment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
futureSpace,
|
|
|
|
comment,
|
|
|
|
test.class.TestComment,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'child'
|
|
|
|
}
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<AttachedComment>(
|
|
|
|
test.class.TestComment,
|
|
|
|
{ _id: childComment },
|
|
|
|
(result) => {
|
2022-06-06 05:42:51 +00:00
|
|
|
attempt++
|
2022-01-31 09:06:30 +00:00
|
|
|
const comment = result[0]
|
|
|
|
if (comment !== undefined) {
|
2022-04-29 05:27:17 +00:00
|
|
|
expect(
|
|
|
|
((comment.$lookup?.attachedTo as WithLookup<AttachedComment>)?.$lookup?.space as Space).name
|
|
|
|
).toEqual(attempt.toString())
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
if (attempt > 0) {
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ lookup: { attachedTo: [test.class.TestComment, { space: core.class.Space }] } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
await factory.updateDoc(core.class.Space, core.space.Model, futureSpace, {
|
|
|
|
name: '1'
|
|
|
|
})
|
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
|
|
|
it('lookup reverse query update doc', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
const spaces = await liveQuery.findAll(core.class.Space, {})
|
2022-04-29 05:27:17 +00:00
|
|
|
const parentComment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
spaces[0]._id,
|
|
|
|
spaces[0]._id,
|
|
|
|
spaces[0]._class,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: 'test'
|
|
|
|
}
|
|
|
|
)
|
2022-06-06 05:42:51 +00:00
|
|
|
let attempt = -1
|
2022-04-29 05:27:17 +00:00
|
|
|
const childComment = await factory.addCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
spaces[0]._id,
|
|
|
|
parentComment,
|
|
|
|
test.class.TestComment,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: '0'
|
|
|
|
}
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
const pp = new Promise((resolve) => {
|
|
|
|
liveQuery.query<AttachedComment>(
|
|
|
|
test.class.TestComment,
|
|
|
|
{ _id: parentComment },
|
|
|
|
(result) => {
|
2022-06-06 05:42:51 +00:00
|
|
|
attempt++
|
2022-01-31 09:06:30 +00:00
|
|
|
const comment = result[0]
|
|
|
|
if (comment !== undefined) {
|
2022-04-14 16:55:56 +00:00
|
|
|
expect(((comment.$lookup as any)?.comments[0] as AttachedComment).message).toEqual(attempt.toString())
|
2022-01-31 09:06:30 +00:00
|
|
|
}
|
|
|
|
if (attempt > 0) {
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ lookup: { _id: { comments: test.class.TestComment } } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
await factory.updateCollection(
|
|
|
|
test.class.TestComment,
|
|
|
|
spaces[0]._id,
|
|
|
|
childComment,
|
|
|
|
parentComment,
|
|
|
|
test.class.TestComment,
|
|
|
|
'comments',
|
|
|
|
{
|
|
|
|
message: '1'
|
|
|
|
}
|
|
|
|
)
|
2022-01-31 09:06:30 +00:00
|
|
|
await pp
|
|
|
|
})
|
|
|
|
|
2021-11-26 11:05:18 +00:00
|
|
|
// it('update with over limit', async () => {
|
|
|
|
// const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
// const spaces = await liveQuery.findAll(core.class.Space, {})
|
|
|
|
// let attempt = 0
|
|
|
|
// const pp = new Promise((resolve) => {
|
|
|
|
// liveQuery.query<Space>(
|
|
|
|
// core.class.Space,
|
|
|
|
// {},
|
|
|
|
// (result) => {
|
|
|
|
// expect(result[0].name).toEqual(`Sp${++attempt}`)
|
|
|
|
// if (attempt === spaces.length + 1) resolve(null)
|
|
|
|
// },
|
|
|
|
// { sort: { name: SortingOrder.Ascending }, limit: 1 }
|
|
|
|
// )
|
|
|
|
// })
|
|
|
|
|
|
|
|
// for (let index = 0; index < spaces.length; index++) {
|
|
|
|
// const space = spaces[index]
|
|
|
|
// await factory.updateDoc(space._class, space.space, space._id, {
|
|
|
|
// name: `Sp${index + spaces.length + 1}`
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// await pp
|
|
|
|
// })
|
2022-03-23 09:03:41 +00:00
|
|
|
|
|
|
|
it('update-array-value', async () => {
|
|
|
|
const { liveQuery, factory } = await getClient()
|
|
|
|
|
|
|
|
const spaces = await liveQuery.findAll(core.class.Space, {})
|
|
|
|
await factory.createDoc(test.class.ParticipantsHolder, spaces[0]._id, {
|
|
|
|
participants: ['a' as Ref<Doc>]
|
|
|
|
})
|
|
|
|
const a2 = await factory.createDoc(test.class.ParticipantsHolder, spaces[0]._id, {
|
|
|
|
participants: ['b' as Ref<Doc>]
|
|
|
|
})
|
|
|
|
|
|
|
|
const holderBefore = await liveQuery.findAll(test.class.ParticipantsHolder, { participants: 'a' as Ref<Doc> })
|
|
|
|
expect(holderBefore.length).toEqual(1)
|
|
|
|
|
|
|
|
let attempt = 0
|
|
|
|
let resolvePpv: (value: Doc[] | PromiseLike<Doc[]>) => void
|
|
|
|
|
|
|
|
const resolveP = new Promise<Doc[]>((resolve) => {
|
|
|
|
resolvePpv = resolve
|
|
|
|
})
|
|
|
|
const pp = await new Promise((resolve) => {
|
|
|
|
liveQuery.query<Space>(
|
|
|
|
test.class.ParticipantsHolder,
|
|
|
|
{ participants: 'a' as Ref<Doc> },
|
|
|
|
(result) => {
|
|
|
|
if (attempt > 0) {
|
|
|
|
resolvePpv(result)
|
|
|
|
} else {
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ sort: { private: SortingOrder.Ascending } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
await pp // We have first value returned
|
|
|
|
|
|
|
|
attempt++
|
|
|
|
await factory.updateDoc<ParticipantsHolder>(test.class.ParticipantsHolder, spaces[0]._id, a2, {
|
|
|
|
$push: {
|
|
|
|
participants: 'a' as Ref<Doc>
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const result = await resolveP
|
|
|
|
expect(result.length).toEqual(2)
|
|
|
|
})
|
2021-11-26 11:05:18 +00:00
|
|
|
})
|