@azteam/mongo-model
Version:
N/A
784 lines (781 loc) • 37.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _lodash = _interopRequireDefault(require("lodash"));
var _constant = require("@azteam/constant");
var _error = require("@azteam/error");
var _excluded = ["force", "trash", "cache"],
_excluded2 = ["force", "trash"],
_excluded3 = ["force", "trash", "cache"],
_excluded4 = ["force", "trash", "cache"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return r; }; var t, r = {}, e = Object.prototype, n = e.hasOwnProperty, o = "function" == typeof Symbol ? Symbol : {}, i = o.iterator || "@@iterator", a = o.asyncIterator || "@@asyncIterator", u = o.toStringTag || "@@toStringTag"; function c(t, r, e, n) { Object.defineProperty(t, r, { value: e, enumerable: !n, configurable: !n, writable: !n }); } try { c({}, ""); } catch (t) { c = function c(t, r, e) { return t[r] = e; }; } function h(r, e, n, o) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype); return c(a, "_invoke", function (r, e, n) { var o = 1; return function (i, a) { if (3 === o) throw Error("Generator is already running"); if (4 === o) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var u = n.delegate; if (u) { var c = d(u, n); if (c) { if (c === f) continue; return c; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (1 === o) throw o = 4, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = 3; var h = s(r, e, n); if ("normal" === h.type) { if (o = n.done ? 4 : 2, h.arg === f) continue; return { value: h.arg, done: n.done }; } "throw" === h.type && (o = 4, n.method = "throw", n.arg = h.arg); } }; }(r, n, new Context(o || [])), !0), a; } function s(t, r, e) { try { return { type: "normal", arg: t.call(r, e) }; } catch (t) { return { type: "throw", arg: t }; } } r.wrap = h; var f = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var l = {}; c(l, i, function () { return this; }); var p = Object.getPrototypeOf, y = p && p(p(x([]))); y && y !== e && n.call(y, i) && (l = y); var v = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(l); function g(t) { ["next", "throw", "return"].forEach(function (r) { c(t, r, function (t) { return this._invoke(r, t); }); }); } function AsyncIterator(t, r) { function e(o, i, a, u) { var c = s(t[o], t, i); if ("throw" !== c.type) { var h = c.arg, f = h.value; return f && "object" == _typeof(f) && n.call(f, "__await") ? r.resolve(f.__await).then(function (t) { e("next", t, a, u); }, function (t) { e("throw", t, a, u); }) : r.resolve(f).then(function (t) { h.value = t, a(h); }, function (t) { return e("throw", t, a, u); }); } u(c.arg); } var o; c(this, "_invoke", function (t, n) { function i() { return new r(function (r, o) { e(t, n, r, o); }); } return o = o ? o.then(i, i) : i(); }, !0); } function d(r, e) { var n = e.method, o = r.i[n]; if (o === t) return e.delegate = null, "throw" === n && r.i["return"] && (e.method = "return", e.arg = t, d(r, e), "throw" === e.method) || "return" !== n && (e.method = "throw", e.arg = new TypeError("The iterator does not provide a '" + n + "' method")), f; var i = s(o, r.i, e.arg); if ("throw" === i.type) return e.method = "throw", e.arg = i.arg, e.delegate = null, f; var a = i.arg; return a ? a.done ? (e[r.r] = a.value, e.next = r.n, "return" !== e.method && (e.method = "next", e.arg = t), e.delegate = null, f) : a : (e.method = "throw", e.arg = new TypeError("iterator result is not an object"), e.delegate = null, f); } function w(t) { this.tryEntries.push(t); } function m(r) { var e = r[4] || {}; e.type = "normal", e.arg = t, r[4] = e; } function Context(t) { this.tryEntries = [[-1]], t.forEach(w, this), this.reset(!0); } function x(r) { if (null != r) { var e = r[i]; if (e) return e.call(r); if ("function" == typeof r.next) return r; if (!isNaN(r.length)) { var o = -1, a = function e() { for (; ++o < r.length;) if (n.call(r, o)) return e.value = r[o], e.done = !1, e; return e.value = t, e.done = !0, e; }; return a.next = a; } } throw new TypeError(_typeof(r) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, c(v, "constructor", GeneratorFunctionPrototype), c(GeneratorFunctionPrototype, "constructor", GeneratorFunction), c(GeneratorFunctionPrototype, u, GeneratorFunction.displayName = "GeneratorFunction"), r.isGeneratorFunction = function (t) { var r = "function" == typeof t && t.constructor; return !!r && (r === GeneratorFunction || "GeneratorFunction" === (r.displayName || r.name)); }, r.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, c(t, u, "GeneratorFunction")), t.prototype = Object.create(v), t; }, r.awrap = function (t) { return { __await: t }; }, g(AsyncIterator.prototype), c(AsyncIterator.prototype, a, function () { return this; }), r.AsyncIterator = AsyncIterator, r.async = function (t, e, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(h(t, e, n, o), i); return r.isGeneratorFunction(e) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, g(v), c(v, u, "Generator"), c(v, i, function () { return this; }), c(v, "toString", function () { return "[object Generator]"; }), r.keys = function (t) { var r = Object(t), e = []; for (var n in r) e.unshift(n); return function t() { for (; e.length;) if ((n = e.pop()) in r) return t.value = n, t.done = !1, t; return t.done = !0, t; }; }, r.values = x, Context.prototype = { constructor: Context, reset: function reset(r) { if (this.prev = this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(m), !r) for (var e in this) "t" === e.charAt(0) && n.call(this, e) && !isNaN(+e.slice(1)) && (this[e] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0][4]; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(r) { if (this.done) throw r; var e = this; function n(t) { a.type = "throw", a.arg = r, e.next = t; } for (var o = e.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i[4], u = this.prev, c = i[1], h = i[2]; if (-1 === i[0]) return n("end"), !1; if (!c && !h) throw Error("try statement without catch or finally"); if (null != i[0] && i[0] <= u) { if (u < c) return this.method = "next", this.arg = t, n(c), !0; if (u < h) return n(h), !1; } } }, abrupt: function abrupt(t, r) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var n = this.tryEntries[e]; if (n[0] > -1 && n[0] <= this.prev && this.prev < n[2]) { var o = n; break; } } o && ("break" === t || "continue" === t) && o[0] <= r && r <= o[2] && (o = null); var i = o ? o[4] : {}; return i.type = t, i.arg = r, o ? (this.method = "next", this.next = o[2], f) : this.complete(i); }, complete: function complete(t, r) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && r && (this.next = r), f; }, finish: function finish(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[2] === t) return this.complete(e[4], e[3]), m(e), f; } }, "catch": function _catch(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[0] === t) { var n = e[4]; if ("throw" === n.type) { var o = n.arg; m(e); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(r, e, n) { return this.delegate = { i: x(r), r: e, n: n }, "next" === this.method && (this.arg = t), f; } }, r; }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var DataRepository = /*#__PURE__*/function () {
function DataRepository(_ref) {
var activeModel = _ref.activeModel,
trashModel = _ref.trashModel,
redis = _ref.redis;
var dummyData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
_classCallCheck(this, DataRepository);
this._model = activeModel;
this._trashModel = trashModel;
this.redis = redis;
this._dummyData(dummyData);
}
return _createClass(DataRepository, [{
key: "queryWithCache",
value: function () {
var _queryWithCache = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(cacheConfig, queryBuilder) {
var redis, cacheName, data;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
if (!cacheConfig.key) {
_context.next = 13;
break;
}
redis = cacheConfig.redis ? cacheConfig.redis : this.redis;
if (!redis) {
_context.next = 13;
break;
}
cacheName = "".concat(cacheConfig.trash ? 'trash_' : '').concat(cacheConfig.key);
_context.next = 6;
return redis.get(cacheName);
case 6:
data = _context.sent;
if (data) {
_context.next = 12;
break;
}
_context.next = 10;
return queryBuilder;
case 10:
data = _context.sent;
redis.set(cacheName, data, cacheConfig.ttl ? cacheName.ttl : 300); // default cache 5 minutes
case 12:
return _context.abrupt("return", data);
case 13:
return _context.abrupt("return", queryBuilder);
case 14:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function queryWithCache(_x, _x2) {
return _queryWithCache.apply(this, arguments);
}
return queryWithCache;
}()
}, {
key: "getModel",
value: function getModel() {
if (this._model) {
return this._model;
}
throw new _error.ErrorException(_error.NOT_INIT_METHOD);
}
}, {
key: "getTrashModel",
value: function getTrashModel() {
if (this._trashModel) {
return this._trashModel;
}
throw new _error.ErrorException(_error.NOT_INIT_METHOD);
}
}, {
key: "getUniqueFields",
value: function () {
var _getUniqueFields = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
var indexes;
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return this.getModel().collection.getIndexes({
full: true
});
case 2:
indexes = _context2.sent;
return _context2.abrupt("return", indexes.filter(function (index) {
return index.unique;
}).map(function (index) {
return Object.keys(index.key);
}).flat());
case 4:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function getUniqueFields() {
return _getUniqueFields.apply(this, arguments);
}
return getUniqueFields;
}()
}, {
key: "_dummyData",
value: function _dummyData(data) {
var _this = this;
data.map(/*#__PURE__*/function () {
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(item) {
var obj, Model, model;
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return _this.findOne(item.query);
case 2:
obj = _context3.sent;
if (obj) {
_context3.next = 9;
break;
}
Model = _this.getModel();
model = new Model();
_lodash["default"].map(item.data, function (value, key) {
model[key] = value;
});
_context3.next = 9;
return model.save();
case 9:
case "end":
return _context3.stop();
}
}, _callee3);
}));
return function (_x3) {
return _ref2.apply(this, arguments);
};
}());
}
}, {
key: "count",
value: function count(query) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var queryBuilder = this.getModel().countDocuments(query, options);
return this.queryWithCache(_objectSpread(_objectSpread({}, options.cache), {}, {
trash: false
}), queryBuilder);
}
}, {
key: "countTrash",
value: function countTrash() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var queryBuilder = this.getTrashModel().countDocuments(query, options);
return this.queryWithCache(_objectSpread(_objectSpread({}, options.cache), {}, {
trash: true
}), queryBuilder);
}
}, {
key: "find",
value: function find() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var force = options.force,
trash = options.trash,
cache = options.cache,
newOpts = _objectWithoutProperties(options, _excluded);
if (newOpts.lean !== false) {
newOpts.lean = true;
newOpts.leanWithId = true;
}
if (!newOpts.sort) {
if (trash) {
newOpts.sort = {
deleted_at: 'desc'
};
} else {
newOpts.sort = {
_id: 'desc'
};
}
}
var limit = newOpts.limit && newOpts.limit < 10000 ? newOpts.limit : 10000;
if (newOpts.page) {
if (newOpts.page * limit >= 60000) {
newOpts.allowDiskUse = true;
}
} else {
newOpts.page = 1;
}
var queryBuilder = null;
if (trash) {
queryBuilder = this.getTrashModel().paginate(query, newOpts);
} else {
queryBuilder = this.getModel().paginate(query, newOpts);
}
return this.queryWithCache(_objectSpread(_objectSpread({}, cache), {}, {
trash: trash
}), queryBuilder);
}
}, {
key: "findTrash",
value: function findTrash() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.find(query, _objectSpread(_objectSpread({}, options), {}, {
trash: true
}));
}
}, {
key: "findAvailable",
value: function findAvailable() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.find(_objectSpread(_objectSpread({}, query), {}, {
status: _constant.AVAILABLE_STATUS.AVAILABLE
}), options);
}
}, {
key: "findUnavailable",
value: function findUnavailable() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.find(_objectSpread(_objectSpread({}, query), {}, {
status: _constant.AVAILABLE_STATUS.UNAVAILABLE
}), options);
}
}, {
key: "findWaiting",
value: function findWaiting() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.find(_objectSpread(_objectSpread({}, query), {}, {
status: _constant.AVAILABLE_STATUS.WAITING
}), options);
}
// findNear(geo = {}, query = {}, options = {}) {
// geo = {
// name: 'geo',
// coords: [],
// ...geo,
// };
//
// if (geo.maxDistance) {
// query[geo.name] = {
// $nearSphere: {
// $geometry: {
// type: 'Point',
// coordinates: geo.coords,
// },
// $maxDistance: geo.maxDistance,
// },
// };
// } else {
// query[geo.name] = {
// $nearSphere: {
// $geometry: {
// type: 'Point',
// coordinates: geo.coords,
// },
// },
// };
// }
//
// return this.find(query, {
// ...options,
// forceCountFn: true,
// });
// }
}, {
key: "explain",
value: function explain() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var force = options.force,
trash = options.trash,
newOpts = _objectWithoutProperties(options, _excluded2);
var queryBuilder = null;
if (trash) {
queryBuilder = this.getTrashModel().findOne(query, null, _objectSpread(_objectSpread({}, newOpts), {}, {
explain: true
}));
} else {
queryBuilder = this.getModel().findOne(query, null, _objectSpread(_objectSpread({}, newOpts), {}, {
explain: true
}));
}
return queryBuilder;
}
}, {
key: "findOne",
value: function findOne() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var force = options.force,
trash = options.trash,
cache = options.cache,
newOpts = _objectWithoutProperties(options, _excluded3);
var queryBuilder = null;
if (trash) {
queryBuilder = this.getTrashModel().findOne(query, null, newOpts);
} else {
queryBuilder = this.getModel().findOne(query, null, newOpts);
}
return this.queryWithCache(_objectSpread(_objectSpread({}, cache), {}, {
trash: trash
}), queryBuilder);
}
}, {
key: "findAll",
value: function findAll() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var force = options.force,
trash = options.trash,
cache = options.cache,
newOpts = _objectWithoutProperties(options, _excluded4);
var queryBuilder = null;
if (trash) {
queryBuilder = this.getTrashModel().find(query);
} else {
queryBuilder = this.getModel().find(query);
}
if (newOpts.sort) {
queryBuilder = queryBuilder.sort(newOpts.sort);
}
if (newOpts.limit) {
queryBuilder = queryBuilder.limit(newOpts.limit);
}
if (newOpts.select) {
queryBuilder = queryBuilder.select(newOpts.select);
}
if (newOpts.select || newOpts.lean) {
queryBuilder = queryBuilder.lean({
virtuals: true
});
}
return this.queryWithCache(_objectSpread(_objectSpread({}, cache), {}, {
trash: trash
}), queryBuilder);
}
}, {
key: "findOneAvailable",
value: function findOneAvailable() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.findOne(_objectSpread(_objectSpread({}, query), {}, {
status: _constant.AVAILABLE_STATUS.AVAILABLE
}), options);
}
}, {
key: "findOneUnavailable",
value: function findOneUnavailable() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.findOne(_objectSpread(_objectSpread({}, query), {}, {
status: _constant.AVAILABLE_STATUS.UNAVAILABLE
}), options);
}
}, {
key: "findOneWaiting",
value: function findOneWaiting() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.findOne(_objectSpread(_objectSpread({}, query), {}, {
status: _constant.AVAILABLE_STATUS.WAITING
}), options);
}
}, {
key: "findOneTrash",
value: function findOneTrash() {
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.findOne(query, _objectSpread(_objectSpread({}, options), {}, {
trash: true
}));
}
// findOneNear(geo = {}, query = {}, options = {}) {
// geo = {
// name: 'geo',
// coords: [],
// ...geo,
// };
//
// if (geo.maxDistance) {
// query[geo.name] = {
// $nearSphere: {
// $geometry: {
// type: 'Point',
// coordinates: geo.coords,
// },
// $maxDistance: geo.maxDistance,
// },
// };
// } else {
// query[geo.name] = {
// $nearSphere: {
// $geometry: {
// type: 'Point',
// coordinates: geo.coords,
// },
// },
// };
// }
//
// return this.findOne(query, options);
// }
}, {
key: "findOneById",
value: function findOneById(id) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.findOne({
_id: id
}, options);
}
}, {
key: "findOneTrashById",
value: function findOneTrashById(id) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.findOneTrash({
_id: id
}, options);
}
}, {
key: "findOneBySlug",
value: function () {
var _findOneBySlug = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(slug) {
var options,
_args4 = arguments;
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
options = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {};
return _context4.abrupt("return", this.findOne({
$or: [{
slug: slug
}, {
related_slugs: slug
}]
}, options));
case 2:
case "end":
return _context4.stop();
}
}, _callee4, this);
}));
function findOneBySlug(_x4) {
return _findOneBySlug.apply(this, arguments);
}
return findOneBySlug;
}()
}, {
key: "findOneOrCreate",
value: function () {
var _findOneOrCreate = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(query, data) {
var options,
model,
_args5 = arguments;
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
while (1) switch (_context5.prev = _context5.next) {
case 0:
options = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : {};
_context5.next = 3;
return this.findOne(query, options);
case 3:
model = _context5.sent;
if (model) {
_context5.next = 8;
break;
}
_context5.next = 7;
return this.create(data);
case 7:
model = _context5.sent;
case 8:
return _context5.abrupt("return", model);
case 9:
case "end":
return _context5.stop();
}
}, _callee5, this);
}));
function findOneOrCreate(_x5, _x6) {
return _findOneOrCreate.apply(this, arguments);
}
return findOneOrCreate;
}()
}, {
key: "create",
value: function create(data) {
var allow = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var createdId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var Model = this.getModel();
var model = new Model();
if (createdId) {
model.created_id = createdId;
model.modified_id = createdId;
}
return model.loadData(data, allow).save();
}
}, {
key: "createByUser",
value: function createByUser(createdId) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var allow = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
return this.create(data, allow, createdId);
}
}, {
key: "update",
value: function () {
var _update = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(query, data) {
var model;
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
while (1) switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return this.findOne(query);
case 2:
model = _context6.sent;
return _context6.abrupt("return", model.modify(data));
case 4:
case "end":
return _context6.stop();
}
}, _callee6, this);
}));
function update(_x7, _x8) {
return _update.apply(this, arguments);
}
return update;
}()
}, {
key: "updateById",
value: function () {
var _updateById = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(id, data) {
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
while (1) switch (_context7.prev = _context7.next) {
case 0:
return _context7.abrupt("return", this.update({
_id: id
}, data));
case 1:
case "end":
return _context7.stop();
}
}, _callee7, this);
}));
function updateById(_x9, _x0) {
return _updateById.apply(this, arguments);
}
return updateById;
}()
}, {
key: "fix",
value: function () {
var _fix = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(query, data) {
var model;
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
while (1) switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return this.findOne(query);
case 2:
model = _context8.sent;
return _context8.abrupt("return", model.fix(data));
case 4:
case "end":
return _context8.stop();
}
}, _callee8, this);
}));
function fix(_x1, _x10) {
return _fix.apply(this, arguments);
}
return fix;
}()
}, {
key: "fixById",
value: function () {
var _fixById = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(id, data) {
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
while (1) switch (_context9.prev = _context9.next) {
case 0:
return _context9.abrupt("return", this.fix({
_id: id
}, data));
case 1:
case "end":
return _context9.stop();
}
}, _callee9, this);
}));
function fixById(_x11, _x12) {
return _fixById.apply(this, arguments);
}
return fixById;
}()
}, {
key: "updateAll",
value: function () {
var _updateAll = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee0(query, data) {
return _regeneratorRuntime().wrap(function _callee0$(_context0) {
while (1) switch (_context0.prev = _context0.next) {
case 0:
return _context0.abrupt("return", this.getModel().updateMany(query, data));
case 1:
case "end":
return _context0.stop();
}
}, _callee0, this);
}));
function updateAll(_x13, _x14) {
return _updateAll.apply(this, arguments);
}
return updateAll;
}()
}, {
key: "delete",
value: function () {
var _delete2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee1(obj, deletedId) {
return _regeneratorRuntime().wrap(function _callee1$(_context1) {
while (1) switch (_context1.prev = _context1.next) {
case 0:
_context1.next = 2;
return this.getTrashModel().create(_objectSpread(_objectSpread({}, obj.toJSON()), {}, {
deleted_id: deletedId,
deleted_at: Math.floor(Date.now() / 1000)
}));
case 2:
_context1.next = 4;
return this.getModel().deleteOne({
_id: obj.id
});
case 4:
case "end":
return _context1.stop();
}
}, _callee1, this);
}));
function _delete(_x15, _x16) {
return _delete2.apply(this, arguments);
}
return _delete;
}()
}, {
key: "restore",
value: function () {
var _restore = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(obj, restoredId) {
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
while (1) switch (_context10.prev = _context10.next) {
case 0:
_context10.next = 2;
return this.getModel().create(_objectSpread(_objectSpread({}, obj.toJSON()), {}, {
modified_id: restoredId
}));
case 2:
_context10.next = 4;
return this.getTrashModel().deleteOne({
_id: obj.id
});
case 4:
case "end":
return _context10.stop();
}
}, _callee10, this);
}));
function restore(_x17, _x18) {
return _restore.apply(this, arguments);
}
return restore;
}()
}, {
key: "destroy",
value: function () {
var _destroy = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(id) {
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
while (1) switch (_context11.prev = _context11.next) {
case 0:
_context11.next = 2;
return this.getModel().deleteOne({
_id: id
});
case 2:
_context11.next = 4;
return this.getTrashModel().deleteOne({
_id: id
});
case 4:
case "end":
return _context11.stop();
}
}, _callee11, this);
}));
function destroy(_x19) {
return _destroy.apply(this, arguments);
}
return destroy;
}()
}, {
key: "destroyAll",
value: function () {
var _destroyAll = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12(query) {
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
while (1) switch (_context12.prev = _context12.next) {
case 0:
_context12.next = 2;
return this.getModel().deleteMany(query);
case 2:
_context12.next = 4;
return this.getTrashModel().deleteMany(query);
case 4:
case "end":
return _context12.stop();
}
}, _callee12, this);
}));
function destroyAll(_x20) {
return _destroyAll.apply(this, arguments);
}
return destroyAll;
}()
}, {
key: "cleanTrash",
value: function () {
var _cleanTrash = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
var durationDay,
deletedAt,
_args13 = arguments;
return _regeneratorRuntime().wrap(function _callee13$(_context13) {
while (1) switch (_context13.prev = _context13.next) {
case 0:
durationDay = _args13.length > 0 && _args13[0] !== undefined ? _args13[0] : 30;
deletedAt = Math.floor(Date.now() / 1000) - 86400 * durationDay;
_context13.next = 4;
return this.getTrashModel().deleteMany({
deleted_at: {
$lte: deletedAt
}
});
case 4:
case "end":
return _context13.stop();
}
}, _callee13, this);
}));
function cleanTrash() {
return _cleanTrash.apply(this, arguments);
}
return cleanTrash;
}()
}]);
}();
var _default = exports["default"] = DataRepository;