UNPKG

@stoqey/firestore

Version:

NodeJS Firebase Firestore CRUD library

121 lines (120 loc) 3.26 kB
import { firestore } from 'firebase-admin'; import { AutoPagination, PaginationData, Operators, OrderByDirection } from './interface'; interface IField { name: string; operator?: Operators; value: any; } interface IQuery { limit?: number; page?: number; id?: any; field?: any; min?: any; max?: any; operator?: any; value?: any; multiple?: boolean; data?: any; } interface IWhereAll { fields: IField[]; multiple?: boolean; limit?: number; snapshot?: boolean; sort?: { name: string; direction: OrderByDirection; }[]; } interface IDocument { [x: string]: any; } interface IAddDocumentOptions extends IDocument { checkName?: boolean; } /** * This is the Base class to fetch and store data to Firestore */ export declare class BaseFireStore { db: firestore.Firestore; collection: string; storageRef: string; options?: { logger?: (event: any) => void; debug?: boolean; }; constructor(db: firestore.Firestore, options?: { logger?: (event: any) => void; debug?: boolean; }); logger(event: any): void | null; /** * Return the collection */ getRef(): firestore.CollectionReference; /** * @param {String} path * @return {arrayObject} deployedApps by user */ autoPagination(query: AutoPagination): Promise<PaginationData>; /** * Return all data from a collection with pagination support. * @param {Object} query */ all(query?: IQuery): Promise<{ data: firestore.DocumentData[]; total: number; }>; /** * Fetch collection with where conditions * @param {Object} query */ where(query?: IQuery): Promise<firestore.DocumentData | any | any[]>; /** * Fetch collection with where conditions * @param {Object} query */ whereAll(query: IWhereAll): Promise<firestore.DocumentData | any | any[]>; /** * Fetch documents which falls between two range * @param {Object} query */ range(query?: IQuery): Promise<any[]>; /** * Fetch a document by its ID * @param id */ byId(id: string): Promise<firestore.DocumentData>; /** * Delete a document by its ID * @param id */ delete(id?: any): Promise<firestore.DocumentData>; byName(name: string): Promise<firestore.DocumentData>; /** * Push a new document under a specific collection check if name exists, else update it * @param {Object} query * @checkName is @true by `default` */ add(data: IDocument, options?: IAddDocumentOptions): Promise<firestore.DocumentData>; set(query?: IQuery): Promise<firestore.DocumentData>; /** * Update a document by ID and new data * @param {Object} query */ update(query?: IQuery): Promise<firestore.DocumentData>; /** * timelinePagination * Pagination using createdAt from newest time to older -> * Alway DESC * use start and order to pull chunk from a specific time range */ timelinePagination(args: { order: 'new' | 'old'; limit: number; start: Date; }): Promise<firestore.DocumentData[]>; } export default BaseFireStore;