/// import { EventEmitter } from 'events'; import Promise from 'bluebird'; import Document from './document'; import Query from './query'; import Schema from './schema'; import Mutex from './mutex'; import type Database from './database'; import type { AddSchemaTypeOptions, NodeJSLikeCallback, Options, PopulateResult } from './types'; declare class Model extends EventEmitter { name: string; _mutex: Mutex; data: Record; schema: Schema; length: number; Document: any; Query: any; _database: Database; /** * Model constructor. * * @param {string} name Model name * @param {Schema|object} [schema_] Schema */ constructor(name: string, schema_: Schema | Record); /** * Creates a new document. * * @param {object} data * @return {Document} */ new(data?: T): Document; /** * Finds a document by its identifier. * * @param {*} id * @param {object} options * @param {boolean} [options.lean=false] Returns a plain JavaScript object * @return {Document|object} */ findById(id: PropertyKey, options_?: Options): Document | T; /** * Checks if the model contains a document with the specified id. * * @param {*} id * @return {boolean} */ has(id: PropertyKey): boolean; /** * Acquires write lock. * * @return {Promise} * @private */ _acquireWriteLock(): Promise.Disposer; /** * Inserts a document. * * @param {Document|object} data * @return {Promise} * @private */ _insertOne(data_: Document | T): Promise; /** * Inserts a document. * * @param {object} data * @param {function} [callback] * @return {Promise} */ insertOne(data: Document | T, callback?: NodeJSLikeCallback): Promise; /** * Inserts documents. * * @param {object|array} data * @param {function} [callback] * @return {Promise} */ insert(data: Document | T | Document[] | T[], callback?: NodeJSLikeCallback): Promise; /** * Inserts the document if it does not exist; otherwise updates it. * * @param {object} data * @param {function} [callback] * @return {Promise} */ save(data: Document | T, callback?: NodeJSLikeCallback): Promise; /** * Updates a document with a compiled stack. * * @param {*} id * @param {array} stack * @return {Promise} * @private */ _updateWithStack(id: string | number, stack: ((data: any) => void)[]): Promise; /** * Finds a document by its identifier and update it. * * @param {*} id * @param {object} update * @param {function} [callback] * @return {Promise} */ updateById(id: string | number, update: object, callback?: NodeJSLikeCallback): Promise; /** * Updates matching documents. * * @param {object} query * @param {object} data * @param {function} [callback] * @return {Promise} */ update(query: object, data: object, callback?: NodeJSLikeCallback): Promise; /** * Finds a document by its identifier and replace it. * * @param {*} id * @param {object} data * @return {Promise} * @private */ _replaceById(id: string | number, data_: Document | T): Promise; /** * Finds a document by its identifier and replace it. * * @param {*} id * @param {object} data * @param {function} [callback] * @return {Promise} */ replaceById(id: string | number, data: Document | T, callback?: NodeJSLikeCallback): Promise; /** * Replaces matching documents. * * @param {object} query * @param {object} data * @param {function} [callback] * @return {Promise} */ replace(query: object, data: any, callback?: NodeJSLikeCallback): Promise; /** * Finds a document by its identifier and remove it. * * @param {*} id * @return {Promise} * @private */ _removeById(id: string | number): Promise; /** * Finds a document by its identifier and remove it. * * @param {*} id * @param {function} [callback] * @return {Promise} */ removeById(id: string | number, callback?: NodeJSLikeCallback): Promise; /** * Removes matching documents. * * @param {object} query * @param {function} [callback] * @return {Promise} */ remove(query: object, callback?: NodeJSLikeCallback): Promise; /** * Deletes a model. */ destroy(): void; /** * Returns the number of elements. * * @return {number} */ count(): number; /** * Iterates over all documents. * * @param {function} iterator * @param {object} [options] See {@link Model#findById}. */ forEach(iterator: (value: any, index: number) => void, options?: Options): void; /** * Returns an array containing all documents. * * @param {Object} [options] See {@link Model#findById}. * @return {Array} */ toArray(options?: Options): any[]; /** * Finds matching documents. * * @param {Object} query * @param {Object} [options] * @param {Number} [options.limit=0] Limits the number of documents returned. * @param {Number} [options.skip=0] Skips the first elements. * @param {Boolean} [options.lean=false] Returns a plain JavaScript object. * @return {Query|Array} */ find(query: object): Query; find(query: object, options: Options): Query | T[]; /** * Finds the first matching documents. * * @param {Object} query * @param {Object} [options] * @param {Number} [options.skip=0] Skips the first elements. * @param {Boolean} [options.lean=false] Returns a plain JavaScript object. * @return {Document|Object} */ findOne(query: object): Document; findOne(query: object, options_: Options): Document | T; /** * Sorts documents. See {@link Query#sort}. * * @param {String|Object} orderby * @param {String|Number} [order] * @return {Query} */ sort(orderby: string | object, order?: string | number): Query; /** * Returns the document at the specified index. `num` can be a positive or * negative number. * * @param {Number} i * @param {Object} [options] See {@link Model#findById}. * @return {Document|Object} */ eq(i_: number, options?: Options): Document | Record; /** * Returns the first document. * * @param {Object} [options] See {@link Model#findById}. * @return {Document|Object} */ first(options?: Options): Document | Record; /** * Returns the last document. * * @param {Object} [options] See {@link Model#findById}. * @return {Document|Object} */ last(options?: Options): Document | Record; /** * Returns the specified range of documents. * * @param {Number} start * @param {Number} [end] * @return {Query} */ slice(start_?: number, end_?: number): Query; /** * Limits the number of documents returned. * * @param {Number} i * @return {Query} */ limit(i: number): Query; /** * Specifies the number of items to skip. * * @param {Number} i * @return {Query} */ skip(i: number): Query; /** * Returns documents in a reversed order. * * @return {Query} */ reverse(): Query; /** * Returns documents in random order. * * @return {Query} */ shuffle(): Query; /** * Creates an array of values by iterating each element in the collection. * * @param {Function} iterator * @param {Object} [options] * @return {Array} */ map(iterator: (value: any, index: number) => T, options?: Options): T[]; /** * Reduces a collection to a value which is the accumulated result of iterating * each element in the collection. * * @param {Function} iterator * @param {*} [initial] By default, the initial value is the first document. * @return {*} */ reduce(iterator: (pre: any, cur: any, index: number) => T, initial?: T): T; /** * Reduces a collection to a value which is the accumulated result of iterating * each element in the collection from right to left. * * @param {Function} iterator * @param {*} [initial] By default, the initial value is the last document. * @return {*} */ reduceRight(iterator: (pre: any, cur: any, index: number) => T, initial?: T): T; /** * Creates a new array with all documents that pass the test implemented by the * provided function. * * @param {Function} iterator * @param {Object} [options] * @return {Query} */ filter(iterator: (value: any, index: number) => any, options?: Options): Query; /** * Tests whether all documents pass the test implemented by the provided * function. * * @param {Function} iterator * @return {Boolean} */ every(iterator: (value: any, index: number) => any): boolean; /** * Tests whether some documents pass the test implemented by the provided * function. * * @param {Function} iterator * @return {Boolean} */ some(iterator: (value: any, index: number) => any): boolean; /** * Returns a getter function for normal population. * * @param {Object} data * @param {Model} model * @param {Object} options * @return {Function} * @private */ _populateGetter(data: string | number, model: Model, options: unknown): () => Document | Record; /** * Returns a getter function for array population. * * @param {Object} data * @param {Model} model * @param {Object} options * @return {Function} * @private */ _populateGetterArray(data: any[], model: Model, options: Options): () => any[] | Query; /** * Populates document references with a compiled stack. * * @param {Object} data * @param {Array} stack * @return {Object} * @private */ _populate(data: Document, stack: PopulateResult[]): Document; /** * Populates document references. * * @param {String|Object} path * @return {Query} */ populate(path: string | any[] | { path?: string; model?: any; [key: PropertyKey]: any; }): Query; /** * Imports data. * * @param {Array} arr * @private */ _import(arr: any[]): void; /** * Exports data. * * @return {String} * @private */ _export(): string; toJSON(): any[]; get: Model['findById']; size: Model['count']; each: Model['forEach']; random: Model['shuffle']; } export default Model;