@mayajs/mongo
v0.7.2
Published
MayaJS Mongo decorators and modules
Downloads
4
Maintainers
Readme
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";
// Define schema for mongoose
const User = new Schema({
name: String,
email: String,
password: String,
});
const mongodbOptions = {
connectionString: "your-mongodb-connection-string",
name: "your-collection-name",
options: {
// Any mongoose options can be used here
// i.e. useUnifiedTopology: true, useNewUrlParser: true, useFindAndModify: false
},
schemas: [
{
name: "User", // Name of model
schema: User, // Mongoose Schema
},
,
],
};
@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 {
// Inject MongoDbServices in a controller
constructor(private mongo: MongoDbServices) {}
sample() {
// Get specific collection in mongodb
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() {
// Get specific collection in mongodb
const db = this.mongo.database("your-collection-name");
// Get specific model instance
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