forked from Automattic/mongoose
/
index.js
144 lines (117 loc) · 4.98 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
var fs = require('fs'),
path = require('path'),
sys = require('sys'),
object = require('./lib/utils/object'),
EventEmitter = require('events').EventEmitter,
Script = process.binding('evals').Script,
Storage = require('./lib/storage').Storage,
Model = require('./lib/model/').Model,
Schema = require('./lib/schema').Schema,
Plugin = require('./lib/model/plugin').Plugin,
Mongoose = (function(){
var API = function(){
EventEmitter.call(this);
this.Schema = new Schema(this);
this.Model = new Model(this);
return this;
};
API.prototype = {
loaded : false,
options : {
strict : true,
sandboxName : 'Mongoose',
loadAddons : false,
activeStoreEnabled : true,
activeStore : false,
connections : {},
},
connections : {},
defineTypes : {},
Instance : API,
Storage : Storage,
configure : function(options){
object.merge(this.options,options);
object.merge(this.connections,this.options.connections);
if(this.options.loadAddons){
this.load('./schema/types');
this.load('./schema/validators');
this.load('./model/plugins');
}
this.loaded = true;
return this;
},
load : function(resourcePath){
var stats = fs.statSync(resourcePath),
files = [];
if(stats.isFile()) files = [resourcePath];
if(stats.isDirectory())
files = fs.readdirSync(resourcePath).map(function(file){
return path.join(resourcePath,file);
});
files.forEach(function(file){
if(fs.statSync(file).isFile())
var sandbox = {},
code = fs.readFileSync(file) + "\n\n__func_proto__ = Function.prototype;";
sandbox[this.options.sandboxName] = this;
sandbox['Model'] = this.Model;
sandbox['require'] = require;
sandbox['__filename'] = file;
sandbox['__dirname'] = path.dirname(file);
Script.runInNewContext(code ,sandbox );
for(i in Function.prototype) if(!sandbox.__func_proto__[i]) sandbox.__func_proto__[i] = Function.prototype[i];
}.bind(this));
},
connect : function(uri,options){
var uri = uri;
if(!this.loaded) this.configure();
if(!uri){
if(this.options.activeStoreEnabled){
if(this.options.activeStore) uri = this.options.activeStore;
}
else {
this.emit('error','activeStore not enabled or specify. Use an URI with connect');
return false;
}
}
if(!this.connections[uri]) this.connections[uri] = uri;
if(typeof this.connections[uri] == 'string') this.connections[uri] = this.parseURI(this.connections[uri]);
if(this.connections[uri] instanceof Array) this.connections[uri] = new Storage(this,this.connections[uri],options);
if(this.options.activeStoreEnabled) this.options.activeStore = uri;
return this.connections[uri];
},
noSchema : function(collection,store){
if(!(store instanceof Storage)){
store = this.connect(store);
if(!(store instanceof Storage)) return false;
}
return this.Model.get(collection,store,true);
},
define : function(type,obj){
var definer = this.defineTypes[type];
if(definer && (typeof definer.define == 'function')) definer.define(obj);
else this.emit('error','Define type ['+type+'] is invalid');
},
get : function(model,store,noSchema){
return this.Model.get(model,store,noSchema)
},
close : function(){
for(conn in this.connections)
if(this.connections[conn] instanceof Storage) this.connections[conn].db.close();
},
parseURI : function(uri){
return uri.split(',').map(function(conn,idx){
var a = conn.match(/^(?:(.+):\/\/(?:(.+?):(.+?)@)?)?(?:(.+?)(?::([0-9]+?))?(?:\/(.+?))?)$/);
return { 'type' : a[1], 'user' : a[2], 'password' : a[3], 'host' : a[4], 'port' : a[5], 'db' : a[6] };
});
},
addDefiner : function(name,callback){
if(this.defineTypes[name] && (typeof this.defineTypes[name] == 'function'))
this.emit('error','Definer ['+name+'] already exists');
else this.defineTypes[name] = callback;
},
merge : object.merge
};
for(i in EventEmitter.prototype) API.prototype[i] = EventEmitter.prototype[i];
return new API();
})();
this.Mongoose = Mongoose;