UNPKG

@iredium/butterfly

Version:
254 lines (253 loc) 11 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseSchema = void 0; var autopopulate = require("mongoose-autopopulate"); var mongoose = require("mongoose"); var uuid_1 = require("./_types/uuid"); var uuid_2 = require("../helpers/uuid"); uuid_1.registerType(mongoose); function convertUUIDToString(root) { var keys = Object.keys(root); if (root.constructor.name === 'Array') { keys = keys.map(function (k) { return Number(k); }); } for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { var key = keys_1[_i]; var type = root[key] ? root[key].constructor.name : null; var isArray = type === 'Array'; var isObject = type === 'object' || type === 'Object'; if (type === 'Binary') { try { root[key] = uuid_1.getter(root[key]); } catch (e) { // } } else if (isArray || isObject) { convertUUIDToString(root[key]); } } } var BaseSchema = /** @class */ (function (_super) { __extends(BaseSchema, _super); function BaseSchema(schema) { var _this = this; var baseOptions = { id: false, toObject: { virtuals: true, }, toJSON: { virtuals: true, transform: function (doc, ret, options) { convertUUIDToString(ret); ret.id = uuid_1.getter(ret.uuid); delete ret._id; delete ret.uuid; delete ret.password; delete ret.__v; return ret; } }, timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } }; var combinedSchema = __assign(__assign({}, schema), { uuid: { type: uuid_1.UUID, default: uuid_2.UUID.v4Base62, protect: true, unique: true, dropDups: true }, deleted_at: { type: Date, default: null, protect: true }, deleted_by: { type: String, default: null, hidden: true, protect: true } }); _this = _super.call(this, combinedSchema, baseOptions) || this; _this.wasNew = false; _this.populateUser = { select: 'id username first_name last_name default_address email' }; _this.defaultRouteKeyName = 'uuid'; _this.fillable = Object.keys(schema).filter(function (key) { return key != '_id' && key != '__v' && key != 'uuid' && key != 'created_at' && key != 'updated_at' && key != 'deleted_at' && !schema[key].guarded; }); _this.registerCommonStatics(); _this.registerCommonMethods(); _this.registerCommonPres(); _this.registerCommonVirtuals(); _this.registerPlugins(); return _this; } BaseSchema.prototype.registerCommonVirtuals = function () { this.virtual('id') .get(function () { // @ts-ignore return this.uuid; }).set(function (id) { // @ts-ignore this.set({ uuid: id }); }); }; BaseSchema.prototype.registerCommonStatics = function () { this.statics.getRouteKeyName = function () { return this.defaultRouteKeyName; }; }; BaseSchema.prototype.registerCommonMethods = function () { var baseSchema = this; this.methods.getRouteKeyName = function () { return baseSchema.defaultRouteKeyName; }; this.methods.promisify = function promisify(method) { var _this = this; return new Promise(function (resolve, reject) { _this[method](function (error, res) { if (error) { reject(error); } else { resolve(res); } }); }); }; this.methods.massAssign = function massAssign(data) { return __awaiter(this, void 0, void 0, function () { var allowedData, _i, _a, key; return __generator(this, function (_b) { try { allowedData = {}; for (_i = 0, _a = Object.keys(data); _i < _a.length; _i++) { key = _a[_i]; if (baseSchema.fillable.includes(key)) { allowedData[key] = data[key]; } } this.set(allowedData); return [2 /*return*/, this.save()]; } catch (e) { return [2 /*return*/, Promise.reject(e)]; } return [2 /*return*/]; }); }); }; this.methods.softDelete = function softDelete(user) { return __awaiter(this, void 0, void 0, function () { var e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); this.set({ deleted_at: Date.now(), deleted_by: user ? user.uuid : null }); if (!this['unIndex']) return [3 /*break*/, 2]; return [4 /*yield*/, this.promisify('unIndex')]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/, this.save()]; case 3: e_1 = _a.sent(); return [2 /*return*/, Promise.reject(e_1)]; case 4: return [2 /*return*/]; } }); }); }; this.methods.restore = function restore() { return __awaiter(this, void 0, void 0, function () { var e_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); this.set({ deleted_at: null }); if (!this['index']) return [3 /*break*/, 2]; return [4 /*yield*/, this.promisify('index')]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/, this.save()]; case 3: e_2 = _a.sent(); return [2 /*return*/, Promise.reject(e_2)]; case 4: return [2 /*return*/]; } }); }); }; }; BaseSchema.prototype.registerCommonPres = function () { this.pre('save', function (next) { this['wasNew'] = this.isNew; next(); }); }; BaseSchema.prototype.registerPlugins = function () { this.plugin(autopopulate); }; return BaseSchema; }(mongoose.Schema)); exports.BaseSchema = BaseSchema;