Mongoose Plugin for kvell
Kvell-db-plugin-mongoose
wraps up mongoose and exposes all three values required to be exposed by a kvell database plugin.
Install
npm i kvell-db-plugin-mongoose
Configuration Variables
Internally, kvell-db-plugin-mongoose
will instantiate the database object using mongoose.connect
method.
Usage
Example:
- kvell-plugins.js:
module.exports = {
databasePlugins: [
{
resolve: "kvell-db-plugin-mongoose",
options: {
mongoConnectionString: "mongodb://localhost:27017/test",
options: {},
showConnectionMessage: false
}
}
]
};
- userModel.js:
const mongoose = require("kvell-db-plugin-mongoose").dbLib;
// Create your User model's schema here and export it.
const userSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
age: Number
});
const User = mongoose.model("User", userSchema);
module.exports = User;
To use it, install the package and add a databasePlugin
object in kvell-plugins.js
with the following fields:
resolve
: Name of the plugin, i.e, kvell-db-plugin-mongooseoptions
: All the parameters that you need to pass in the mongoose constructor. The following key is mandatory:mongoConnectionString
(string): The connection string for the mongoose server.
You can add more keys which conform to the
mongoose.connect
api'soptions
object. You can read more on that here.
showConnectionMessage
(boolean): If set totrue
, a success message will be logged on the console once the connection is successfully established.
By default, the following options are taken as true while instantiating the mongoose instance:
useNewUrlParser
useUnifiedTopology
You may choose to override it by simply adding these keys as fields in the options
field of the object.
The plugin exports the following:
dbLib: The
mongoose
object. Check mongoose docs for complete api reference.dbInstance: The instantiated
mongoose
instance.initHandler: (not for use)