This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
84 lines (63 loc) · 1.91 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"use strict";
/**
* Thinky Loader
*
* @mwielbut
*/
let _ = require('lodash');
let Thinky = require('thinky');
let requireAll = require('require-all');
let loader = {
thinky: null,
models:
{}
};
loader.initialize = function(config, thinky)
{
loader.thinky = thinky || new Thinky(config.thinky.rethinkdb);
return loader
.thinky
.dbReady()
.then(function()
{
if (config.debug)
{
console.dir("DB Ready");
}
if (config.debug)
{
console.dir("Loading models from path: " + config.modelsPath);
}
let definitions = requireAll(
{
dirname: config.modelsPath,
filter: /(.+)\.(js)$/,
depth: 1,
caseSensitive: true
});
definitions = _.mapValues(definitions, (d) => d.call(loader));
_.each(definitions, function createModels(definition)
{
var modelId = definition.tableName || definition.globalId;
if (config.debug)
{
console.dir("Creating model id: " + modelId);
}
var model = loader.thinky.createModel(modelId, definition.schema, definition.options);
loader.models[modelId] = model;
});
// call the init funciton on each def to set up relationships
_.each(definitions, function initModel(definition)
{
var modelId = definition.tableName || definition.globalId;
if (config.debug)
{
console.dir("Initializing model id: " + modelId);
}
var model = loader.models[modelId];
definition.init(model);
});
return loader;
});
};
module.exports = loader;