Mongoose-Joins is an extension for Mongoose that provides basic join support for the Mongoose ORM that can be extended with different join styles and provides some utilities, plugins and patches that allow joins to be manipulated / followed.
- join : a mapping between two parties - a source party and a target party
- party : a model that has been installed in a Mongoose instance
- source party : the party on which the join is installed
- target party : the party which is the type of model(s) returned when following the join
- join binding : a lightweight object that represents the join for a given source party instance
The extension provides the following join types:
DBRefJoin
: a join where one party holds aDBRef
to another partyFkJoin
: a join where one party holds a foreign key of another partyMappedJoin
: a join where one party holds field values that can be mapped to those in another partyQueryJoin
: a join where the mapping between parties is defined by a query generated from afunction
The extension provides the following monkey-patches:
schema.join
: used to define a join on aSchema
instance (representing the source party)
npm install mongoose-joins
To install all of the types, plugins, patches and utilities provided by the extension into a Mongoose instance:
var mongoose = require("mongoose");
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
// Access the mongoose-joins module and install everything
var joins = require("mongoose-joins");
var utils = joins.utils
// Install the types, plugins and monkey patches
var loaded = joins.install(mongoose);
The loaded
value returned contains 2 properties:
loaded.types
: the join types that were loadedloaded.plugins
: the extension plugins that were loaded
If you want to control what is installed, you can either install types/plugins/patches separately (see below)
or pass in a second argument to the install
function.
If this second argument is a Function
then it will be used as a filter when installing the types, plugins and
patches. If it is an Object
then the types
property (either a filter Function
or list of type names) is used
when loading the types, the plugins
property (either a filter Function
or list of plugin names) is used when
installing the plugins and the patches
property (either a filter Function
or list of patch names) is used when
installing the patches.
To just install the types provided by the extension:
var mongoose = require("mongoose");
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
// Access the mongoose-joins module
var joins = require("mongoose-joins");
var utils = joins.utils
// Install the plugins
var loaded = joins.loadTypes(mongoose);
The loaded
value returned contains the types that were loaded, keyed by the name of each type
loaded.
If you just want to load a specific list of types, or want to filter the types loaded then use one
of the following signatures with the loadTypes()
function:
loadTypes(mongoose, 'dbrefJoin')
: just loads thedbrefJoin
typeloadTypes(mongoose, function(type) { return type.slice(1,2) === 'db'; })
: loads types starting withdb
To just install the plugins provided by the extension:
var mongoose = require("mongoose");
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
// Access the mongoose-joins module
var joins = require("mongoose-joins");
var utils = joins.utils
// Install the plugins
var loaded = joins.installPlugins(mongoose);
The loaded
value returned contains the plugins that were loaded, keyed by the name of each plugin
loaded.
If you just want to install a specific list of plugins, or want to filter the plugins loaded then use one
of the following signatures with the installPlugins()
function:
installPlugins(mongoose, 'validateJoins')
: just install thevalidateJoins
plugininstallPlugins(mongoose, function(plugin) { return plugin.slice(1,2) === 'db'; })
: installs plugins starting withdb
To just install the patches provided by the extension (all patches, named named patches or filtered patches):
var mongoose = require("mongoose");
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
// Access the mongoose-joins module and the utilities
var joins = require("mongoose-joins");
var utils = joins.utils;
// Install the monkey patches
joins.installPatches(mongoose);
If you just want to install a specific list of patches, or want to filter the patches loaded then use one
of the following signatures with the installPatches()
function:
installPatches(mongoose, 'schema')
: just install theschema
patchinstallPatches(mongoose, function(patch) { return patch.slice(1,2) === 'db'; })
: installs patch starting withdb
Each join is defined as an instance of a concrete JoinType
subclass, where the concrete subclass provides behaviour
for following the join and (optionall) manipulating the parties at either end of the relationship. All joins have
the following properties.
path
: the virtual path that the join is installed on within the source partyoptions
: a map of option values set when the join was createdresultSet
: aBoolean
which indicates whether the result of the join is 1 (false
) or many (true
)nullOk
: aBoolean
which indicates whether he result of the join can benull
or emptytargetSchema
: the name of theSchema
representing the target partymappedBy
: the mapping definition (specific to the concreteJoinType
subclass)
When a join is installed on a Schema
(this is what the schema.join
monkey patch does) then a virtual
is created
on the Schema
under the associated path
that will yeild a join binding for an instance of the model created
from the Schema
.
All join bindings supply the following functions:
follow
- follows the join for the model instance and invokes thecallback
supplied with(err, result)
The behavour that is executed for these functions is dependent upon the concrete JoinType
subclass and the options
(including target
and mapping
) supplied when creating the join for the Schema
.
The DBRefJoin
join type can be used where the relationship between the two parties is controlled through a DBRef
on
one of the parties.
The FkJoin
join type can be used where the relationship between the two parties is controlled through a foreign key on
one of the parties.
The MappedJoin
join type can be used where the relationship between the two parties is controlled a field mapping for
the two parties.
The QueryJoin
join type can be used where the relationship between the two parties is controlled through a query that
can be executed against the target party using properties of the source party instance.
Once you have installed the patches, or installed the whole extension, you can begin to use them.
This join
monkey patch to the Schema
class can be used to define and install a join on the Schema
representing the
source party of the join.
MIT License
- Brian Noguchi for the 'mongoose-types' extension that was used as a template for this extension
Stuart Hudson