breeze-sequelize
Version:
Breeze Sequelize server implementation
107 lines • 5.11 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const sequelize_1 = require("sequelize");
const breeze_client_1 = require("breeze-client");
const adapter_model_library_backing_store_1 = require("breeze-client/adapter-model-library-backing-store");
const dbUtils_1 = require("./dbUtils");
const MetadataMapper_1 = require("./MetadataMapper");
const _ = require("lodash");
const utils = require("./utils");
const ModelMapper_1 = require("./ModelMapper");
const log = utils.log;
/** Manages the Sequelize instance for Breeze query and save operations */
class SequelizeManager {
constructor(dbConfig, sequelizeOptions) {
const defaultOptions = {
dialect: "mysql",
port: 3306,
// omitNull: true,
logging: false,
dialectOptions: { decimalNumbers: true },
define: {
freezeTableName: true,
timestamps: false // deactivate the timestamp columns (createdAt, etc.)
}
};
const define = defaultOptions.define;
this.sequelizeOptions = _.extend(defaultOptions, sequelizeOptions || {});
this.sequelizeOptions.define = _.extend(define, (sequelizeOptions && sequelizeOptions.define) || {});
this.dbConfig = dbConfig;
this.sequelize = new sequelize_1.Sequelize(dbConfig.dbName, dbConfig.user, dbConfig.password, this.sequelizeOptions);
log.enabled = !!this.sequelizeOptions.logging;
adapter_model_library_backing_store_1.ModelLibraryBackingStoreAdapter.register(breeze_client_1.breeze.config);
}
/** Connect to the database */
authenticate() {
return __awaiter(this, void 0, void 0, function* () {
// check database connection
try {
yield this.sequelize.authenticate();
log('Connection has been established successfully.');
}
catch (err) {
log('Unable to connect to the database:', err);
throw err;
}
});
}
/** Create a new database */
createDb() {
return dbUtils_1.createDb(this.dbConfig, this.sequelizeOptions);
}
/** Import Sequelize models, create Breeze metadata from the models, and build the maps between them. */
importModels(modelDir, namespace) {
// load models into sequelize instance
ModelMapper_1.ModelMapper.loadSequelizeModels(this.sequelize, modelDir);
// add models to the metadata store
this.metadataStore = new breeze_client_1.MetadataStore();
const mm = new ModelMapper_1.ModelMapper(this.metadataStore);
mm.addModels(this.sequelize, namespace);
// create maps between the metadata and the models
this.importMetadata(this.metadataStore);
}
/** Convert Breeze metadata to Sequelize models */
importMetadata(breezeMetadata) {
const metadataMapper = new MetadataMapper_1.MetadataMapper(breezeMetadata, this.sequelize);
// TODO: should we merge here instead ; i.e. allow multiple imports...
this.models = this.resourceNameSqModelMap = metadataMapper.resourceNameSqModelMap;
this.entityTypeSqModelMap = metadataMapper.entityTypeSqModelMap;
this.metadataStore = metadataMapper.metadataStore;
}
/** Sync the Sequelize model with the database */
sync(shouldCreateDb, sequelizeOpts) {
return __awaiter(this, void 0, void 0, function* () {
if (shouldCreateDb) {
yield this.createDb();
}
return yield this.syncCore(this.sequelize, sequelizeOpts);
});
}
syncCore(sequelize, sequelizeOpts) {
return __awaiter(this, void 0, void 0, function* () {
const defaultOptions = { force: true };
sequelizeOpts = _.extend(defaultOptions, sequelizeOpts || {});
try {
yield sequelize.sync(sequelizeOpts);
log("schema created");
return sequelize;
}
catch (err) {
console.log("schema creation failed");
throw err;
}
});
}
}
exports.SequelizeManager = SequelizeManager;
SequelizeManager.Sequelize = sequelize_1.Sequelize;
//# sourceMappingURL=SequelizeManager.js.map