UNPKG

mission.core

Version:
54 lines (53 loc) 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vendor_1 = require("../vendor"); class Repository { constructor(Dal, Models, Logger) { this.Dal = Dal; this.Models = Models; this.Logger = Logger; } static get Instance() { if (!this.singleton) { throw new Error(`Repository is not initialized. Please call 'Repository.init' method on bootstrap the application.`); } return this.singleton; } static init(config, modelPatterns, logger) { // (SequelizeStatic as any).cls = cls.createNamespace("sequelize-transaction"); const dal = new vendor_1.Sequelize(config); const models = {}; logger.log('Model file pattern to load ', modelPatterns); const files = Repository.getFiles(modelPatterns); logger.log('Loading model files...', files); files.forEach((file) => { const model = dal.import(file); models[model.name] = model; }); Object.keys(models) .forEach((modelName) => { if (typeof models[modelName].associate === 'function') { models[modelName].associate(models); } }); this.singleton = new Repository(dal, models, logger); return this.singleton; } static getFiles(patterns) { let results = []; if (patterns && patterns.length > 0) { patterns.forEach((pattern) => { results = results.concat(vendor_1.glob.sync(pattern, { sync: true })); }); } return results; } } exports.Repository = Repository; // TODO:Move to App Start up file. // const Repo = new Repository(DbConfig, StartUpLogger); // export const Models = Repo.Models; // export const Dal = Repo.Dal; /* https://github.com/sequelize/express-example */