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

mongfaux

v0.9.1

Published

A flat JSON file database; reverse engineer of a good portion of the mongodb nodejs API; great for prototyping.

Downloads

3

Readme

Mongfaux

Mongfaux is a reverse engineering of the mongodb interface. It saves the data as flat JSON files for fast prototyping and/or a Git-able datastore.

Shortcomings

  • Mongfaux is <600 lines of code and only attempts to replicate a small, heavily used, portion of the API. You should consider using a real database if you need more.

  • Mongfaux uses a simple string for the auto-generated "_id" field. I would suggest ignoring _id and creating your own "id" field using something like shortid. Alternatively you can manually set your own _id with require('mongodb').ObjectID.

  • Doesn't have your back with things like enforcing unique keys. Inserting two documents with the same "_id" will pass through no problem.

  • Does not produce very helpful errors

  • Very little consideration has been given to performance. Use mongoDB if that's a priority!

  • Callbacks didn't make the cut, use promises

Installation

npm install mongfaux

Usage

const db = require('mongfaux');

await db.collection('book').insertOne({author: 'foobar', title: 'Mongfaux'});

let books = await db.collection('book').find({}).toArray();

//.json files saved in process.cwd()/mongfaux/ by default

Multiple instances

const Mf = require('mongfaux');
const db1 = Mf.instance();
const db2 = Mf.instance();

db1.name('db1');
db2.name('db2');

//mongfaux/
//--db1/
//--db2/

Queries

let longJohnBooks = await db.collection('books').find({
    'author.firstName': 'John',
    'pages': {
        '$gte': 300
    }
}).toArray();
  • Understands nested queries like {'author.firstName': 'John'}
  • Accepts the following operators: $ne $gt $gte $lt $lte $in $nin $eq $exists
  • Can query values in a document array. {tags: 'blue'} will select {_id: '', tags: ['blue', green]}, but does not understand array query operators ($all, $elemMatch, $size)

Updates

await db.collection('book').insertOne({
    title: 'Good Book',
    author: 'John Doe',
    reviews: 'positive'
})
await db.collection('book').findOneAndUpdate(
     {title: 'Good Book'},
     {
         $rename: {title: 'lametitle'},
         $set: {author: 'Jane Doe'},
         $unset: {reviews: true}
     }
 );
/*{
    lametitle: 'Good Book',
    author: 'Jane Doe'
}*/
  • Doesn't accept shorthand updates like {author: 'Jane Doe'}
  • Accepts the following operators: $inc $min $max $mul $set $unset $rename $currentDate $addToSet $pop $push

Projections

let firstNames = await db.collection('book').find({}).project({
    {'author.firstName': 1, _id: 0, title: 1}
}).toArray();
/*[
    {author: {firtName: 'John'}, title: 'My Book'},
    {author: {firstName: 'Jane'}, title: 'Hello World'}
]*/
  • Accepts nested keys
  • Doesn't accept operators

API

Db

.collection(name)

Returns the collection. If the collection doesn't exist, a new collection and .json file will be created

.parentDirectory( directory )

Sets the parent directory of the "mongfaux" folder where .json files are saved. Directory is process.cwd() by default.

.db( dbname )

Sets the database name. Also uses "mongfaux/dbname" folder for .json files. Has "name()" alias.

Collection

.createIndex()

Does nothing

.insertOne( document ) -> Promise

Autogenerates _id key and inserts document into the collection

  • Returns e.g. {insertedCount: 1, ops: [document], insertedId: id}

.insertMany( documents ) -> Promise

Autogenerated _id key for each document and inserts each document.

  • Returns e.g. {insertedCount: 1, ops: documents, insertedIds: []}

.find( query, options ) -> Cursor

Finds one or more documents in the collection.

  • Requires .toArray() to return a promise.
  • Option object accepts: sort, skip, limit, and projection

.findOne( query ) -> Promise

Finds a single document.

  • Does not require .toArray()
  • Options object accepts: sort, skip, and projection options

.findOneAndUpdate( query, update, options ) -> Promise

Finds and updates a single document.

  • Returns e.g. {value: document, ok: 1}.
  • Options object accepts: sort, upsert, returnOriginal, projection
  • returnOriginal is true by default

.updateOne( query, update, options ) -> Promise

Updates a single document without returning any documents.

  • Returns e.g. {matchedCount: 1, modifiedCount: 1, upsertedId: id}
  • Options object accepts: upsert

.updateMany( query, update, options ) -> Promise

Updates many documents without returning any documents.

  • Returns e.g {matchedCount: 1, modifiedCount: 1, upsertedId: id}
  • Options object accepts: upsert

.findOneAndDelete ( query, options ) -> Promise

Finds, returns, and deletes a single document.

  • Returns e.g. {ok: 1, value: document}
  • Options object accepts: sort, projection

.deleteOne ( query ) -> Promise

Deletes a single document without returning any documents.

  • Returns e.g. {deletedCount: 1}

.deleteMany ( query ) -> Promise

Delete multiple documents without returning any documents.

  • Returns e.g. {deletedCount: 1}

.findOneAndReplace ( query, document, options ) -> Promise

Finds a single document and replaces it.

  • Returns e.g. {value: }
  • Options object accepts: sort, upsert, returnOriginal
  • returnOriginal is true by default

.replaceOne ( query, document, options ) -> Promise

Replaces a document without returning any documents.

  • Returns e.g. {matchedCount: 1, modifiedCount: 1, upsertedId: id}
  • Options object accepts: upsert

Cursor

.project ( object ) -> Cursor

Sets a field projection for the query result

.skip ( amount ) -> Cursor

Skips over a number of documents at the beginning of the query result

.limit ( amount ) -> Cursor

Specifies the maximum number of documents to return

.max ( indexBounds ) -> Cursor

Specifies the exclusive upper bound for a specific index

  • e.g {countingid: 10000}

.min ( indexBounds ) -> Cursor

Specifies the inclusive lower bound for a specific index

  • e.g {countingid: 10}

.sort ( object ) -> Cursor

  • e.g {date: -1} for sorting by date in a descending order

.toArray () -> Promise

  • Finishes query and returns the result.

.count () -> Promise

  • Finishes query and returns the number of documents.