mongoose-idexists
Mongoose plugin that checks if referenced documents actually exist.
This plugin can be used to validate Mongoose Schema paths that are referencing other documents (usually by mongoose ObjectId of the _id field). It can be used as a full plugin that recursively add a validator to each ref path, or it can also be used to protect single schema paths.
Usage
Install
Simply run:
$ npm install mongoose-idexists --save
Then require it in your project
var idexists = ;
Basic Usage (default mongoose connection)
If you use idexists
without any further configuration, it uses the default mongoose connection.
You can add an idexists
validator for each Schema paths you want by using idexists.forPath(_the_mongose_path_object)
// require the idexists pluginvar idexists = ; // Let's define some schemas with ref fieldsvar personSchema = name: String; var storySchema = _creator: type: SchemaTypesObjectId ref: 'Person' fans: type: SchemaTypesObjectId ref: 'Person' ; // Let's add the validator only to the _creator pathidexists;
Array of references are supported. So you can use.
// Add the validator also to fans arrayidexists;
You can also recursively add a validator to all the Schema paths (that have references to other documents).
// Let's add the validator to _creator and fans at the same timeidexists; // As an alternative you can also use the mongoose plugin notationstorySchema // both previous notation produce the same effects ofidexists;idexists;
After the Schema definition as usual you can create the mongoose model. The previous code works for default mongoose connection, so somewhere after configuration you have to use
Story = mongoose;Person = mongoose;
Custom Connection
If you want to use a mongoose custom connection, you have to configure idexists
options.
The easies and comfortable way is to setup the connection as a global ìdexists
option, so it is used each time you use forSchema
or forPath
methods.
// Let's configure ìdexists in order to use a custom configuration// require the idexists pluginvar idexists = ; // during init phasevar connection = mongoose;idexists; // after initialization you can use forPath and forSchema, that now use the custom connectionidexists;
You can also specify a custom connection exclusively for some paths or schemas:
// during init phasevar connection = mongoose; // validator for _creator path uses the custom connectionidexists
Validation Messages
Default validation error messages use the following pattern:
{PATH} document not found in {MODEL} collection
where PATH
is substituted with the schema path that causes the validation error, while MODEL
is the referencing model, for example for the code seen before we have this validation error for PATH _creator
:
_creator document not found in Person collection
Custom Messages
To add a custom Message you can configure idexists
options globally:
idexists;
Note that you can use the {MODEL}
keyword in your string to indicate the referencing model (in addition to default keywords).
You can also specify a custom message exclusively for some paths or schemas:
idexists; idexists;
You can combine configuration for custom messages and connection both for global options and single path or schema options:
idexists; idexists; idexists; storySchema;
How it works
The plugin is very simple, it perform a simple count query (by using TargetModel.count
) filtered by the referencing id. The count returning value is checked accordingly to the referencing field type (single object or array).
NB the filter query uses the _id
field (it works as population).
Test
Tests require a local mongodb database running with default configuration.
$ npm test
License
MIT © Andrea Tarquini