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 🙏

© 2025 – Pkg Stats / Ryan Hefner

froncubator-mongo

v0.0.9

Published

MongoDB library with typing and schema-models.

Downloads

30

Readme

froncubator-mongo

MongoDB library with typing and schema-models. https://github.com/froncubator/froncubator-mongo

Installation and Usage

npm install froncubator-mongo

Quick Start

const FroncubatorMongo = require('froncubator-mongo')

async function main() {
    // Init library
    const db = new FroncubatorMongo()

    // Connect to MongoDB
    await db.connect('mongodb://user:password@ip_address:mongo_port/db_name', 'db_name')

    // Create schema with default values.
    // These values are used in the library for type checking.
    db.models.user = function() {
        return {
            firstName: '', // default values
            lastName: '',
            age: 0,
            interests: []
        }
    }

    // Allmost all FroncubatorMongo methods return {promise}
    await db.insert('user', {
        firstName: 'Jack',
        lastName: 'Black',
        age: 24,
        interests: ['beer', 'develop like a beast ;)', 'sleep']
    })
}

// ...
main()

Create Document

// create model-schema
db.models.post = function() {
    return {
        title: '',
        text: ''
    }
}

try {
    // Creating data. Return default response from mongodb driver
    // "result" Contains the result document from MongoDB
    // "ops" Contains the documents inserted with added _id fields
    // "connection" Contains the connection used to perform the insert
    let newPost = await db.insert('post', {
        title: 'Hello',
        text: 'World'
    })

    // Creating data with wrong type field.
    await db.insert('post', {
        title: 'Hello',
        text: 100
    })

    // You can use promise if you want, but
    // We recommend async/await way!
    db.insert('post', {
        title: 'Hello',
        text: 100
    }).then(
        data => {
            console.log(data)
        },
        error => [
            console.log(error)
        ]
    )

} catch(err) {
    console.log(err)
    // output
    //     type: 'insertValidateError',
    //     field: invalidKey,
    //     message: 'Error in collection - post. Field - text incorrect or type field is invalid.'
}

Insert and Update methods return "reject" if field has wrong type or not exist in "db.models"

Find Document

let posts = await db.find('post', { title: 'Hello' })
// output: [{ _id: *****, title: 'Hello', text: 'World' }]

let posts = await db.find('post',
    { title: 'Hello' }, // query param
    {  // options
        fields: { // include|exclude fields
            title: 1,
            text: 0
        },
        limit: 10, // limit data
        sort: : { title: 1 } // sort by field
    }, {
        cursor: true // and you can return 'cursor' instead [array]
        // or you can get 'count' of documents found
        // count: true
    })

You can use all query params and options from official docs http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#find

Update Document

// ... model-shema
await db.updateOne('post', { title: 'Hello' }, { $set: { text: 'Bro!' } })
await db.updateMany('post', { title: 'Hello' }, { $set: { text: 'Bro!' } })
// return reject if value has wrong type or not exist

You can use all features of mongodb. http://mongodb.github.io/node-mongodb-native/3.0/quick-start/quick-start/

Delete Documents

await db.deleteOne('post', { title: 'Hello' })
await db.deleteMany('post', { title: 'Hello' })
// return true/false or promise reject

Drop Collection

await db.dropCollection('post')
// return true/false or promise reject

Drop Indexes

await db.dropIndex('post') // drop all indexes from post collection
// return true/false or promise reject

Connect

await db.connect('some_mongo_url') // connect to DB
// return db object (by mongodb driver)
// if FroncubatorMongo lost connection, then module start "auto reconnect" after 5 seconds

Create ObjectID

db.toObjectID('59d10a3d320d720010522391')
// or you can send array and toObjectID will return ObjectID[]
db.toObjectID(['59d10a3d320d720010522391', '59d10a3d320d720010522391'])

Disconnect

await db.disconnect() // close db connection.
// return true/false or promise reject

Some features

Create instance of the class with additional params.

    const db = new FroncubatorMongo({
        // Add default query part. When you create new document, hide param will be added to your document by default with 0 value.
        // $ne param would be added to db.find by default
        defaultQueryPart: {
            hide: {
                default: 0,
                $ne: 1
            }
        }
    })

Execution time request in db.find()

    // Change MaxTimeMS (default: 90000 ms)
    db.maxTimeMS = 10000

add own db connection and add to FroncubatorMongo

    const FroncubatorMongo = require('froncubator-mongo');
    let db = null;
    const MongoClient = require('mongodb').MongoClient;
    // Connection URL
    const url = 'mongodb://localhost:27017';
    // Database Name
    const dbName = 'myproject';

    let fMongo = new FroncubatorMongo()

    // Use connect method to connect to the server
    MongoClient.connect(url, function(err, client) {
        db = client.db(dbName);
        fMongo.db = db // set db param from native driver
    });

Tested in Node.js v8+ and use async/await pattern. By Froncubator Pro

MIT license