2021-08-06 10:18:50 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 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.
|
|
|
|
//
|
|
|
|
|
|
|
|
import {
|
|
|
|
Ref, Class, Doc, Tx, DocumentQuery, TxCreateDoc, TxRemoveDoc, Client,
|
|
|
|
FindOptions, TxUpdateDoc, _getOperator, TxProcessor, resultSort, SortingQuery,
|
2021-08-07 17:21:52 +00:00
|
|
|
FindResult, Hierarchy, Refs, WithLookup, LookupData
|
2021-08-06 10:18:50 +00:00
|
|
|
} from '@anticrm/core'
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
interface Query {
|
2021-08-06 10:18:50 +00:00
|
|
|
_class: Ref<Class<Doc>>
|
|
|
|
query: DocumentQuery<Doc>
|
|
|
|
result: Doc[] | Promise<Doc[]>
|
|
|
|
options?: FindOptions<Doc>
|
|
|
|
callback: (result: FindResult<Doc>) => void
|
|
|
|
}
|
|
|
|
|
2021-08-07 05:39:49 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-07 17:21:52 +00:00
|
|
|
export class LiveQuery extends TxProcessor implements Client {
|
2021-08-06 10:18:50 +00:00
|
|
|
private readonly client: Client
|
2021-08-07 17:21:52 +00:00
|
|
|
private readonly queries: Query[] = []
|
2021-08-06 10:18:50 +00:00
|
|
|
|
|
|
|
constructor (client: Client) {
|
|
|
|
super()
|
|
|
|
this.client = client
|
|
|
|
}
|
|
|
|
|
|
|
|
getHierarchy (): Hierarchy {
|
|
|
|
return this.client.getHierarchy()
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
private match (q: Query, doc: Doc): boolean {
|
2021-08-06 10:18:50 +00:00
|
|
|
if (!this.getHierarchy().isDerived(doc._class, q._class)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for (const key in q.query) {
|
|
|
|
const value = (q.query as any)[key]
|
|
|
|
if ((doc as any)[key] !== value) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
async findAll<T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T>): Promise<FindResult<T>> {
|
|
|
|
return await this.client.findAll(_class, query, options)
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:00:56 +00:00
|
|
|
async findOne<T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T>): Promise<WithLookup<T> | undefined> {
|
|
|
|
return (await this.findAll(_class, query, options))[0]
|
|
|
|
}
|
|
|
|
|
2021-08-06 10:18:50 +00:00
|
|
|
query<T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, callback: (result: T[]) => void, options?: FindOptions<T>): () => void {
|
|
|
|
const result = this.client.findAll(_class, query, options)
|
2021-08-07 17:21:52 +00:00
|
|
|
const q: Query = {
|
2021-08-06 10:18:50 +00:00
|
|
|
_class,
|
|
|
|
query,
|
|
|
|
result,
|
|
|
|
options,
|
|
|
|
callback: callback as (result: Doc[]) => void
|
|
|
|
}
|
|
|
|
this.queries.push(q)
|
|
|
|
result
|
|
|
|
.then((result) => {
|
|
|
|
q.callback(result)
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log('failed to update Live Query: ', err)
|
|
|
|
})
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
this.queries.splice(this.queries.indexOf(q), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
protected async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
|
2021-08-06 10:18:50 +00:00
|
|
|
for (const q of this.queries) {
|
|
|
|
if (q.result instanceof Promise) {
|
|
|
|
q.result = await q.result
|
|
|
|
}
|
|
|
|
const updatedDoc = q.result.find(p => p._id === tx.objectId)
|
|
|
|
if (updatedDoc !== undefined) {
|
|
|
|
await this.__updateDoc(updatedDoc, tx)
|
|
|
|
this.sort(q, tx)
|
|
|
|
await this.callback(updatedDoc, q)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async lookup (doc: Doc, lookup: Refs<Doc>): Promise<void> {
|
|
|
|
const result: LookupData<Doc> = {}
|
|
|
|
for (const key in lookup) {
|
|
|
|
const _class = (lookup as any)[key] as Ref<Class<Doc>>
|
|
|
|
const _id = (doc as any)[key] as Ref<Doc>
|
|
|
|
(result as any)[key] = (await this.client.findAll(_class, { _id }))[0]
|
|
|
|
}
|
|
|
|
(doc as WithLookup<Doc>).$lookup = result
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
protected async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
|
2021-08-08 21:04:39 +00:00
|
|
|
console.log('query tx', tx)
|
2021-08-06 10:18:50 +00:00
|
|
|
for (const q of this.queries) {
|
|
|
|
const doc = TxProcessor.createDoc2Doc(tx)
|
|
|
|
if (this.match(q, doc)) {
|
|
|
|
if (q.result instanceof Promise) {
|
|
|
|
q.result = await q.result
|
|
|
|
}
|
|
|
|
|
|
|
|
if (q.options?.lookup !== undefined) await this.lookup(doc, q.options.lookup)
|
|
|
|
|
|
|
|
q.result.push(doc)
|
|
|
|
|
|
|
|
if (q.options?.sort !== undefined) resultSort(q.result, q.options?.sort)
|
|
|
|
|
|
|
|
if (q.options?.limit !== undefined && q.result.length > q.options.limit) {
|
|
|
|
if (q.result.pop()?._id !== doc._id) {
|
|
|
|
q.callback(q.result)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
q.callback(q.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
protected async txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
|
2021-08-06 10:18:50 +00:00
|
|
|
for (const q of this.queries) {
|
|
|
|
if (q.result instanceof Promise) {
|
|
|
|
q.result = await q.result
|
|
|
|
}
|
|
|
|
const index = q.result.findIndex(p => p._id === tx.objectId)
|
|
|
|
if (index > -1) {
|
|
|
|
q.result.splice(index, 1)
|
|
|
|
q.callback(q.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async tx (tx: Tx): Promise<void> {
|
2021-08-08 21:04:39 +00:00
|
|
|
// await this.client.tx(tx)
|
2021-08-06 10:18:50 +00:00
|
|
|
await super.tx(tx)
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
private async __updateDoc (updatedDoc: Doc, tx: TxUpdateDoc<Doc>): Promise<void> {
|
2021-08-06 10:18:50 +00:00
|
|
|
const ops = tx.operations as any
|
|
|
|
for (const key in ops) {
|
|
|
|
if (key.startsWith('$')) {
|
|
|
|
const operator = _getOperator(key)
|
|
|
|
operator(updatedDoc, ops[key])
|
|
|
|
} else {
|
|
|
|
(updatedDoc as any)[key] = ops[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updatedDoc.modifiedBy = tx.modifiedBy
|
|
|
|
updatedDoc.modifiedOn = tx.modifiedOn
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
private sort (q: Query, tx: TxUpdateDoc<Doc>): void {
|
2021-08-06 10:18:50 +00:00
|
|
|
const sort = q.options?.sort
|
|
|
|
if (sort === undefined) return
|
|
|
|
let needSort = sort.modifiedBy !== undefined || sort.modifiedOn !== undefined
|
|
|
|
if (!needSort) needSort = this.checkNeedSort(sort, tx)
|
|
|
|
|
|
|
|
if (needSort) resultSort(q.result as Doc[], sort)
|
|
|
|
}
|
|
|
|
|
|
|
|
private checkNeedSort (sort: SortingQuery<Doc>, tx: TxUpdateDoc<Doc>): boolean {
|
|
|
|
const ops = tx.operations as any
|
|
|
|
for (const key in ops) {
|
|
|
|
if (key.startsWith('$')) {
|
|
|
|
for (const opKey in ops[key]) {
|
|
|
|
if (opKey in sort) return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (key in sort) return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
private async callback (updatedDoc: Doc, q: Query): Promise<void> {
|
2021-08-06 10:18:50 +00:00
|
|
|
q.result = q.result as Doc[]
|
|
|
|
|
|
|
|
if (q.options?.limit !== undefined && q.result.length > q.options.limit) {
|
|
|
|
if (q.result[q.options?.limit]._id === updatedDoc._id) {
|
|
|
|
const res = await this.findAll(q._class, q.query, q.options)
|
|
|
|
q.result = res
|
|
|
|
q.callback(res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (q.result.pop()?._id !== updatedDoc._id) q.callback(q.result)
|
|
|
|
} else {
|
|
|
|
q.callback(q.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|