-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (34 loc) · 1.02 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
const Connection = require('./lib/connection'),
Definition = require('./lib/definition'),
ModelFactory = require('./lib/modelfactory');
class API {
constructor(config, definitions) {
if (!config || (typeof config !== 'string' && typeof config !== 'object'))
throw new TypeError('Expecting config to be a string or object');
if (!definitions || Array.isArray(definitions) === false)
throw new TypeError('Expecting definitions to be an array');
this.connection = new Connection(config);
this.definitions = buildDefinitions(definitions);
this.models = buildModels(this);
// Set up shorthand
for (var name in this.models) {
this[name] = this.models[name];
}
}
}
function buildDefinitions(definitions) {
let defs = {};
definitions.forEach(function(def) {
def = new Definition(def);
defs[def.name] = def;
})
return defs;
}
function buildModels(api) {
let models = {};
for (let name in api.definitions) {
models[name] = ModelFactory(api, api.definitions[name]);
}
return models;
}
module.exports = API