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

simple-mongodb-helper2

v1.0.2

Published

This helper expose native entries from mongodb driver and add some elements to simplify some actions. A lot of old commands are purged from v1 helper.

Downloads

7

Readme

simple-mongo-helper-2

Synopsis

This helper expose native entries from mongodb driver and add some elements to simplify some actions. A lot of old commands are purged from v1 helper.

Installation

npm i simple-mongodb-helper2

Usage

init in your code

with import

import { helper as db } from './dist/index.js'

with require

const myMongoHelperClass = require('simple-mongodb-helper-2'); const db = myMongoHelperClass.helper;

connect

db.connect(param)

param = {
    "uri": "mongodb://ip:port/dbname", <-- MongoDb uri with options
    "dbName": "dbname",
    "options": { <-- MongoDB drivers options
        "auth": {
            "password": "xxxx",
            "username": "yyyy",
            "authSource": "dbauth"
        }
    },
    keepConnected : true <-- if set to true, check connexion every 2s
};

db.connect(param).then(() => {
  //...
}).catch((e: any) =>  {
  //...
});

This method create db.client witch is active db connexion (the connected MongoClient) db.usedDB : dbname db.config : all configuration parameters db.connected : last connection check result

Default parameters if not overxwrited are :

  defaultParameters = {
      authSource: (helper.config.dbName !== undefined) ? helper.config.dbName : 'admin',
      keepAlive: true,
      tlsAllowInvalidCertificates: true,
      tlsAllowInvalidHostnames: true,
      tlsInsecure: true,
      sslValidate: false
  }

All parameters could be overwrited

example with native function call

db.client.db().collection('colname').find({}).toArray().then((list) => {
    return Promise.map(list, (el) => {
        console.log(el)
    }, { concurrency: 4 });
})

find

db.find(collection, filter, projection, sort, limit)

db.find('test',{_id:'test'},{_id:0,order:1,info:1},{order:-1},10).then((result) => {
  //...
}).catch((e: any) =>  {
  //...
});

aggregate

db.aggregate(collection, AggResquests)

db.aggregate('test',[{...},{...}]).then((result) => {
  //...
}).catch((e: any) =>  {
  //...
});

insert

db.insert(collection, documents)

db.insert('test',[{"a":"b"},{"c":"d"}]).then((result) => {
  //...
}).catch((e: any) =>  {
  //...
});

update

db.update(collection, filter, values, upsert)

db.update('test',{_id:'test'},{order:2,info:"new test"},true).then((result) => {
  //...
}).catch((e: any) =>  {
  //...
});

eval raw request

db.eval(rawScript, inputs)

// inputs is an object
let inputs = { database : "exemple", collection" : "demo" }
// rawscript must return a promise
let rawScript = "db.db(inputs.database).collection(inputs.collection).find({}).toArray()";
db.eval(rawScript,inputs).then((result) => {
  //...
}).catch((e: any) =>  {
  //...
});

replace

db.replace(collection, filter, to)

db.replace('test',{_id:'test'},{ 'element':'test'}).then((result) => {
  //...
}).catch((e: any) =>  {
  //...
});

more informations

db.count = (collection: string, filter: any)

count documents associated with filter

db.purgeBefore = (collection: string, filterKey: string, olderThanSecond: number)

purge elements where filterKey key is older than olderThanSecond parameters

db.delete = (collection: string, filter: any)

delete elements corresponding to filter

db.eval = (rawScript: string, inputs: any)

Allow to run script on server side with inputs as parameters, db as database connection and rawscript the script to eval. waring about security issue on bad use.

db.aggregate = (collection: string, AggResquests: any)

the aggregate request

db.find = (collection: string, filter: any, projection: any, sort: any, limit: any)

the find request. If filter is an array, run all filter and return an array of all results If not define, limit is 100

db.insert = (collection: string, documents: any)

the insert command, single or array of documents

db.replace = (collection: string, filter: any, to: any)

The replace command, as find filter is selector (single or array of selectors) and to the set of values

db.update = (collection: string, filter: any, values: any, upsert: any)

The update command, as find filter is selector (single or array of selectors)

db.collections = ()

get list of collections