npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rexfng/db

v9.0.0

Published

Mongo Db wrapper with CRUD functionality, listing collections and exposing raw mongoose modal

Downloads

6

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)