moringa
Version:
Kamboja helper for MongooseJS
39 lines (38 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var Shortid = require("shortid");
var SchemaGenerator = (function () {
function SchemaGenerator(pathResolver, converter) {
this.pathResolver = pathResolver;
this.converter = converter;
}
SchemaGenerator.prototype.generate = function (clazz) {
var _this = this;
var schema = {};
if (!clazz.properties)
return;
//shortid
if (clazz.decorators && clazz.decorators.some(function (x) { return x.name == "shortid"; })) {
schema._id = {
type: String,
default: Shortid.generate
};
}
clazz.properties.forEach(function (x) {
var type = _this.getType(x, clazz.name);
schema[x.name] = _this.converter.convert(type, clazz.name);
});
return schema;
};
SchemaGenerator.prototype.getType = function (property, typeName) {
var decorators = property.decorators.filter(function (x) { return x.name == "type"; });
if (decorators.length > 1)
throw new Error("Multiple @type found in [" + typeName + "]");
var decorator = decorators[0];
var parameter = decorator.parameters[0];
var type = parameter.value;
return type;
};
return SchemaGenerator;
}());
exports.SchemaGenerator = SchemaGenerator;