MongoDB is already included in the bootstrap and can be used to communicate to a Mongo database.
By default MongoDB is disabled unless you add the connection details to your application config like this:
{
"mongo": {
"host": "localhost",
"port": 27017,
"username": "root",
"password": "root",
"database": "database_name"
}
}
The creation of models is automated in the bootstrap.
Add your collection file to the app/collections
folder in your project.
An example would be (Robot.json):
{
"id": {
"type": "Number",
"description": "Unique identifier for the robot"
},
"name": {
"type": "String",
"description": "Name of the robot"
},
"active": {
"type": "Boolean",
"description": "Is the robot active"
}
}
Additional Mongoose Schema Documentation: https://mongoosejs.com/docs/guide.html#definition
Models are available for manual usage:
const NextBootstrap = require('@dpdk/bootstrap');
const bootstrap = new NextBootstrap(__dirname, process.cwd(), nextConfig);
bootstrap.init();
const models = bootstrap.mongo.models;
models.Robot.find().then((data) => {
console.log('data', data);
});
Simple indexes can be set in config like this (this will be used by GraphQL for sorts/filters):
{
"mongo": {
"index": {
"page": [
{
"nid": 1
},
{
"title": 1
}
]
}
}
}
The mongo config can be extended to allow replica sets to work on a loadbalanced mongo setup:
{
"mongo": {
"replica": {
"name": "replicaName",
"hosts": [
{
"host": "mongo2.local",
"port": 27017
},
{
"host": "mongo3.local",
"port": 27017
}
]
}
}
}
Additional Mongoose Documentation: https://mongoosejs.com/docs/api.html
Additional MongoDB Documentation: https://docs.mongodb.com/manual/
Last modified | Friday, April 30, 2021, 12:00:21 PM UTC |
Last author | Colin van Eenige |
Commit ID | 4c7a701 |