-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (145 loc) · 4.95 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Generated by CoffeeScript 1.10.0
(function() {
var CWD, Waterline, _, def, loadedModels, memoryAdapter, orm, waterlineLoader;
_ = require('lodash');
Waterline = require('waterline');
def = require('def-type');
memoryAdapter = require('sails-memory');
orm = new Waterline();
loadedModels = null;
CWD = process.cwd();
waterlineLoader = def.Module(function() {
var _destroyModel, _getOriginalName, _loadModelIntoCollection, _loadModels, config, defaultModel, namesHashMap;
config = {
adapters: {
'default': memoryAdapter,
'memory': memoryAdapter
},
connections: {
memory: {
adapter: 'memory'
}
},
defaults: {
migrate: 'drop'
},
lookUpPath: CWD + "/api/models",
defaultModelConf: {
connection: 'memory'
},
useLog: true
};
namesHashMap = {};
defaultModel = null;
this.init = function(options, callback) {
var attachModelsTo, models;
if (options == null) {
options = {};
}
models = options.models || options.collections;
delete options.models;
delete options.collections;
config = _.defaults(options, config);
defaultModel = config.defaultModelConf;
attachModelsTo = config.attachModelsTo != null ? _.isArray(config.attachModelsTo) ? config.attachModelsTo : [config.attachModelsTo] : [global];
delete config.defaultModelConf;
_loadModels(models);
return orm.initialize(config, function(err, orm) {
var i, len, lowerCaseName, model, obj, ref;
if (config.useLog) {
console.log('WaterlineLoader: Initializing ORM');
}
if (err) {
return callback(err);
}
loadedModels = orm.collections;
ref = orm.collections;
for (lowerCaseName in ref) {
model = ref[lowerCaseName];
model.associations = _.reduce(model.attributes, function(associatedWith, attrDef, attrName) {
var assoc, ref1;
if (typeof attrDef === 'object' && (attrDef.model || attrDef.collection)) {
assoc = {
alias: attrName,
type: (ref1 = attrDef.model) != null ? ref1 : {
'model': 'collection'
}
};
if (attrDef.model) {
assoc.model = attrDef.model;
}
if (attrDef.collection) {
assoc.collection = attrDef.collection;
}
if (attrDef.via) {
assoc.via = attrDef.via;
}
associatedWith.push(assoc);
}
return associatedWith;
}, []);
if (lowerCaseName.indexOf('__') === -1) {
for (i = 0, len = attachModelsTo.length; i < len; i++) {
obj = attachModelsTo[i];
obj[_getOriginalName(lowerCaseName)] = model;
}
}
}
return callback(null, orm.collections);
});
};
this.teardown = function(callback) {
if (config.useLog) {
console.log('WaterlineLoader: tearing down...');
}
return _destroyModel(loadedModels, _.keys(loadedModels), callback);
};
_destroyModel = function(models, modelNamesArray, callback) {
return models[modelNamesArray.pop()].destroy().then((function(_this) {
return function() {
if (modelNamesArray.length === 0) {
return orm.teardown(callback);
} else {
return _destroyModel(models, modelNamesArray, callback);
}
};
})(this));
};
_loadModels = function(models) {
var i, len, model, modelDefinition, modelFileName, modelName;
for (i = 0, len = models.length; i < len; i++) {
model = models[i];
modelFileName = _.isString(model) ? model : model.fileName || model.path;
modelName = model.alias || modelFileName;
modelDefinition = require(config.lookUpPath + "/" + modelFileName);
if (_.isFunction(modelDefinition)) {
if (model.afterLoadFilter != null) {
modelDefinition = model.afterLoadFilter(modelDefinition);
} else {
modelDefinition = modelDefinition();
}
}
_loadModelIntoCollection({
name: modelName,
definition: modelDefinition
});
}
return this;
};
_loadModelIntoCollection = function(model) {
var waterlineCollection, waterlineModel;
waterlineModel = _.defaults(model.definition, {
identity: model.name,
tableName: model.name
}, defaultModel);
waterlineCollection = Waterline.Collection.extend(waterlineModel);
orm.loadCollection(waterlineCollection);
return namesHashMap[model.name.toLowerCase()] = model.name;
};
return _getOriginalName = function(lowerCaseName) {
return namesHashMap[lowerCaseName];
};
});
module.exports = waterlineLoader;
}).call(this);
//# sourceMappingURL=index.js.map