tsgoose
Version:
TypeScript decorators for Mongoose
73 lines (72 loc) • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
var data_1 = require("./data");
var mongoose = require("mongoose");
var TSGoose = /** @class */ (function () {
function TSGoose() {
}
TSGoose.getTSGooseModel = function () {
return getTSGooseModel(this);
};
return TSGoose;
}());
exports.TSGoose = TSGoose;
function getTSGooseSchema(t) {
var name = t.name;
if (!data_1.schemasInstances[name]) {
var schemaDef = data_1.schemas[name];
data_1.schemasInstances[name] = new mongoose.Schema(schemaDef, data_1.schemaOptions[name]);
var schema_1 = data_1.schemasInstances[name];
if (data_1.methods[name]) {
for (var methodPropertyKey in data_1.methods[name]) {
if (data_1.methods[name].hasOwnProperty(methodPropertyKey)) {
schema_1.methods[methodPropertyKey] = data_1.methods[name][methodPropertyKey];
}
}
}
if (data_1.statics[name]) {
for (var methodPropertyKey in data_1.statics[name]) {
if (data_1.statics[name].hasOwnProperty(methodPropertyKey)) {
schema_1.statics[methodPropertyKey] = data_1.statics[name][methodPropertyKey];
}
}
}
if (data_1.preHooks[name]) {
data_1.preHooks[name].forEach(function (hookEntry) {
schema_1.pre(hookEntry.name, hookEntry.method);
});
}
if (data_1.postHooks[name]) {
data_1.postHooks[name].forEach(function (hookEntry) {
schema_1.post(hookEntry.name, hookEntry.method);
});
}
if (data_1.queryHelpers[name]) {
data_1.queryHelpers[name].forEach(function (qEntry) {
schema_1.query[qEntry.name] = qEntry.method;
});
}
if (data_1.virtuals[name]) {
data_1.virtuals[name].forEach(function (virtual) {
if (virtual.get) {
schema_1.virtual(virtual.name).get(virtual.get);
}
if (virtual.set) {
schema_1.virtual(virtual.name).set(virtual.set);
}
});
}
}
return data_1.schemasInstances[name];
}
exports.getTSGooseSchema = getTSGooseSchema;
function getTSGooseModel(t) {
var name = t.name;
if (!data_1.models[name]) {
var schema = getTSGooseSchema(t);
data_1.models[name] = mongoose.model(name, schema);
}
return data_1.models[name];
}
exports.getTSGooseModel = getTSGooseModel;