-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
48 lines (40 loc) · 941 Bytes
/
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';
const orm = require('./lib/orm');
module.exports = (initialConfigs) => {
const databases = {};
let first;
function register(configs) {
if (!configs) return;
// Object to Array
if (!Array.isArray(configs)) {
configs = [configs];
}
configs.forEach((c, i) => {
const name = c.name || c.db || c.database;
if (databases[name]) {
throw new Error(`Duplicate name: '${name}'`);
}
if (!first && i === 0) {
first = name;
}
});
Object.assign(databases, orm(configs));
}
function database(name) {
// Default is first database
name = name || first;
return databases[name];
}
function middleware(ctx, next) {
if (ctx.orm) return next();
ctx.orm = database;
return next();
}
register(initialConfigs);
return {
register,
database,
middleware
};
};
module.exports.Sequelize = require('sequelize');