Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mayajs/mongo

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mayajs/mongo

MayaJS Mongo decorators and modules

  • 0.7.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-92.86%
Maintainers
1
Weekly downloads
 
Created
Source

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";

// 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

License

MIT

Keywords

FAQs

Package last updated on 25 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc