-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (44 loc) · 1.04 KB
/
index.js
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
'use strict';
require('es6-promise').polyfill();
var Mapper = require('./lib/mapper');
var cache = {};
function Factory(name) {
if (!name) {
name = Object.keys(cache)[0];
}
var mapper = cache[name];
if (!mapper) {
throw new Error('Mapper ' + name + ' is not exist');
}
return mapper;
}
/*
var opts = {
adaptor:'',
database: '',
driver:{},
pool:{},
plugins: {
pluginName: {
provider: 'plugin',
opts: {}
}
}
}
*/
Factory.create = function(mappers, opts) {
opts.database = opts.database || opts.driver.database;
var adaptor = require(opts.adaptor)(opts);
var mapper = new Mapper(opts.database, adaptor);
return new Promise(function(resolve, reject){
mapper.build(mappers).then(function() {
for (var key in opts.plugins) { // config plugins
var pluginOpts = opts.plugins[key];
mapper.plugin.set(key, require(pluginOpts.provider)(pluginOpts.opts || {}));
}
cache[opts.database] = mapper;
resolve(mapper);
}).catch(reject);
});
};
module.exports = Factory;