Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

README and installer

  • Loading branch information...
commit ca7a622313a630210b7fea668ae928e15fa9ff25 1 parent bb8aa8a
@goulash1971 goulash1971 authored
View
37 README.md
@@ -41,7 +41,15 @@ instance:
// Access the mongoose-dbref module and install everything
var dbref = require("mongoose-dbref");
- var utils = dbref.install(mongoose);
+ var utils = dbref.utils
+
+ // Install the types, plugins and monkey patches
+ var loaded = dbref.install(mongoose);
+
+The `loaded` value returned contains 2 properties:
+
+- `loaded.types` : the join types that were loaded
+- `loaded.plugins` : the extension plugins that were loaded
To just install the types provided by the extension (either all types or a list of named types):
@@ -50,9 +58,15 @@ To just install the types provided by the extension (either all types or a list
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
- // Access the mongoose-dbref module and install types
+ // Access the mongoose-dbref module
var dbref = require("mongoose-dbref");
- var utils = dbref.loadTypes(mongoose);
+ var utils = dbref.utils
+
+ // Install the plugins
+ var loaded = dbref.loadTypes(mongoose);
+
+The `loaded` value returned contains the types that were loaded, keyed by the name of each type
+loaded.
To just install the plugins provided by the extension (either all plugins or list of named plugins):
@@ -61,9 +75,15 @@ To just install the plugins provided by the extension (either all plugins or lis
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
- // Access the mongoose-dbref module and install plugins
+ // Access the mongoose-dbref module
var dbref = require("mongoose-dbref");
- var utils = dbref.installPlugins(mongoose);
+ var utils = dbref.utils
+
+ // Install the plugins
+ var loaded = dbref.installPlugins(mongoose);
+
+The `loaded` value returned contains the plugins that were loaded, keyed by the name of each plugin
+loaded.
To just install the patches provided by the extension (either all patches or list of named patches):
@@ -72,9 +92,12 @@ To just install the patches provided by the extension (either all patches or lis
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
- // Access the mongoose-dbref module and install patches
+ // Access the mongoose-dbref module and the utilities
var dbref = require("mongoose-dbref");
- var utils = dbref.installPatches(mongoose);
+ var utils = dbref.utils;
+
+ // Install the monkey patches
+ dbref.installPatches(mongoose);
### Using the types
Once you have loaded the types, or installed the whole extension, you can begin to use them.
View
7 examples/basic.js
@@ -3,9 +3,12 @@ var mongoose = require("mongoose");
// Create a connection
var db = mongoose.createConnection();
-// Access the mongoose-dbref module and install everything
+// Access the mongoose-dbref module
var dbref = require("mongoose-dbref");
-var utils = dbref.install(mongoose);
+var utils = dbref.utils;
+
+// Install everything
+var loaded = dbref.install(mongoose);
// Create the schemas
var Schema = mongoose.Schema;
View
7 examples/hooks.js
@@ -3,9 +3,12 @@ var mongoose = require("mongoose");
// Create a connection
var db = mongoose.createConnection();
-// Access the mongoose-dbref module and install everything
+// Access the mongoose-dbref module
var dbref = require("mongoose-dbref");
-var utils = dbref.install(mongoose);
+var utils = dbref.utils
+
+// Instll everything
+var loaded = dbref.install(mongoose);
// Create the schemas
var Schema = mongoose.Schema;
View
40 lib/index.js
@@ -21,23 +21,30 @@ exports.utils = utils = require("./utils");
*
* @param {Mongoose} the active Mongoose instance for installation
* @param {Array} (optional) type filenames
- * @return the utilities of the extension (see './utils.js')
+ * @return {Object} the types that were loaded for the module
* @api public
*/
exports.loadTypes = loadTypes = function (mongoose) {
+ if (!('JoinTypes') in mongoose)
+ mongoose.JoinTypes = {};
var types = Array.prototype.slice.call(arguments, 1);
+ var loaded = {};
if (types.length) {
types.forEach(function (type) {
- require("./types/" + type).loadType(mongoose);
+ 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) {
var base = filename.slice(0, filename.length-3);
- require("./types/" + base).loadType(mongoose);
+ var val = require("./types/" + base).loadType(mongoose);
+ if (typeof val === 'function')
+ loaded[val.name] = val;
});
}
- return utils;
+ return loaded;
};
@@ -47,23 +54,28 @@ exports.loadTypes = loadTypes = function (mongoose) {
*
* @param {Mongoose} the active Mongoose instance for installation
* @param {Array} (optional) plugin filenames
- * @return the utilities of the extension (see './utils.js')
+ * @return {Object} the plugins that were loaded for this module
* @api public
*/
exports.installPlugins = installPlugins = function (mongoose) {
var plugins = Array.prototype.slice.call(arguments, 1);
+ var loaded = {};
if (plugins.length) {
types.forEach(function (plugin) {
- require("./plugins/" + plugin).install(mongoose);
+ 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) {
var base = filename.slice(0, filename.length-3);
- require("./plugins/" + base).install(mongoose);
+ var val = require("./plugins/" + base).install(mongoose);
+ if (typeof val === 'function')
+ loaded[val.name] = val;
});
}
- return utils;
+ return loaded;
}
/**
@@ -88,7 +100,6 @@ exports.installPatches = installPatches = function (mongoose) {
require("./patches/" + base).install(mongoose);
});
}
- return utils;
}
/**
@@ -97,12 +108,13 @@ exports.installPatches = installPatches = function (mongoose) {
* extension.
*
* @param {Mongoose} the active Mongoose instance for installation
- * @return the utilities of the extension (see './utils.js')
+ * @return {Object} the types, plugins and patches loaded
* @api public
*/
exports.install = function (mongoose) {
- loadTypes (mongoose);
- installPlugins(mongoose);
- installPatches(mongoose);
- return utils;
+ var loaded = {};
+ loaded.types = loadTypes (mongoose);
+ loaded.plugins = installPlugins(mongoose);
+ loaded.patches = installPatches(mongoose);
+ return loaded;
}
View
6 lib/plugins/dbrefHooks.js
@@ -27,7 +27,7 @@ var mongoose = require("mongoose");
* @param {Object} global options that apply to this plugin
* @api public
*/
-exports.dbrefHooks = dbrefHooks = function (schema, options) {
+exports.dbrefHooks = dbrefHooks = function dbrefHooks (schema, options) {
if (!('mongoose' in options))
throw new Error("'mongoose' option not defined");
if (!('DBRef' in options.mongoose.Types))
@@ -52,8 +52,10 @@ exports.dbrefHooks = dbrefHooks = function (schema, options) {
* Installer that installs a plugin into the mongoose infrastructure
*
* @param {Mongoose} the active Mongoose instance for installation
+ * @result {Object} the plugin that was installed
* @api public
*/
exports.install = function (mongoose) {
- return mongoose.plugin(dbrefHooks, {mongoose: mongoose});
+ mongoose.plugin(dbrefHooks, {mongoose: mongoose});
+ return dbrefHooks;
}
View
6 lib/plugins/resolveDBRefs.js
@@ -53,7 +53,7 @@ var namesForPath = function (path) {
* @param {Object} global options that apply to this plugin
* @api public
*/
-exports.resolveDBRefs = resolveDBRefs = function (schema, options) {
+exports.resolveDBRefs = resolveDBRefs = function resolveDBRefs (schema, options) {
if (!('mongoose' in options))
throw new Error("'mongoose' option not defined");
if (!('DBRef' in options.mongoose.Types))
@@ -116,8 +116,10 @@ exports.resolveDBRefs = resolveDBRefs = function (schema, options) {
* Installer that installs a plugin into the mongoose infrastructure
*
* @param {Mongoose} the active Mongoose instance for installation
+ * @result {Object} the plugin that was installed
* @api public
*/
exports.install = function (mongoose) {
- return mongoose.plugin(resolveDBRefs, {mongoose: mongoose});
+ mongoose.plugin(resolveDBRefs, {mongoose: mongoose});
+ return resolveDBRefs;
}
View
6 lib/types/dbref.js
@@ -4,7 +4,7 @@
* Copyright 2011, Stuart Hudson <goulash1971@yahoo.com>
* Released under the terms of the MIT License.
*
- * Version 0.0.1
+ * Version 0.0.2
*/
var mongoose = require("mongoose");
@@ -34,6 +34,7 @@ function handleArray (value) {
* Loader that loads the type into the mongoose infrastructure
*
* @param {Mongoose} the active Mongoose instance for installation
+ * @result {Object} the type that is loaded
* @api public
*/
exports.loadType = function (mongoose) {
@@ -90,4 +91,7 @@ exports.loadType = function (mongoose) {
// Perform the installation
mongoose.SchemaTypes.DBRef = DBRef;
mongoose.Types.DBRef = dbref;
+
+ // Return the type
+ return DBRef;
}
Please sign in to comment.
Something went wrong with that request. Please try again.