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

mongodb-if

v1.4.4

Published

MongoDB Interface

Downloads

16

Readme

MongoDB Database Interface

A simple MongoDB interface library built upon the mongodb library

npm npm bundle size npm downloads

Import

import { MongoDBHandler } from 'mongodb-if'

Initialise

The MongoDBHandler class is the core of the MongoDB interface, you can create the database interface instance using:

const DBHandler = MongoDBHandler({connectionDomain, connectionOptions, dbNameList, dbListOptions});

or

const handlerOptions: MongoDBInterface = {
  connectionDomain: 'mongodb://127.0.0.1:27017',
  connectionOptions: {
    ...
  },
  dbNameList: ['DB1', 'DB2'],
  dbListOptions: [{ replicaSet: 'myRepl' }],

const DBHandler = MongoDBHandler(handlerOptions);

Initialisation parameters

  • connectionDomain refers to your MongoDB connection string, a typical connection string might be mongodb://127.0.0.1:27017
  • connectionOptions refer to your MongoDB connection options which can be found under MongoDBClient documentation and are defined by the interface MongoClientOptions
  • dbNameList is a string array containing the names of each database you wish to connect to.
  • dbListOptions is a MongoClientCommonOption array containing the connection parameters for each database in dbNameList (see Connection string options)

Available Functions

All the following command have the optional argument dbName (note: if this is not provided, the first (or only) database in list will be selected)

connect()

Establish a connection to each of the requested databases Returns Promise Success

disconnect()

Close a connection to all databases Returns boolean Success

getDatabaseObjectList() :MongoDBListInterface[]

Get the list of associated databases, includes name and database object Returns MongoDBListInterface[] Database list

getDatabaseClient()

Return the MongoDB Client Object. Typically used for MongoDB commands that are not supported under this library (however issues and PRs are appreciated)

getDatabaseObject(string)

Return a MongoDB Database Object. Typically used for MongoDB commands that are not supported under this library (however issues and PRs are appreciated)

getDataItem(HandlerGetItemSingle)

Retrieve a data item, Command arguments, Required: collectionName and Query

getDataItemsMany(HandlerGetItemMany)

Retrieve multiple data items, Command arguments, Required: collectionName, Optional: Query, limit, skip, sort

  • limit: Pass a number to restrict number of items retrieved
  • skip: Pass a number to skip to first x matching results
  • Sort: Pass an object for a field to sort by, use 1 for ascending, -1 for descending. E.g. { age: 1 }

addDataItem(HandlerAddItem)

Add a data item, Command arguments, Required: collectionName and data, Optional: options

addMultipleDataItems(HandlerAddItemMultiple)

Add multiple data items, Command arguments, Required: collectionName and data, Optional: options

modifyDataItem(HandlerEditItem)

Modify or append a data item, Command arguments, Required: collectionName, query and Data, Optional: options

modifyDataItemsMany(HandlerEditItem)

Modify or append multiple data items matching query, Command arguments, Required: collectionName, query and Data, Optional: options

countDataItems(HandlerCountItems)

Determine number of data items in the database collection, Command arguments, Required: collectionName, Optional: query & options

isCapped(HandlerIsCapped)

Returns whether the collection is capped or not, Command arguments, Required: collectionName, Optional: options

getOptions(HandlerGetOptions)

Returns collection options, Command arguments, Required: collectionName, Optional: options

getFieldList(HandlerGetFieldList)

Retrieve every value with the matching field (database key) as a list, Command arguments, Required: collectionName & fieldName, Optional: options, query

getIndices(HandlerGetindices)

Returns an array that holds a list of documents that identify and describe the existing indexes on the collection, Command arguments, Required: collectionName, Optional: options

renameCollection(HandlerRenameColleciton)

Rename a collection Command arguments, Required: collectionName, fieldName (new collection name), Optional: options

deleteItemSingle(HandlerDeleteItem)

Delete a single item as defined by the passed query, Command arguments, Required: collectionName & query, Optional: options

deleteItemMany(HandlerDeleteItem)

Delete all items matching the passed query, Command arguments, Required: collectionName & query, Optional: options

doesCollectionExist(HandlerBaseRequest)

Check if a collection exists in the DB, Command arguments, Required: collectionName

createCollection(HandlerModifyCollection)

Create a new collection, Command arguments, Required: collectionName, Optional: options

dropCollection(HandlerModifyCollection)

Drop the entire collection, Command arguments, Required: collectionName & query, Optional: options

dropDatabase(databaseName)

Drop the entire database. Database name is required.

Exported Interfaces

  • MongoDBInterface : Used to initialise the MongoDB handler
  • MongoDBListInterface : Stores information about each database

Examples

See Library Test for some basic usage examples.