vitamin
Version:
Data Mapper library for Node.js applications
74 lines (57 loc) • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var mappers = {};
var connections = {};
// exports
exports.default = {
/**
* Register a connection object
*
* @param {String} name
* @param {Object} object
* @return connection
*/
connection: function connection() {
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
var object = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
name = name.toLowerCase();
if (object != null) {
if (connections[name]) throw new Error(name + " connection is already defined");
connections[name] = object;
}
if (!connections[name]) throw new Error(name + " connection is not yet defined");
return connections[name];
},
/**
* Set a mapper in the registry
*
* @param {String} name
* @param {Mapper} mapper
* @return mapper
*/
set: function set(name, mapper) {
if (this.has(name)) throw new Error(name + " is already defined in the registry");
return mappers[mapper.name = name.toLowerCase()] = mapper;
},
/**
* Get the mapper by name
*
* @param {String} name
* @return mapper
*/
get: function get(name) {
if (!this.has(name)) throw new Error(name + " is not defined in the registry");
return mappers[name.toLowerCase()];
},
/**
* Determine if the mapper name exists in the registry
*
* @param {String} name
* @return boolean
*/
has: function has(name) {
return !!mappers[name.toLowerCase()];
}
};