mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-13 11:50:56 +00:00
implement findOne
Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
parent
e2b336d017
commit
cc521d651e
@ -15,7 +15,7 @@
|
||||
|
||||
import type { Doc, Ref, Class } from './classes'
|
||||
import type { Tx } from './tx'
|
||||
import type { Storage, DocumentQuery, FindOptions, FindResult } from './storage'
|
||||
import type { Storage, DocumentQuery, FindOptions, FindResult, WithLookup } from './storage'
|
||||
|
||||
import { SortingOrder } from './storage'
|
||||
import { Hierarchy } from './hierarchy'
|
||||
@ -35,6 +35,11 @@ export type TxHander = (tx: Tx) => void
|
||||
export interface Client extends Storage {
|
||||
notify?: (tx: Tx) => void
|
||||
getHierarchy: () => Hierarchy
|
||||
findOne: <T extends Doc>(
|
||||
_class: Ref<Class<T>>,
|
||||
query: DocumentQuery<T>,
|
||||
options?: FindOptions<T>
|
||||
) => Promise<WithLookup<T> | undefined>
|
||||
}
|
||||
|
||||
class ClientImpl implements Client {
|
||||
@ -57,6 +62,14 @@ class ClientImpl implements Client {
|
||||
return await this.conn.findAll(_class, query, options)
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
async tx (tx: Tx): Promise<void> {
|
||||
if (tx.objectSpace === core.space.Model) {
|
||||
this.hierarchy.tx(tx)
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
import type { KeysByType } from 'simplytyped'
|
||||
import type { Class, Data, Doc, Domain, Ref, Account, Space, Arr, Mixin } from './classes'
|
||||
import { DocumentQuery, FindOptions, FindResult, Storage } from './storage'
|
||||
import { DocumentQuery, FindOptions, FindResult, Storage, WithLookup } from './storage'
|
||||
import core from './component'
|
||||
import { generateId } from './utils'
|
||||
|
||||
@ -163,6 +163,10 @@ export class TxOperations implements Storage {
|
||||
return this.storage.findAll(_class, query, options)
|
||||
}
|
||||
|
||||
async findOne <T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T> | undefined): Promise<WithLookup<T> | undefined> {
|
||||
return (await this.findAll(_class, query, options))[0]
|
||||
}
|
||||
|
||||
tx (tx: Tx): Promise<void> {
|
||||
return this.storage.tx(tx)
|
||||
}
|
||||
|
@ -60,6 +60,10 @@ export class LiveQuery extends TxProcessor implements Client {
|
||||
return await this.client.findAll(_class, query, options)
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
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)
|
||||
const q: Query = {
|
||||
|
@ -38,7 +38,7 @@ async function connect(): Promise<Client | undefined> {
|
||||
|
||||
const getClient = await getResource(client.function.GetClient)
|
||||
const instance = await getClient(token, endpoint)
|
||||
const me = (await instance.findAll(contact.class.Employee, { email }))[0]
|
||||
const me = await instance.findOne(contact.class.Employee, { email })
|
||||
if (me !== undefined) {
|
||||
setCurrentAccount(me._id)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user