@rexfng/db
v9.0.0
Published
Mongo Db wrapper with CRUD functionality, listing collections and exposing raw mongoose modal
Downloads
19
Readme
Description
Mongo Db wrapper with CRUD functionality, listing collections and exposing raw mongoose modal
Initialize
Uses environment variable or directly passing mongodb url to constructor. Recommend using dotenv to manage enviornment variable MONGODB_DATABASE_URL
DB Helper Methods
//returns mongoose helpers which can be used for creating custom routes. Method returns a promise.
const DB = require('@rexfng/db')
let MongoDB = new DB()
Find
The find method has been deprecated in favir of using the much more powerful mongoose api's model method.
MongoDB.model.find({"type": "users"})
.then(function(response){
console.log(response)
})
.catch(function(error){
console.log(error)
})
Create
MongoDB.create('users', {
name: "John",
age: 20,
talents: [
{
activity: "piano",
experiences_in_years: 2
},
{
activity: "basketball",
experiences_in_years: 5
}
]
})
.then(function(response){
console.log(response)
})
.catch(function(error){
console.log(error)
})
Update
Passing in BSON ID and object wish to update based on corresponding key in an existing object.
MongoDB.update('5b7450d4607eb60004cd4eab', {
talents: [
{
activity: "piano",
experiences_in_years: 2
},
{
activity: "basketball",
experiences_in_years: 5
},
{
activity: "chess",
experiences_in_years: 3
}
]
})
.then(function(response){
console.log(response)
})
.catch(function(error){
console.log(error)
})
Delete
Passing in BSON ID as string or array of strings
MongoDB.delete('5b7450d4607eb60004cd4eab')
.then(function(response){
console.log(response)
})
.catch(function(error){
console.log(error)
})
Accessing mongoose lower API
Returns mongoose Modal, can then access mongoose npm api
MongoDB.model.find().select('data.user').lean().exec() //promise
Transactions
Perform Transaction with rollback feature for fail operation. This method uses mongoose-transactions
library
MongoDB.transactions([
{
"method":"CREATE",
"obj": {
"type": "test", // for CREATE method please provide type
"data": { // for CREATE method please provide payload
"quantity": 13000
}
}
},
{
"method":"UPDATE",
"obj": {
"_id": "5e75532fb1db9b638ee85f6e" // for UPDATE method please provide _id
"type": "test",
"data": {
"quantity": -500
}
}
},
{
"method":"DELETE",
"obj": {
"_id": "5e75532fb1db9b638ee85f6e" // for DELETE method please provide _id
}
}
])
Format Response
This helper flattens the response structure by moving data
up one level for ease of populating and combining multiple Models together.
transform
dbQuery.format([response]) or dbQuery.format(response)
{
_id: "5c3a68669e832f00dcbe6a69",
type: 'nba',
data: { firstname: 'Klay', lastname: 'Thompson', award: [Object] },
updatedAt: 2019-01-14T22:33:22.221Z,
createdAt: 2019-01-14T22:33:22.221Z
}
into
{
_id: "5c3a68669e832f00dcbe6a69",
type: 'nba',
firstname: 'Klay',
lastname: 'Thompson',
award: [Object] ,
updatedAt: 2019-01-14T22:33:22.221Z,
createdAt: 2019-01-14T22:33:22.221Z
}
Router Methods
Returns routes which can be used with express. For the gets method, it also accepts query filtering via library api-query-params
module. Please check their library for further documentation.
const express = require('express')
app = express();
app.listen(3000);
app.use('/', DB.routes.get)
app.use('/', DB.routes.gets)
app.use('/', DB.routes.post)
app.use('/', DB.routes.put)
app.use('/', DB.routes.del)