mongoe
Version:
MongoDB driver with relational functionalities
185 lines (184 loc) • 8.78 kB
JavaScript
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Collection = void 0;
var _1 = require(".");
var Collection = /** @class */ (function () {
function Collection(database, name, _a) {
if (_a === void 0) { _a = {}; }
var primaryKey = _a.primaryKey, foreignKeys = _a.foreignKeys, mongodbOptions = __rest(_a, ["primaryKey", "foreignKeys"]);
this.name = name;
this.database = database;
this.handle = database.handle.then(function (db) {
return db.collection(name, mongodbOptions);
});
this.registerRelation({ primaryKey: primaryKey, foreignKeys: foreignKeys });
}
Collection.prototype.registerRelation = function (relation) {
var _a;
this.database.registerRelations((_a = {},
_a[this.name] = relation,
_a));
};
// Query methods :
Collection.prototype.findOne = function (filter, options) {
return this.handle.then(function (col) { return col.findOne(filter, options); });
};
Collection.prototype.find = function (query, options) {
return this.handle.then(function (col) { return col.find(query, options).toArray(); });
};
Collection.prototype.aggregate = function (pipeline, options) {
return this.handle.then(function (col) {
return col.aggregate(pipeline, options).toArray();
});
};
Collection.prototype.countDocuments = function (query, options) {
return this.handle.then(function (col) { return col.countDocuments(query, options); });
};
Collection.prototype.distinct = function (key, query, options) {
return this.handle.then(function (col) { return col.distinct(key, query, options); });
};
Collection.prototype.estimatedDocumentCount = function (query, options) {
return this.handle.then(function (col) { return col.estimatedDocumentCount(query, options); });
};
Collection.prototype.geoHaystackSearch = function (x, y, options) {
return this.handle.then(function (col) { return col.geoHaystackSearch(x, y, options); });
};
Collection.prototype.isCapped = function (options) {
return this.handle.then(function (col) { return col.isCapped(options); });
};
Collection.prototype.mapReduce = function (map, reduce, options) {
return this.handle.then(function (col) {
return col.mapReduce(map, reduce, options);
});
};
Collection.prototype.stats = function (options) {
return this.handle.then(function (col) { return col.stats(options); });
};
Collection.prototype.options = function (options) {
return this.handle.then(function (col) { return col.options(options); });
};
// Insert methods :
Collection.prototype.insertOne = function (doc, options) {
var _this = this;
return _1.verifyInsert(this, [doc])
.then(function () { return _this.handle; })
.then(function (col) { return col.insertOne(doc, options); })
.then(function (_a) {
var ops = _a.ops;
return ops[0];
});
};
Collection.prototype.insertMany = function (docs, options) {
var _this = this;
return _1.verifyInsert(this, docs)
.then(function () { return _this.handle; })
.then(function (col) { return col.insertMany(docs, options); })
.then(function (_a) {
var ops = _a.ops;
return ops;
});
};
// Update methods :
Collection.prototype.updateOne = function (filter, update, options) {
var _this = this;
return _1.verifyUpdate(this, filter, update, { many: false })
.then(function () { return _this.handle; })
.then(function (col) { return col.updateOne(filter, update, options); });
};
Collection.prototype.updateMany = function (filter, update, options) {
var _this = this;
return _1.verifyUpdate(this, filter, update, { many: true })
.then(function () { return _this.handle; })
.then(function (col) { return col.updateMany(filter, update, options); });
};
Collection.prototype.findOneAndUpdate = function (filter, update, options) {
var _this = this;
return _1.verifyUpdate(this, filter, update, {
many: false,
sort: options === null || options === void 0 ? void 0 : options.sort
})
.then(function () { return _this.handle; })
.then(function (col) { return col.findOneAndUpdate(filter, update, options); })
.then(function (_a) {
var value = _a.value;
return value !== null && value !== void 0 ? value : null;
});
};
// Deletion methods :
Collection.prototype.deleteOne = function (filter, options, deletedKeys) {
var _this = this;
if (deletedKeys === void 0) { deletedKeys = {}; }
return _1.verifyDelete(this, filter, deletedKeys, { many: false })
.then(function () { return _this.handle; })
.then(function (col) { return col.deleteOne(filter, options); });
};
Collection.prototype.deleteMany = function (filter, options, deletedKeys) {
var _this = this;
if (deletedKeys === void 0) { deletedKeys = {}; }
return _1.verifyDelete(this, filter, deletedKeys, { many: true })
.then(function () { return _this.handle; })
.then(function (col) { return col.deleteMany(filter, options); });
};
Collection.prototype.findOneAndDelete = function (filter, options, deletedKeys) {
var _this = this;
if (deletedKeys === void 0) { deletedKeys = {}; }
return _1.verifyDelete(this, filter, deletedKeys, {
many: false,
sort: options === null || options === void 0 ? void 0 : options.sort
})
.then(function () { return _this.handle; })
.then(function (col) { return col.findOneAndDelete(filter, options); })
.then(function (_a) {
var value = _a.value;
return value !== null && value !== void 0 ? value : null;
});
};
Collection.prototype.drop = function (options, deletedKeys) {
var _this = this;
if (deletedKeys === void 0) { deletedKeys = {}; }
return _1.verifyDelete(this, {}, deletedKeys, { many: true })
.then(function () { return _this.handle; })
.then(function (col) { return col.drop(options); });
};
// Indexation methods :
Collection.prototype.createIndex = function (fieldOrSpec, options) {
return this.handle.then(function (col) { return col.createIndex(fieldOrSpec, options); });
};
Collection.prototype.createIndexes = function (indexSpecs, options) {
return this.handle.then(function (col) { return col.createIndexes(indexSpecs, options); });
};
Collection.prototype.dropIndex = function (indexName, options) {
return this.handle.then(function (col) { return col.dropIndex(indexName, options); });
};
Collection.prototype.dropIndexes = function (options) {
return this.handle.then(function (col) { return col.dropIndexes(options); });
};
Collection.prototype.indexes = function (options) {
return this.handle.then(function (col) { return col.indexes(options); });
};
Collection.prototype.indexExists = function (indexes, options) {
return this.handle.then(function (col) { return col.indexExists(indexes, options); });
};
Collection.prototype.indexInformation = function (options) {
return this.handle.then(function (col) { return col.indexInformation(options); });
};
Collection.prototype.listIndexes = function (options) {
return this.handle.then(function (col) { return col.listIndexes(options).toArray(); });
};
Collection.prototype.reIndex = function (options) {
return this.handle.then(function (col) { return col.reIndex(options); });
};
return Collection;
}());
exports.Collection = Collection;