@ztimson/momentum
Version:
Client library for momentum
73 lines • 2.75 kB
TypeScript
import { Unsubscribe } from '@ztimson/utils';
import { Meta, PathEventEmitter } from './core';
import { Momentum } from './momentum';
/** Generic MongoDB document */
export type Document<T> = Meta & T;
/** Raw MongoDB query */
export type RawQuery = {
/** Query operation type */
operand: 'delete' | 'deleteOne' | 'find' | 'findOne' | 'insertOne' | 'replaceOne' | 'update' | 'updateOne';
/** Filter affected documents */
filter?: any;
/** Query */
data?: any;
/** MongoDB operation options */
options?: any;
/** Return document count */
count?: boolean;
};
/** Create custom data collections */
export declare class Data extends PathEventEmitter {
protected momentum: Momentum;
protected readonly subscribers: {
[key: string]: Unsubscribe;
};
constructor(momentum: Momentum);
/**
* Create new document in collection
* @param {string} collection Target collection
* @param {Document<T>} document New document
* @return {Promise<Document<T>>} New saved document
*/
create<T>(collection: string, document: Document<T>): Promise<Document<T>>;
/**
* Delete document from collection
* @param {string} collection Target collection
* @param {number} id Document ID
* @return {Promise<void>} Returns once complete
*/
delete(collection: string, id: number): Promise<number>;
/**
* Read collection/document
* @param {string} collection target collection/document
* @return {Promise<T[] | T>} Found collection of documents
*/
read<T extends Meta>(collection: string): Promise<T[]>;
read<T extends Meta>(collection: string, id: number): Promise<T>;
/**
* Update document in collection
* @param {string} collection Target collection
* @param {Document<T>} document Document to update
* @param {boolean} append Append or replace existing document
* @return {Promise<Document<T>>} New saved document
*/
update<T>(collection: string, document: Document<T>, append?: boolean): Promise<Document<T>>;
/**
* Create raw MongoDB query
* @param {string} collection Target collection name
* @param {RawQuery} query Raw query
* @return {Promise<any>} Query response
*/
raw<T>(collection: string, query: RawQuery): Promise<any>;
/**
* Subscribe to live updates with callback
* @param path Path to data
* @param {(value: T[]) => any | null} callback Received changes
* @param opts Reload data immediately
* @return {() => void} Function to unsubscribe
*/
sync<T>(path: string, callback: ((value: T | T[]) => any), opts?: {
reload?: boolean;
}): Unsubscribe;
}
//# sourceMappingURL=data.d.ts.map