2.0.2 • Published 3 years ago
abyjs.mongo v2.0.2
abyjs.Mongo
Table Of Contents
About
A MongoDB wrapper using mongoose with abyjs.db like API
Examples
Default Instance
const mongoose = require("mongoose")
const abymongo = require("abyjs.mongo").default
mongoose.connect("mongodb://localhost:27017/database", {
useNewUrlParser: true,
useUnifiedTopology: true,
keepAlive: true
})
abymongo.createModel("main")
abymongo.set("main", "my_name", "Matthew")
abymongo.set("main", "my_house", "USA")
//etc
Custom Instance
const mongoose = require("mongoose")
const abymongo = require("abyjs.mongo")
;(async () => {
const instance = abymongo.create(await mongoose.createConnection("mongodb://localhost:27017/database", {
useNewUrlParser: true,
useUnifiedTopology: true,
keepAlive: true
}))
instance.createModel("main")
abymongo.set("main", "my_name", "Matthew")
abymongo.set("main", "my_house", "USA")
//etc
})
API
Module
- default
- Default abyjs.mongo instance
- Type: Instance
const abymongo = require("abyjs.mongo").default
- create(mongoInstance)
- Create a abyjs.mongo instance
- Parameters
- mongoInstance: Mongoose | Connection
- Return: Instance
const abymongo = require("abyjs.mongo").create(await mongoose.createConnection(
"mongodb://localhost:27017/database", {
useNewUrlParser: true,
useUnifiedTopology: true,
keepAlive: true
}))
Instance
- setInstance(mongoInstance)
- Set a mongo instance to the abyjs.mongo instance
- Paramaters
- mongoInstance: Mongoose | Connection
- Return: void
abymongo.setInstance(await mongoose.createConnection(
"mongodb://localhost:27017/database", {
useNewUrlParser: true,
useUnifiedTopology: true,
keepAlive: true
}))
- createModel(name)
- Create a model to the abyjs.mongo instance
- Parameters
- name: string
- Return: void
abymongo.createModel("main")
- getModel(name)
- Get a model from the abyjs.mongo instance
- Parameters
- name: string
- Return: Model
abymongo.getModel("main")
- deleteModel(name)
- Delete a model from the abyjs.mongo instance
- Parameters
- name: string
- Return: void
abymongo.deleteModel("main")
- set(model, key, value)
- Set a data to the model
- Parameters
- model: string
- key: string
- value: any
- Return: boolean
abymongo.set("main", "my_name", "Matthew")
- get(model, key)
- Get a data from the model
- Parameters
- model: string
- key: string
- Return: { key: string, value: any }
abymongo.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 }>
abymongo.all("main", { filter: d => d.data === "Matthew" })
- delete(model, key)
- Delete a data from the model
- Parameters
- model: string
- key: string
- Return: boolean
abymongo.delete("main", "my_name")