123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966 |
- 'use strict';
- const { EventEmitter } = require('events');
- const cloneDeep = require('rfdc')();
- const Promise = require('bluebird');
- const { parseArgs, getProp, setGetter, shuffle } = require('./util');
- const Document = require('./document');
- const Query = require('./query');
- const Schema = require('./schema');
- const Types = require('./types');
- const WarehouseError = require('./error');
- const PopulationError = require('./error/population');
- const Mutex = require('./mutex');
- class Model extends EventEmitter {
- /**
- * Model constructor.
- *
- * @param {string} name Model name
- * @param {Schema|object} [schema] Schema
- */
- constructor(name, schema_) {
- super();
- let schema;
- // Define schema
- if (schema_ instanceof Schema) {
- schema = schema_;
- } else if (typeof schema_ === 'object') {
- schema = new Schema(schema_);
- } else {
- schema = new Schema();
- }
- // Set `_id` path for schema
- if (!schema.path('_id')) {
- schema.path('_id', {type: Types.CUID, required: true});
- }
- this.name = name;
- this.data = {};
- this._mutex = new Mutex();
- this.schema = schema;
- this.length = 0;
- class _Document extends Document {
- constructor(data) {
- super(data);
- // Apply getters
- schema._applyGetters(this);
- }
- }
- this.Document = _Document;
- _Document.prototype._model = this;
- _Document.prototype._schema = schema;
- class _Query extends Query {}
- this.Query = _Query;
- _Query.prototype._model = this;
- _Query.prototype._schema = schema;
- // Apply static methods
- Object.assign(this, schema.statics);
- // Apply instance methods
- Object.assign(_Document.prototype, schema.methods);
- }
- /**
- * Creates a new document.
- *
- * @param {object} data
- * @return {Document}
- */
- new(data) {
- return new this.Document(data);
- }
- /**
- * 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, options_) {
- const raw = this.data[id];
- if (!raw) return;
- const options = Object.assign({
- lean: false
- }, options_);
- const data = cloneDeep(raw);
- return options.lean ? data : this.new(data);
- }
- /**
- * Checks if the model contains a document with the specified id.
- *
- * @param {*} id
- * @return {boolean}
- */
- has(id) {
- return Boolean(this.data[id]);
- }
- /**
- * Acquires write lock.
- *
- * @param {*} id
- * @return {Promise}
- * @private
- */
- _acquireWriteLock(id) {
- const mutex = this._mutex;
- return new Promise((resolve, reject) => {
- mutex.lock(resolve);
- }).disposer(() => {
- mutex.unlock();
- });
- }
- /**
- * Inserts a document.
- *
- * @param {Document|object} data
- * @return {Promise}
- * @private
- */
- _insertOne(data_) {
- const schema = this.schema;
- // Apply getters
- const data = data_ instanceof this.Document ? data_ : this.new(data_);
- const id = data._id;
- // Check ID
- if (!id) {
- return Promise.reject(new WarehouseError('ID is not defined', WarehouseError.ID_UNDEFINED));
- }
- if (this.has(id)) {
- return Promise.reject(new WarehouseError('ID `' + id + '` has been used', WarehouseError.ID_EXIST));
- }
- // Apply setters
- const result = data.toObject();
- schema._applySetters(result);
- // Pre-hooks
- return execHooks(schema, 'pre', 'save', data).then(data => {
- // Insert data
- this.data[id] = result;
- this.length++;
- this.emit('insert', data);
- return execHooks(schema, 'post', 'save', data);
- });
- }
- /**
- * Inserts a document.
- *
- * @param {object} data
- * @param {function} [callback]
- * @return {Promise}
- */
- insertOne(data, callback) {
- return Promise.using(this._acquireWriteLock(), () => this._insertOne(data)).asCallback(callback);
- }
- /**
- * Inserts documents.
- *
- * @param {object|array} data
- * @param {function} [callback]
- * @return {Promise}
- */
- insert(data, callback) {
- if (Array.isArray(data)) {
- return Promise.mapSeries(data, item => this.insertOne(item)).asCallback(callback);
- }
- return this.insertOne(data, callback);
- }
- /**
- * Inserts the document if it does not exist; otherwise updates it.
- *
- * @param {object} data
- * @param {function} [callback]
- * @return {Promise}
- */
- save(data, callback) {
- const id = data._id;
- if (!id) return this.insertOne(data, callback);
- return Promise.using(this._acquireWriteLock(), () => {
- if (this.has(id)) {
- return this._replaceById(id, data);
- }
- return this._insertOne(data);
- }).asCallback(callback);
- }
- /**
- * Updates a document with a compiled stack.
- *
- * @param {*} id
- * @param {array} stack
- * @return {Promise}
- * @private
- */
- _updateWithStack(id, stack) {
- const schema = this.schema;
- const data = this.data[id];
- if (!data) {
- return Promise.reject(new WarehouseError('ID `' + id + '` does not exist', WarehouseError.ID_NOT_EXIST));
- }
- // Clone data
- let result = cloneDeep(data);
- // Update
- for (let i = 0, len = stack.length; i < len; i++) {
- stack[i](result);
- }
- // Apply getters
- const doc = this.new(result);
- // Apply setters
- result = doc.toObject();
- schema._applySetters(result);
- // Pre-hooks
- return execHooks(schema, 'pre', 'save', doc).then(data => {
- // Update data
- this.data[id] = result;
- this.emit('update', data);
- return execHooks(schema, 'post', 'save', data);
- });
- }
- /**
- * Finds a document by its identifier and update it.
- *
- * @param {*} id
- * @param {object} update
- * @param {function} [callback]
- * @return {Promise}
- */
- updateById(id, update, callback) {
- return Promise.using(this._acquireWriteLock(), () => {
- const stack = this.schema._parseUpdate(update);
- return this._updateWithStack(id, stack);
- }).asCallback(callback);
- }
- /**
- * Updates matching documents.
- *
- * @param {object} query
- * @param {object} data
- * @param {function} [callback]
- * @return {Promise}
- */
- update(query, data, callback) {
- return this.find(query).update(data, callback);
- }
- /**
- * Finds a document by its identifier and replace it.
- *
- * @param {*} id
- * @param {object} data
- * @return {Promise}
- * @private
- */
- _replaceById(id, data_) {
- const schema = this.schema;
- if (!this.has(id)) {
- return Promise.reject(new WarehouseError('ID `' + id + '` does not exist', WarehouseError.ID_NOT_EXIST));
- }
- data_._id = id;
- // Apply getters
- const data = data_ instanceof this.Document ? data_ : this.new(data_);
- // Apply setters
- const result = data.toObject();
- schema._applySetters(result);
- // Pre-hooks
- return execHooks(schema, 'pre', 'save', data).then(data => {
- // Replace data
- this.data[id] = result;
- this.emit('update', data);
- return execHooks(schema, 'post', 'save', data);
- });
- }
- /**
- * Finds a document by its identifier and replace it.
- *
- * @param {*} id
- * @param {object} data
- * @param {function} [callback]
- * @return {Promise}
- */
- replaceById(id, data, callback) {
- return Promise.using(this._acquireWriteLock(), () => this._replaceById(id, data)).asCallback(callback);
- }
- /**
- * Replaces matching documents.
- *
- * @param {object} query
- * @param {object} data
- * @param {function} [callback]
- * @return {Promise}
- */
- replace(query, data, callback) {
- return this.find(query).replace(data, callback);
- }
- /**
- * Finds a document by its identifier and remove it.
- *
- * @param {*} id
- * @param {function} [callback]
- * @return {Promise}
- * @private
- */
- _removeById(id) {
- const schema = this.schema;
- const data = this.data[id];
- if (!data) {
- return Promise.reject(new WarehouseError('ID `' + id + '` does not exist', WarehouseError.ID_NOT_EXIST));
- }
- // Pre-hooks
- return execHooks(schema, 'pre', 'remove', data).then(data => {
- // Remove data
- this.data[id] = null;
- this.length--;
- this.emit('remove', data);
- return execHooks(schema, 'post', 'remove', data);
- });
- }
- /**
- * Finds a document by its identifier and remove it.
- *
- * @param {*} id
- * @param {function} [callback]
- * @return {Promise}
- */
- removeById(id, callback) {
- return Promise.using(this._acquireWriteLock(), () => this._removeById(id)).asCallback(callback);
- }
- /**
- * Removes matching documents.
- *
- * @param {object} query
- * @param {object} [callback]
- * @return {Promise}
- */
- remove(query, callback) {
- return this.find(query).remove(callback);
- }
- /**
- * Deletes a model.
- */
- destroy() {
- this._database._models[this.name] = null;
- }
- /**
- * Returns the number of elements.
- *
- * @return {number}
- */
- count() {
- return this.length;
- }
- /**
- * Iterates over all documents.
- *
- * @param {function} iterator
- * @param {object} [options] See {@link Model#findById}.
- */
- forEach(iterator, options) {
- const keys = Object.keys(this.data);
- let num = 0;
- for (let i = 0, len = keys.length; i < len; i++) {
- const data = this.findById(keys[i], options);
- if (data) iterator(data, num++);
- }
- }
- /**
- * Returns an array containing all documents.
- *
- * @param {Object} [options] See {@link Model#findById}.
- * @return {Array}
- */
- toArray(options) {
- const result = new Array(this.length);
- this.forEach((item, i) => {
- result[i] = item;
- }, options);
- return result;
- }
- /**
- * 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, options_) {
- const options = options_ || {};
- const filter = this.schema._execQuery(query);
- const keys = Object.keys(this.data);
- const len = keys.length;
- let limit = options.limit || this.length;
- let skip = options.skip;
- const data = this.data;
- const arr = [];
- for (let i = 0; limit && i < len; i++) {
- const key = keys[i];
- const item = data[key];
- if (item && filter(item)) {
- if (skip) {
- skip--;
- } else {
- arr.push(this.findById(key, options));
- limit--;
- }
- }
- }
- return options.lean ? arr : new this.Query(arr);
- }
- /**
- * 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, options_) {
- const options = options_ || {};
- options.limit = 1;
- const result = this.find(query, options);
- return options.lean ? result[0] : result.data[0];
- }
- /**
- * Sorts documents. See {@link Query#sort}.
- *
- * @param {String|Object} orderby
- * @param {String|Number} [order]
- * @return {Query}
- */
- sort(orderby, order) {
- const sort = parseArgs(orderby, order);
- const fn = this.schema._execSort(sort);
- return new this.Query(this.toArray().sort(fn));
- }
- /**
- * 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_, options) {
- let index = i_ < 0 ? this.length + i_ : i_;
- const data = this.data;
- const keys = Object.keys(data);
- for (let i = 0, len = keys.length; i < len; i++) {
- const key = keys[i];
- const item = data[key];
- if (!item) continue;
- if (index) {
- index--;
- } else {
- return this.findById(key, options);
- }
- }
- }
- /**
- * Returns the first document.
- *
- * @param {Object} [options] See {@link Model#findById}.
- * @return {Document|Object}
- */
- first(options) {
- return this.eq(0, options);
- }
- /**
- * Returns the last document.
- *
- * @param {Object} [options] See {@link Model#findById}.
- * @return {Document|Object}
- */
- last(options) {
- return this.eq(-1, options);
- }
- /**
- * Returns the specified range of documents.
- *
- * @param {Number} start
- * @param {Number} [end]
- * @return {Query}
- */
- slice(start_, end_) {
- const total = this.length;
- let start = start_ | 0;
- if (start < 0) start += total;
- if (start > total - 1) return new this.Query([]);
- let end = end_ | 0 || total;
- if (end < 0) end += total;
- let len = start > end ? 0 : end - start;
- if (len > total) len = total - start;
- if (!len) return new this.Query([]);
- const arr = new Array(len);
- const keys = Object.keys(this.data);
- const keysLen = keys.length;
- let num = 0;
- for (let i = 0; num < len && i < keysLen; i++) {
- const data = this.findById(keys[i]);
- if (!data) continue;
- if (start) {
- start--;
- } else {
- arr[num++] = data;
- }
- }
- return new this.Query(arr);
- }
- /**
- * Limits the number of documents returned.
- *
- * @param {Number} i
- * @return {Query}
- */
- limit(i) {
- return this.slice(0, i);
- }
- /**
- * Specifies the number of items to skip.
- *
- * @param {Number} i
- * @return {Query}
- */
- skip(i) {
- return this.slice(i);
- }
- /**
- * Returns documents in a reversed order.
- *
- * @return {Query}
- */
- reverse() {
- return new this.Query(this.toArray().reverse());
- }
- /**
- * Returns documents in random order.
- *
- * @return {Query}
- */
- shuffle() {
- return new this.Query(shuffle(this.toArray()));
- }
- /**
- * Creates an array of values by iterating each element in the collection.
- *
- * @param {Function} iterator
- * @param {Object} [options]
- * @return {Array}
- */
- map(iterator, options) {
- const result = new Array(this.length);
- const keys = Object.keys(this.data);
- const len = keys.length;
- for (let i = 0, num = 0; i < len; i++) {
- const data = this.findById(keys[i], options);
- if (data) {
- result[num] = iterator(data, num);
- num++;
- }
- }
- return result;
- }
- /**
- * 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, initial) {
- const arr = this.toArray();
- const len = this.length;
- let i, result;
- if (initial === undefined) {
- i = 1;
- result = arr[0];
- } else {
- i = 0;
- result = initial;
- }
- for (; i < len; i++) {
- result = iterator(result, arr[i], i);
- }
- return result;
- }
- /**
- * 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, initial) {
- const arr = this.toArray();
- const len = this.length;
- let i, result;
- if (initial === undefined) {
- i = len - 2;
- result = arr[len - 1];
- } else {
- i = len - 1;
- result = initial;
- }
- for (; i >= 0; i--) {
- result = iterator(result, arr[i], i);
- }
- return result;
- }
- /**
- * 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, options) {
- const arr = [];
- this.forEach((item, i) => {
- if (iterator(item, i)) arr.push(item);
- }, options);
- return new this.Query(arr);
- }
- /**
- * Tests whether all documents pass the test implemented by the provided
- * function.
- *
- * @param {Function} iterator
- * @return {Boolean}
- */
- every(iterator) {
- const keys = Object.keys(this.data);
- const len = keys.length;
- let num = 0;
- if (!len) return true;
- for (let i = 0; i < len; i++) {
- const data = this.findById(keys[i]);
- if (data) {
- if (!iterator(data, num++)) return false;
- }
- }
- return true;
- }
- /**
- * Tests whether some documents pass the test implemented by the provided
- * function.
- *
- * @param {Function} iterator
- * @return {Boolean}
- */
- some(iterator) {
- const keys = Object.keys(this.data);
- const len = keys.length;
- let num = 0;
- if (!len) return false;
- for (let i = 0; i < len; i++) {
- const data = this.findById(keys[i]);
- if (data) {
- if (iterator(data, num++)) return true;
- }
- }
- return false;
- }
- /**
- * Returns a getter function for normal population.
- *
- * @param {Object} data
- * @param {Model} model
- * @param {Object} options
- * @return {Function}
- * @private
- */
- _populateGetter(data, model, options) {
- let hasCache = false;
- let cache;
- return () => {
- if (!hasCache) {
- cache = model.findById(data);
- hasCache = true;
- }
- return cache;
- };
- }
- /**
- * Returns a getter function for array population.
- *
- * @param {Object} data
- * @param {Model} model
- * @param {Object} options
- * @return {Function}
- * @private
- */
- _populateGetterArray(data, model, options) {
- const Query = model.Query;
- let hasCache = false;
- let cache;
- return () => {
- if (!hasCache) {
- let arr = [];
- for (let i = 0, len = data.length; i < len; i++) {
- arr.push(model.findById(data[i]));
- }
- if (options.match) {
- cache = new Query(arr).find(options.match, options);
- } else if (options.skip) {
- if (options.limit) {
- arr = arr.slice(options.skip, options.skip + options.limit);
- } else {
- arr = arr.slice(options.skip);
- }
- cache = new Query(arr);
- } else if (options.limit) {
- cache = new Query(arr.slice(0, options.limit));
- } else {
- cache = new Query(arr);
- }
- if (options.sort) {
- cache = cache.sort(options.sort);
- }
- hasCache = true;
- }
- return cache;
- };
- }
- /**
- * Populates document references with a compiled stack.
- *
- * @param {Object} data
- * @param {Array} stack
- * @return {Object}
- * @private
- */
- _populate(data, stack) {
- const models = this._database._models;
- for (let i = 0, len = stack.length; i < len; i++) {
- const item = stack[i];
- const model = models[item.model];
- if (!model) {
- throw new PopulationError('Model `' + item.model + '` does not exist');
- }
- const path = item.path;
- const prop = getProp(data, path);
- if (Array.isArray(prop)) {
- setGetter(data, path, this._populateGetterArray(prop, model, item));
- } else {
- setGetter(data, path, this._populateGetter(prop, model, item));
- }
- }
- return data;
- }
- /**
- * Populates document references.
- *
- * @param {String|Object} path
- * @return {Query}
- */
- populate(path) {
- if (!path) throw new TypeError('path is required');
- const stack = this.schema._parsePopulate(path);
- const arr = new Array(this.length);
- this.forEach((item, i) => {
- arr[i] = this._populate(item, stack);
- });
- return new Query(arr);
- }
- /**
- * Imports data.
- *
- * @param {Array} arr
- * @private
- */
- _import(arr) {
- const len = arr.length;
- const data = this.data;
- const schema = this.schema;
- for (let i = 0; i < len; i++) {
- const item = arr[i];
- data[item._id] = schema._parseDatabase(item);
- }
- this.length = len;
- }
- /**
- * Exports data.
- *
- * @return {String}
- * @private
- */
- _export() {
- return JSON.stringify(this.toJSON());
- }
- toJSON() {
- const result = new Array(this.length);
- const { data, schema } = this;
- const keys = Object.keys(data);
- const { length } = keys;
- for (let i = 0, num = 0; i < length; i++) {
- const raw = data[keys[i]];
- if (raw) {
- result[num++] = schema._exportDatabase(cloneDeep(raw));
- }
- }
- return result;
- }
- }
- Model.prototype.get = Model.prototype.findById;
- function execHooks(schema, type, event, data) {
- const hooks = schema.hooks[type][event];
- if (!hooks.length) return Promise.resolve(data);
- return Promise.each(hooks, hook => hook(data)).thenReturn(data);
- }
- Model.prototype.size = Model.prototype.count;
- Model.prototype.each = Model.prototype.forEach;
- Model.prototype.random = Model.prototype.shuffle;
- module.exports = Model;
|