implement findOne

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-08-24 21:00:56 +02:00
parent e2b336d017
commit cc521d651e
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
4 changed files with 24 additions and 3 deletions

View File

@ -15,7 +15,7 @@
import type { Doc, Ref, Class } from './classes' import type { Doc, Ref, Class } from './classes'
import type { Tx } from './tx' 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 { SortingOrder } from './storage'
import { Hierarchy } from './hierarchy' import { Hierarchy } from './hierarchy'
@ -35,6 +35,11 @@ export type TxHander = (tx: Tx) => void
export interface Client extends Storage { export interface Client extends Storage {
notify?: (tx: Tx) => void notify?: (tx: Tx) => void
getHierarchy: () => Hierarchy getHierarchy: () => Hierarchy
findOne: <T extends Doc>(
_class: Ref<Class<T>>,
query: DocumentQuery<T>,
options?: FindOptions<T>
) => Promise<WithLookup<T> | undefined>
} }
class ClientImpl implements Client { class ClientImpl implements Client {
@ -57,6 +62,14 @@ class ClientImpl implements Client {
return await this.conn.findAll(_class, query, options) 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> { async tx (tx: Tx): Promise<void> {
if (tx.objectSpace === core.space.Model) { if (tx.objectSpace === core.space.Model) {
this.hierarchy.tx(tx) this.hierarchy.tx(tx)

View File

@ -15,7 +15,7 @@
import type { KeysByType } from 'simplytyped' import type { KeysByType } from 'simplytyped'
import type { Class, Data, Doc, Domain, Ref, Account, Space, Arr, Mixin } from './classes' 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 core from './component'
import { generateId } from './utils' import { generateId } from './utils'
@ -163,6 +163,10 @@ export class TxOperations implements Storage {
return this.storage.findAll(_class, query, options) 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> { tx (tx: Tx): Promise<void> {
return this.storage.tx(tx) return this.storage.tx(tx)
} }

View File

@ -60,6 +60,10 @@ export class LiveQuery extends TxProcessor implements Client {
return await this.client.findAll(_class, query, options) 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 { 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 result = this.client.findAll(_class, query, options)
const q: Query = { const q: Query = {

View File

@ -38,7 +38,7 @@ async function connect(): Promise<Client | undefined> {
const getClient = await getResource(client.function.GetClient) const getClient = await getResource(client.function.GetClient)
const instance = await getClient(token, endpoint) 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) { if (me !== undefined) {
setCurrentAccount(me._id) setCurrentAccount(me._id)
} }