Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

dbdjs.mongo

xzusfin9MIT1.0.1TypeScript support: included

MongoDB wrapper using mongoose with dbdjs.db-like API

mongodb, dbdjs, dbd.js, dbd, dbdjs.db, dbd.db, mongo, mongoose, database

readme

DBDJS.Mongo

NPM Version NPM Downloads

Table Of Contents

About

A MongoDB wrapper using mongoose with dbdjs.db like API

Examples

Default Instance

const mongoose = require("mongoose")
const dbdmongo = require("dbdjs.mongo").default

mongoose.connect("mongodb://localhost:27017/database", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    keepAlive: true
})

dbdmongo.createModel("main")

dbdmongo.set("main", "my_name", "Matthew")
dbdmongo.set("main", "my_house", "USA")

//etc

Custom Instance

const mongoose = require("mongoose")
const dbdmongo = require("dbdjs.mongo")

;(async () => {
    const instance = dbdmongo.create(await mongoose.createConnection("mongodb://localhost:27017/database", {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        keepAlive: true
    }))

    instance.createModel("main")

    dbdmongo.set("main", "my_name", "Matthew")
    dbdmongo.set("main", "my_house", "USA")

    //etc
})

API

Module

  • default
    • Default dbdjs.mongo instance
    • Type: Instance
      const dbdmongo = require("dbdjs.mongo").default
  • create(mongoInstance)
    • Create a dbdjs.mongo instance
    • Parameters
    • Return: Instance
      const dbdmongo = require("dbdjs.mongo").create(await mongoose.createConnection(
      "mongodb://localhost:27017/database", {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      keepAlive: true
      }))

Instance

  • setInstance(mongoInstance)
    • Set a mongo instance to the dbdjs.mongo instance
    • Paramaters
    • Return: void
      dbdmongo.setInstance(await mongoose.createConnection(
      "mongodb://localhost:27017/database", {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      keepAlive: true
      }))
  • createModel(name)
    • Create a model to the dbdjs.mongo instance
    • Parameters
      • name: string
    • Return: void
      dbdmongo.createModel("main")
  • getModel(name)
    • Get a model from the dbdjs.mongo instance
    • Parameters
      • name: string
    • Return: Model
      dbdmongo.getModel("main")
  • deleteModel(name)
    • Delete a model from the dbdjs.mongo instance
    • Parameters
      • name: string
    • Return: void
      dbdmongo.deleteModel("main")
  • set(model, key, value)
    • Set a data to the model
    • Parameters
      • model: string
      • key: string
      • value: any
    • Return: boolean
      dbdmongo.set("main", "my_name", "Matthew")
  • get(model, key)
    • Get a data from the model
    • Parameters
      • model: string
      • key: string
    • Return: { key: string, value: any }
      dbdmongo.get("main", "my_name")
  • all(model, options)
    • Get all data from the model and filter it
    • Parameters
      • model: string
      • options: object
        • filter: (document: { key: string, data: any }) => boolean
    • Return: Array<{ key: string, data: any }>
      dbdmongo.all("main", { filter: d => d.data === "Matthew" })
  • delete(model, key)
    • Delete a data from the model
    • Parameters
      • model: string
      • key: string
    • Return: boolean
      dbdmongo.delete("main", "my_name")