all files / generators/api/repositories/ sequelize.js

100% Statements 31/31
100% Branches 0/0
100% Functions 15/15
100% Lines 26/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                                                                                         
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../../../commons/utils/utils");
var schemaUtil_1 = require("../../../commons/utils/schemaUtil");
/**
 * Class which implements the logic for generating valid sequelize repositories.
 *
 * @export
 * @class SequelizeRepositoryGenerator
 */
var SequelizeRepositoryGenerator = (function () {
    function SequelizeRepositoryGenerator() {
    }
    /**
     * Generate repositories for a given schema.
     *
     * @param {Schema} schema - api schema
     * @return {{name: string, content: string}[]}
     */
    SequelizeRepositoryGenerator.prototype.getRepositories = function (schema) {
        var _this = this;
        var repositories = [];
        schema.forEach(function (table) {
            repositories.push({
                name: utils_1.default.toTitleCase(table.name) + "Repository.js",
                content: _this.getRepositoryForTable(table),
            });
        });
        return repositories;
    };
    /**
     * Generate repository for a given table.
     *
     * @param {Table} table - schema for table
     * @return {{name: string, content: string}}
     */
    SequelizeRepositoryGenerator.prototype.getRepositoryForTable = function (table) {
        var related = this.getRelatedEntities(table);
        var entity = utils_1.default.toTitleCase(table.name);
        return "const " + entity + " = require('../models')." + entity.toLowerCase() + ";\n" + related.names.map(function (name) { return "const " + name + " = require('../models')." + name.toLowerCase() + ";"; }).join('\n') + "\n\nclass " + entity + "Repository {\n  get(id) {\n    return " + entity + ".findOne({where: {id}, include: [" + related.includes.join(', ') + "]});\n  }\n\n  getAll(offset, limit) {\n    return " + entity + ".findAll({limit, offset, include: [" + related.includes.join(', ') + "]});\n  }\n\n  save(data) {\n    return " + entity + ".create(data).then((created) => {\n      return created.id;\n    });\n  }\n\n  update(id, data) {\n    return " + entity + ".findOne({where: {id}}).then(\n      (" + entity + ") => {\n        return " + entity + ".update(data, {where: {id}});\n      }\n    )\n  }\n\n  delete(id) {\n    return " + entity + ".destroy({where: {id}});\n  }\n\n  exists(id) {\n    return " + entity + ".count({where: {id}}).then((count) => Boolean(count));\n  }\n\n  count() {\n    return " + entity + ".count();\n  }\n}\n\nmodule.exports = new " + entity + "Repository();\n";
    };
    /**
     * Generate repositories factory.
     *
     * @param {Schema} schema - api schema
     * @return {{name: string, content: string}}
     */
    SequelizeRepositoryGenerator.prototype.getRepositoryFactory = function (schema) {
        var models = schema.map(function (table) { return utils_1.default.toTitleCase(table.name); });
        return {
            name: 'repositoryFactory.js',
            content: models.map(function (name) { return "const " + name.toLowerCase() + "Repository = require('./" + name + "Repository');"; }).join('\n') + "\n\nclass RepositoryFactory {\n  getRepositoryForModel(model) {\n    switch(model) {\n" + models.map(function (name) { return "      case '" + name + "':\n        return " + name.toLowerCase() + "Repository;"; }).join('\n') + "\n      default:\n        throw new Error('Repository not implemented for this model');\n    }\n  }\n}\n\nmodule.exports = new RepositoryFactory();\n\n",
        };
    };
    /**
     * Format related entities.
     *
     * @param {Table} table
     * @returns {{names: string[]; includes: string[]}}
     */
    SequelizeRepositoryGenerator.prototype.getRelatedEntities = function (table) {
        var relations = schemaUtil_1.default.getRelatedTablesForTable(table);
        return {
            names: relations.map(function (column) { return utils_1.default.toTitleCase(column.dataType.type); }).filter(function (item, index, array) { return index === array.indexOf(item); }),
            includes: relations.map(function (column) { return "{model: " + utils_1.default.toTitleCase(column.dataType.type) + ", as: '" + column.name + "'}"; }).filter(function (item, index, array) { return index === array.indexOf(item); }),
        };
    };
    return SequelizeRepositoryGenerator;
}());
exports.default = SequelizeRepositoryGenerator;