UNPKG

@stoqey/sofa

Version:
85 lines (84 loc) 2.26 kB
import { Collection } from 'couchbase'; import { SchemaTypes } from './utils/utils.schema'; import { CustomQueryPagination } from './search'; export interface AutoModelFields { id: string; createdAt: Date; updatedAt?: Date; deleted?: Date; _type: string; _scope: string; } interface ModalOptions { scope?: string; schema?: Record<string, SchemaTypes>; } export declare class Model { collection: Collection; collectionName: string; scope: string; schema: Record<string, SchemaTypes>; constructor(name: string, options?: ModalOptions); /** * Refresh and get default collection from SofaConnection * Because SofaConnection is a singleton, sometimes it might be undefined depending when model was created * So we have to call it from all model methods * to avoid error `Cannot read property 'defaultCollection' of null` */ fresh(): void; /** Get this collection * getCollection */ getCollection(): Collection; /** * create */ create<T>(data: T): Promise<T & AutoModelFields>; /** * findById */ findById(id: string): Promise<any & AutoModelFields>; /** * update */ updateById<T>(id: string, data: T): Promise<T>; /** * save */ save<T>(data: T & { id: string; }): Promise<T>; delete(id: string): Promise<boolean>; /** * Pagination * select = ['id', 'createdAt'] where = { where: { owner: { $eq: "stoqey" }, _type: { $eq: "Trade" } }, }, page = 0, limit = 10, orderBy = { createdAt: "DESC" }, * @param args PaginationArgs */ pagination({ select, where, orderBy, limit, page, customQuery, }: { select?: any[] | string; where?: any; orderBy?: any; limit?: number; page?: number; customQuery?: any; }): Promise<any[]>; /** * Run a custom query with model parsing * @param { select, query } * @returns */ customQuery<T>({ params, limit, query, }: { params: any; limit: number; query: string; }): Promise<[T[], CustomQueryPagination]>; parse<T>(data: T): T; } export default Model;