diff --git a/packages/core/src/client.ts b/packages/core/src/client.ts index c33beaf79b..4a564b5eb0 100644 --- a/packages/core/src/client.ts +++ b/packages/core/src/client.ts @@ -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) diff --git a/packages/core/src/tx.ts b/packages/core/src/tx.ts index c0bc332bee..0390436e8a 100644 --- a/packages/core/src/tx.ts +++ b/packages/core/src/tx.ts @@ -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) } diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index fd9d6d1e84..6b3567f4d9 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -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 = { diff --git a/plugins/workbench-resources/src/components/WorkbenchApp.svelte b/plugins/workbench-resources/src/components/WorkbenchApp.svelte index e7b55cb280..a3442ffac1 100644 --- a/plugins/workbench-resources/src/components/WorkbenchApp.svelte +++ b/plugins/workbench-resources/src/components/WorkbenchApp.svelte @@ -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) }