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

mongoose-utils-helper

v1.0.12

Published

Welcome Users, **Don't repeat yourself, use reusable code :)** Always happy to help you,

Downloads

18

Readme

mongoose-utils-helper

Documentation

Welcome Users, Don't repeat yourself, use reusable code :) Always happy to help you,

Happy Coding ♥

Installation


$ npm install mongoose-utils-helper

Getting Started

Importing

// Using Node.js `require()`

const {
    connection, //connection function
    createModel, //function for creating mongodb model
    common_helper, //common_helper for CRUD operation
    commonFuns, // common functions
    mongoose  // mongoose package object
} = require('mongoose-utils-helper');

// Using ES6 imports
import { connection, createModel, common_helper, commonFuns, mongoose } from  'mongoose-utils-helper';

Connecting to MongoDB

First, we need to define a connection. If your app uses only one database, you should use connection() function.

connection() takes two arguments First mongodb:// URI, or the object with following properties username password, hostname, port and second agrument is to disable default console when connection is connected(default value is true).

Example:


// Using Node.js `require()`
const { connection } = require('mongoose-utils-helper');
// Using ES6 imports
import { connection } from  'mongoose-utils-helper';

let  dbUri = "mongodb://localhost/test";

const  db = connection(dbUri);

connection() function return the database connection Object.

Defining a Model

Models are defined through the createModel() interface.

Example:


import { createModel } from  'mongoose-utils-helper';
**Important!** `createModel(schema,collectionName)`  interface  required  two  arguments,

var MyModel = createModel({
    body : { type: String, default: "" },
    date : { type: Date, default: Date.now },
}, "my_model");

Once we have our model, we can then instantiate it, and save it:


MyModel.find({}, function (err, docs) {
// docs.forEach
});
  

You can also findOne, findById, update, etc. For more details check out the docs.

Using common_helper

Basic insert, insertMany, update, softDelete, delete, find, findOne and count operations using common_helper.xxx(Model,condition) Object.

Example:


// Using Node.js `require()`

const { common_helper } = require('mongoose-utils-helper');
// Using ES6 imports
import { common_helper } from  'mongoose-utils-helper';

let  condition = { name :  "Amitu" };
let  data = await  common_helper.find(MyModel, condition);

//output response:
{
    status : 1, //status code (1 for success 0 for fail)
    message : "Data found", // Response message
    data :<operation  output> }; // can be object, array or string
}

Using commonFuns

Example:

// Using Node.js `require()`
const { commonFuns } = require('mongoose-utils-helper');

// Using ES6 imports
import { commonFuns } from  'mongoose-utils-helper';

let  mongodbId = "5ac343820e617c07555c47f0"

let  convertedMongoObjId = await  commonFuns.convertMongoId(mongodbId);
let  convertedMongoObjId = await  commonFuns.mongooseCheckValid(mongodbId);

Authors

Amit Chauhan

License

Copyright (c) 2019 DGSM <[email protected]>