forked from goulash1971/mongoose-dbref
/
index.js
165 lines (155 loc) · 5 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
157
158
159
160
161
162
163
164
165
/**
* lib/index.js - extension loader
*
* Copyright 2011, Stuart Hudson <goulash1971@yahoo.com>
* Released under the terms of the MIT License.
*
* Version 0.0.2
*/
/**
* Expose the utilities that are available from this module (these
* are also accessed from the loadTypes function.
*/
exports.utils = utils = require("./utils");
/**
* Utility function that will convert an {Array} of arguments into a
* loading specification consiting of a filter {Function} and an
* {Array} of names.
*
* @param {Array} arguments to be converted to a loading spec
* @return {Object} loading spec of filter and names
* @api private
*/
var figureLoadSpec = function (args) {
var spec = {filter: function (name) { return true; }, names: []};
if (args.length == 1) {
if (typeof args[0] === 'function')
spec.filter = args[0];
else spec.names = args;
} else spec.names = args;
return spec;
}
/**
* Loads either the named types or all available types in the
* {@code lib/types} source directory
*
* @param {Mongoose} the active Mongoose instance for installation
* @param {Array} (optional) type filenames
* @return {Object} the types that were loaded for the module
* @api public
*/
exports.loadTypes = loadTypes = function (mongoose, types) {
types = figureLoadSpec(types ? types : []);
var loaded = {};
if (types.names.length) {
types.names.forEach(function (type) {
if (types.filter(type)) {
var val = require("./types/" + type).loadType(mongoose);
if (typeof val === 'function')
loaded[val.name] = val;
}
});
} else {
var files = require("fs").readdirSync(__dirname + "/types");
files.forEach(function(filename) {
if (filename.slice(filename.length-3) === '.js') {
var base = filename.slice(0, filename.length-3);
if (types.filter(base)) {
var val = require("./types/" + base).loadType(mongoose);
if (typeof val === 'function')
loaded[val.name] = val;
}
}
});
}
return loaded;
};
/**
* Installs either the named plugins or all available plugins in the
* {@code lib/plugins} source directory
*
* @param {Mongoose} the active Mongoose instance for installation
* @param {Array} (optional) plugin filenames
* @return {Object} the plugins that were loaded for this module
* @api public
*/
exports.installPlugins = installPlugins = function (mongoose, plugins) {
plugins = figureLoadSpec(plugins ? plugins : []);
var loaded = {};
if (plugins.names.length) {
plugins.names.forEach(function (plugin) {
if (plugins.filter(plugin)) {
var val = require("./plugins/" + plugin).install(mongoose);
if (typeof val === 'function')
loaded[val.name] = val;
}
});
} else {
var files = require("fs").readdirSync(__dirname + "/plugins");
files.forEach(function(filename) {
if (filename.slice(filename.length-3) === '.js') {
var base = filename.slice(0, filename.length-3);
if (plugins.filter(base)) {
var val = require("./plugins/" + base).install(mongoose);
if (typeof val === 'function')
loaded[val.name] = val;
}
}
});
}
return loaded;
}
/**
* Installs either the named patches or all available patches in the
* {@code lib/patches} source directory
*
* @param {Mongoose} the active Mongoose instance for installation
* @param {Array} (optional) patch filenames or filter function
* @return the utilities of the extension (see './utils.js')
* @api public
*/
exports.installPatches = installPatches = function (mongoose, patches) {
patches = figureLoadSpec(patches ? patches : []);
if (patches.names.length) {
patches.names.forEach(function (patch) {
if (patches.filter(patch))
require("./patches/" + patch).install(mongoose);
});
} else {
var files = require("fs").readdirSync(__dirname + "/patches");
files.forEach(function(filename) {
if (filename.slice(filename.length-3) === '.js') {
var base = filename.slice(0, filename.length-3);
if (patches.filter(base))
require("./patches/" + base).install(mongoose);
}
});
}
}
/**
* Installation function that will load all of the types and install
* both the plugins and the patches that are defined for this mongoose
* extension.
*
* The {@param options} can either be a {Function} which is passed to
* the types, plugins and patches installers for filtering, or an
* {Object} which identifies which types ({@param options#types}),
* plugins ({@param options#plugins}) or patches ({@param options#patches})
* are to be installed.
*
* @param {Mongoose} the active Mongoose instance for installation
* @param {Object} identifying what is to be installed
* @return {Object} the types, plugins and patches loaded
* @api public
*/
exports.install = function (mongoose, spec) {
var loaded = {};
if (typeof spec == 'function')
spec = {types: spec, plugins: spec, patches: spec};
else if (!(typeof spec == 'object'))
spec = {types: [], plugins: [], patches: []};
loaded.types = loadTypes (mongoose, spec.types);
loaded.plugins = installPlugins(mongoose, spec.plugins);
loaded.patches = installPatches(mongoose, spec.patches);
return loaded;
}