UNPKG

respond-framework

Version:
81 lines (80 loc) 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _mongodb = require("mongodb"); var _constants = require("../helpers/constants.js"); var _modelMock = require("./model.mock.js"); var _toFromObjectIds = require("./helpers/toFromObjectIds.js"); var _cloneDeep = require("../proxy/helpers/cloneDeep.js"); var _default = exports.default = !_constants.isProd ? _modelMock.default : { async save(moreDoc) { if (moreDoc) { Object.assign(this, moreDoc); } this.updatedAt = new Date(); this.createdAt ??= this.updatedAt; this.id ??= new _mongodb.ObjectId().toString(); const { id, _id: _, ...doc } = this; const selector = (0, _toFromObjectIds.toObjectIdsSelector)({ id: this.id }); await this.db[this._name].mongo().updateOne(selector, { $set: (0, _toFromObjectIds.toObjectIds)(doc) }, { upsert: true }); return this; }, async saveSafe(moreDoc) { if (moreDoc) { Object.assign(this, moreDoc); } this.updatedAt = new Date(); this.createdAt ??= this.updatedAt; const { id, _id: _, roles: __, ...doc } = this; const selector = (0, _toFromObjectIds.toObjectIdsSelector)({ id: this.id }); await this.db[this._name].mongo().updateOne(selector, { $set: (0, _toFromObjectIds.toObjectIds)(doc) }, { upsert: true }); return this; }, clone() { return (0, _cloneDeep.default)(this); }, async remove() { const selector = (0, _toFromObjectIds.toObjectIdsSelector)({ id: this.id }); await this.db[this._name].mongo().deleteOne(selector); return { id: this.id }; }, get super() { if (this._super) return this._super; const proto = Object.getPrototypeOf(Object.getPrototypeOf(this)); const proxy = new Proxy({}, { get: (_, k) => proto[k].bind(this) }); Object.defineProperty(this, '_super', { value: proxy, enumerable: false }); return this._super; } };