ts-db-helper
Version:
Simple ORM based on TypeScript
144 lines • 5.26 kB
JavaScript
import { DbHelperModel } from '../models/db-helper-model.model';
import { DataModel } from '../models/structure/data-model.model';
import { BadColumnDeclarationError } from '../errors/bad-column-declaration.error';
import { BadTableDeclarationError } from '../errors/bad-table-declaration.error';
/**
* @private
* @class ModelManager
*
* @description
* This class is a singleton collecting data model
* This manager has not to be exposed, it is used to provide DataModel
* every time it is necessary
*
* @author Olivier Margarit
* @since 0.1
*/
var ModelManager = /** @class */ (function () {
function ModelManager() {
/**
* @private
* @property {{[index: string]: DbTable}} tables hashmap of table names to their datamodel
*/
this.tables = {};
/**
* @private
* @property {{[index: string]: string}} models hashmap of model names with theirs linked table name
*/
this.models = {};
}
/**
* @static
* @public
* @method getInstance methode that return the manager instance
*
* @return {ModelManager} the unique instance
*/
ModelManager.getInstance = function () {
return ModelManager.instance;
};
/**
* @public
* @method addModel this method is part of private API, it is called by
* decorator to add new table to model
*
* @param {DbTable} dbTable newModel model extending DbHelperModel
*/
ModelManager.prototype.addModel = function (dbTable) {
this.tables[dbTable.name] = dbTable;
this.models[dbTable.modelName] = dbTable.name;
};
/**
* @public
* @method getColumnNameForField is a part of the private API
* This method is called by column decorators to add field to
* the Ddate model tables
*
* @param {{new(): DbHelperModel}} model model extending DbHelperModel
* @param {string} fieldName field to add to the model table
*
* @return {string} the column name for field "fieldName" of the model "model"
*/
ModelManager.prototype.getColumnNameForField = function (model, fieldName) {
if (!this.models.hasOwnProperty(model.name)) {
var error = new BadColumnDeclarationError('Did you forget to declare model: ' +
model.name + '\n Check @Table déclaration on this model');
throw (error);
}
var tableName = this.models[model.name];
var table = this.tables[tableName];
if (!table.fields.hasOwnProperty(fieldName)) {
var error = new BadTableDeclarationError('Did you forget to declare column for field "' +
fieldName + '" of model "' + model.name + ' - tableName : ' + tableName + ' table : ' + table + '' +
'"\n Check @Column déclaration on this model');
throw (error);
}
return table.fields[fieldName].name;
};
/**
* @public
* @method getDataModel get datamodel for model migrations
*
* @return {DataModel} data model generated by decorators
*/
ModelManager.prototype.getDataModel = function () {
return new DataModel(this.tables);
};
/**
* @public
* @method getModelCount return the number of model with decorators
* This method is used to comput minor version of the DataModel to automigrate
* the model. The feature is deactivable from config.
*
* @return {number} the number of table
*/
ModelManager.prototype.getModelCount = function () {
return Object.getOwnPropertyNames(this.models).length;
};
/**
* @public
* @method getModel this is a private API to get model and use it to build queries
*
* @param {string | DbHelperModel | {new(): DbHelperModel }} model reference to the model
*
* @return {DbTable} the table declared for the model
*/
ModelManager.prototype.getModel = function (model) {
if (model instanceof String) {
return this.tables[model];
}
else if (model instanceof DbHelperModel) {
return this.tables[this.getTable(model.constructor)];
}
else {
return this.tables[this.getTable(model)];
}
};
/**
* @public
* @method getTables return the table name linked to the model
*
* @param {{new(): DbHelperModel}} model model extending DbHelperModel
*
* @return {string} table name
*/
ModelManager.prototype.getTable = function (model) {
return this.models[model.name];
};
/**
* @static
* @public
* @property {string} version the current data model version, this is provided by the
* module configuration, @see NgDbHelperModuleConfig
*/
ModelManager.version = '';
/**
* @static
* @private
* @property {ModelManager} instance private reference to the model manager instance
*/
ModelManager.instance = new ModelManager();
return ModelManager;
}());
export { ModelManager };
//# sourceMappingURL=model-manager.js.map