UNPKG

hydrate-mongodb

Version:
316 lines (315 loc) 13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var queryKind_1 = require("./queryKind"); var persistenceError_1 = require("../persistenceError"); var rx_1 = require("rx"); var QueryBuilderImpl = (function () { function QueryBuilderImpl(session, entityCtr) { this._session = session; this._entityCtr = entityCtr; } QueryBuilderImpl.prototype.findAll = function (criteriaOrCallback, callback) { return this._createFindQuery(queryKind_1.QueryKind.FindAll, criteriaOrCallback, callback); }; QueryBuilderImpl.prototype.findOne = function (criteriaOrCallback, callback) { return this._createFindQuery(queryKind_1.QueryKind.FindOne, criteriaOrCallback, callback); }; QueryBuilderImpl.prototype.findOneById = function (id, callback) { var query = this._createQuery(queryKind_1.QueryKind.FindOneById); if (id == null) { query.error = new persistenceError_1.PersistenceError("Missing or invalid identifier."); } query.id = id; return query.handleCallback(callback); }; QueryBuilderImpl.prototype.findOneAndRemove = function (criteriaOrCallback, callback) { return this._createRemoveQuery(queryKind_1.QueryKind.FindOneAndRemove, criteriaOrCallback, callback); }; QueryBuilderImpl.prototype.findOneAndUpdate = function (criteriaOrUpdateDocument, updateDocumentOrCallback, callback) { return this._createUpdateQuery(queryKind_1.QueryKind.FindOneAndUpdate, criteriaOrUpdateDocument, updateDocumentOrCallback, callback); }; QueryBuilderImpl.prototype.findOneAndUpsert = function (criteriaOrUpdateDocument, updateDocumentOrCallback, callback) { return this._createUpdateQuery(queryKind_1.QueryKind.FindOneAndUpsert, criteriaOrUpdateDocument, updateDocumentOrCallback, callback); }; QueryBuilderImpl.prototype.removeAll = function (criteriaOrCallback, callback) { this._createRemoveQuery(queryKind_1.QueryKind.RemoveAll, criteriaOrCallback, callback); }; QueryBuilderImpl.prototype.removeOne = function (criteriaOrCallback, callback) { this._createRemoveQuery(queryKind_1.QueryKind.RemoveOne, criteriaOrCallback, callback); }; QueryBuilderImpl.prototype.updateAll = function (criteriaOrUpdateDocument, updateDocumentOrCallback, callback) { this._createUpdateQuery(queryKind_1.QueryKind.UpdateAll, criteriaOrUpdateDocument, updateDocumentOrCallback, callback); }; QueryBuilderImpl.prototype.updateOne = function (criteriaOrUpdateDocument, updateDocumentOrCallback, callback) { this._createUpdateQuery(queryKind_1.QueryKind.UpdateOne, criteriaOrUpdateDocument, updateDocumentOrCallback, callback); }; QueryBuilderImpl.prototype.upsert = function (criteriaOrUpdateDocument, updateDocumentOrCallback, callback) { this._createUpdateQuery(queryKind_1.QueryKind.Upsert, criteriaOrUpdateDocument, updateDocumentOrCallback, callback); }; QueryBuilderImpl.prototype.distinct = function (key, criteriaOrCallback, callback) { var query = this._createQuery(queryKind_1.QueryKind.Distinct); query.key = key; if (typeof criteriaOrCallback === "function") { query.criteria = {}; callback = criteriaOrCallback; } else { query.criteria = criteriaOrCallback || {}; } query.handleCallback(callback); }; QueryBuilderImpl.prototype.count = function (criteriaOrCallback, callback) { var query = this._createQuery(queryKind_1.QueryKind.Count); if (typeof criteriaOrCallback == "function") { callback = criteriaOrCallback; query.criteria = {}; } else { query.criteria = criteriaOrCallback || {}; } return query.handleCallback(callback); }; QueryBuilderImpl.prototype._createUpdateQuery = function (kind, criteriaOrUpdateDocument, updateDocumentOrCallback, callback) { var query = this._createQuery(kind); if (typeof updateDocumentOrCallback == "function" || updateDocumentOrCallback === undefined) { callback = updateDocumentOrCallback; query.updateDocument = criteriaOrUpdateDocument; query.criteria = {}; } else { query.updateDocument = updateDocumentOrCallback; query.criteria = criteriaOrUpdateDocument; } return query.handleCallback(callback); }; QueryBuilderImpl.prototype._createRemoveQuery = function (kind, criteriaOrCallback, callback) { var query = this._createQuery(kind); if (typeof criteriaOrCallback == "function") { callback = criteriaOrCallback; query.criteria = {}; } else { query.criteria = criteriaOrCallback || {}; } return query.handleCallback(callback); }; QueryBuilderImpl.prototype._createFindQuery = function (kind, criteriaOrCallback, callback) { var query = this._createQuery(kind); if (typeof criteriaOrCallback == "function") { callback = criteriaOrCallback; query.criteria = {}; } else { query.criteria = criteriaOrCallback || {}; } return query.handleCallback(callback); }; QueryBuilderImpl.prototype._createQuery = function (kind) { return new QueryObject(this._session, this._entityCtr, kind); }; return QueryBuilderImpl; }()); exports.QueryBuilderImpl = QueryBuilderImpl; var QueryObject = (function () { function QueryObject(session, entityCtr, kind) { this.kind = kind; this._session = session; this._entityCtr = entityCtr; } Object.defineProperty(QueryObject.prototype, "readOnly", { get: function () { switch (this.kind) { case queryKind_1.QueryKind.FindAll: case queryKind_1.QueryKind.FindEach: case queryKind_1.QueryKind.FindEachSeries: case queryKind_1.QueryKind.FindCursor: case queryKind_1.QueryKind.FindOne: case queryKind_1.QueryKind.FindOneById: case queryKind_1.QueryKind.Distinct: case queryKind_1.QueryKind.Count: return true; default: return false; } }, enumerable: true, configurable: true }); QueryObject.prototype.fetch = function (path, callback) { if (!this.fetchPaths) { this.fetchPaths = []; } if (typeof path === "string") { this.fetchPaths.push(path); } else { this.fetchPaths = this.fetchPaths.concat(path); } return this.handleCallback(callback); }; QueryObject.prototype.sort = function (field, directionOrCallback, callback) { if (!this.sortValue) { this.sortValue = []; } if (typeof field === "string") { if (typeof directionOrCallback === "number") { this.sortValue.push([field, directionOrCallback]); } else { throw new persistenceError_1.PersistenceError("Expected second parameter to be the sort direction when first parameter is a string."); } } else { if (!Array.isArray(field)) { throw new persistenceError_1.PersistenceError("Expected first parameter to be a string or array"); } this.sortValue = this.sortValue.concat(field); } if (typeof directionOrCallback === "number") { return this.handleCallback(callback); } else { return this.handleCallback(directionOrCallback); } }; QueryObject.prototype.returnUpdated = function (callback) { this.wantsUpdated = true; return this.handleCallback(callback); }; QueryObject.prototype.lazy = function (callback) { this.isLazy = true; return this.handleCallback(callback); }; QueryObject.prototype.limit = function (value, callback) { this.limitCount = value; return this.handleCallback(callback); }; QueryObject.prototype.collation = function (value, callback) { this.collationOptions = value; return this.handleCallback(callback); }; QueryObject.prototype.skip = function (value, callback) { this.skipCount = value; return this.handleCallback(callback); }; QueryObject.prototype.batchSize = function (value, callback) { this.batchSizeValue = value; return this.handleCallback(callback); }; QueryObject.prototype.each = function (iterator, callback) { if (!iterator) { throw new persistenceError_1.PersistenceError("Missing required argument 'iterator'."); } if (!callback) { throw new persistenceError_1.PersistenceError("Missing required argument 'callback'."); } this.kind = queryKind_1.QueryKind.FindEach; this.iterator = iterator; this.handleCallback(callback); }; QueryObject.prototype.eachSeries = function (iterator, callback) { if (!iterator) { throw new persistenceError_1.PersistenceError("Missing required argument 'iterator'."); } if (!callback) { throw new persistenceError_1.PersistenceError("Missing required argument 'callback'."); } this.kind = queryKind_1.QueryKind.FindEachSeries; this.iterator = iterator; this.handleCallback(callback); }; QueryObject.prototype.handleCallback = function (callback) { if (callback) { this.execute(callback); } return this; }; QueryObject.prototype.execute = function (callback) { if (this._executed) { callback(new persistenceError_1.PersistenceError("Query already executed. A callback can only be passed to one function in the chain.")); return; } this._executed = true; if (this.error) { callback(this.error); return; } this._session.executeQuery(this, callback); }; QueryObject.prototype.executeInternal = function (callback) { var mapping = this._session.factory.getMappingForConstructor(this._entityCtr); if (!mapping) { callback(new persistenceError_1.PersistenceError("Object type is not mapped as an entity.")); return; } this._session.getPersister(mapping).executeQuery(this, callback); }; QueryObject.prototype.asPromise = function () { var _this = this; return new Promise(function (resolve, reject) { _this.execute(function (err, result) { if (err) { reject(err); } else { resolve(result); } }); }); }; QueryObject.prototype.asObservable = function () { var _this = this; this.kind = queryKind_1.QueryKind.FindCursor; return rx_1.Observable.create(function (observer) { var cursor, disposed = false; _this.execute(function (err, result) { if (err) { observer.onError(err); } else { cursor = result; (function next() { cursor.next(function (err, entity) { if (disposed) { cursor.close(); return; } if (err) { observer.onError(err); return; } if (entity == null) { observer.onCompleted(); return; } observer.onNext(entity); next(); }); })(); } }); return function () { disposed = true; if (cursor) { cursor.close(); } }; }); }; QueryObject.prototype.toObject = function () { return { kind: queryKind_1.QueryKind[this.kind], criteria: this.criteria, fields: this.fields, update: this.updateDocument, isLazy: this.isLazy, wantsUpdated: this.wantsUpdated, fetch: this.fetchPaths, sort: this.sortValue, limit: this.limitCount, skip: this.skipCount, batchSize: this.batchSizeValue }; }; return QueryObject; }());