Mongodb Module and Service
Description
A MayaJS module and service that deals with Mongodb drivers. It uses mongoose
as its core dependency to communicate with the database.
Installation
npm i @mayajs/mongo
Quick Start
MayaJS uses custom modules to add functionality on its core. You can use MongoDbModule
and import in on any MayaJS module in your project.
import { MongoDbModule } from "@mayajs/mongo";
import { Schema } from "mongoose";
const User = new Schema({
name: String,
email: String,
password: String,
});
const mongodbOptions = {
connectionString: "your-mongodb-connection-string",
name: "your-collection-name",
options: {
},
schemas: [
User,
],
};
@Module({
imports: [MongoDbModule.forRoot(mongodbOptions)],
})
class CustomModule {}
Usage
To use mongodb inside your controller you will need to import MongoDbServices
. MongoDbServices provides set of functionality to access, manipulate and interact with mongodb.
Accessing a Collection
MongoDbServices provides a database
function to access a specific collection in mongodb.
import { MongoDbServices } from "@mayajs/mongo";
@Controller()
class UsersController {
constructor(private mongo: MongoDbServices) {}
sample() {
const db = this.mongo.database("your-collection-name");
}
}
Accessing a Model
To access a model you will need first the get the collection instance that we get from the above example. On the db instance you can access the model
object like seen below.
sample() {
const db = this.mongo.database("your-collection-name");
const model = db.instance.model("your-model-name");
}
This model instance is based on Mongoose Model. All of mongoose functionality is available for this model instance.
Collaborating
See collaborating guides here.
People
Author and maintainer Mac Ignacio
License
MIT