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

mongo-schema-gen

v0.0.2

Published

Simple mongoDB collection schema generator

Downloads

29

Readme

mongo-schema-gen

Build Status npm Downloads Licence

Simple mongoDB collection schema generator

So you just inherited a codebase that uses MongoDB and the database contains lots of collections with lots of documents saved with no well defined Schema. That is where mongo-schema-gen comes in. You can easily query any collection in a MongoDB database for document structure that cuts across all documents in said collection and you get an assumed Schema that contains all fields and type of data stored in them.

Installation

Install with npm

$ npm install mongo-schema-gen --save

How to use

var schemaGen = require('mongo-schema-gen');

Four major APIs are exposed which can be used to query mongoDB for collection schema and other similar info

  • getKeys(collectionName, callBack)

The getKeys function returns all keys that has been used as fields in all documents saved in the specified mongoDB collection. The keys are non-repeating.


// connect to mongoDB and populate collection with dummy data
schemaGen.connect(mongoUrl, function (db) {

    var User = db.collection('users');
    User.insertOne({
      name: 'Mustapha Babatunde',
      age: 26,
      job: 'Software Engineer',
      dob: new Date
    });
    
    
    schemaGen.getKeys('users', function (keys) {
      console.log(keys.length); // logs 5, '_id' inclusive
      console.log(keys); // logs ['_id', 'name', 'age', 'job', 'dob']
    });
});
  • getSchema(collectionName, callBack)

The getSchema function returns a possible schema for documents in specified collection. It internally uses getKeys and fetches minimal amount of documents to validate keys against. It assumes that all documents store the same data type in all field with the same name. For example: if documentA = { userNumber: 20001 }, assumes that other documents with userNumber field also store a Number value in it.


// connect to mongoDB and populate collection with dummy data
schemaGen.connect(mongoUrl, function (db) {

    var Packages = db.collection('packages');
    Packages.insertMany([{
      name: 'mongo-schema-gen',
      purpose: 'Simple mongoDB collections schema generator',
      stars: 125000,
      forks: 99000,
      createdDate: new Date
    }, {
      name: 'mongo-schema-gen',
      purpose: 'Simple mongoDB collections schema generator',
      forks: 99000,
      contributors: 8727373
    }]);
    
    
    schemaGen.getSchema('packages', function (schema) {
        console.log(schema);
        // would log...
        /*
            {
                _id: { type: 'object' },
                name: { type: 'string' },
                purpose: { type: 'string' },
                stars: { type: 'number' },
                forks: { type: 'number' },
                createdDate: { type: 'date' },
                contributors: { type: 'number' }
            }
        */
    });
});
  • keyUsed(collectionName, key, callBack)

The keyUsed function returns true if key is in use in any document in specified collection, false otherwise


// connect to mongoDB and populate collection with dummy data
schemaGen.connect(mongoUrl, function (db) {

    var User = db.collection('users');
    User.insertOne({
      name: 'Mustapha Babatunde',
      age: 26,
      job: 'Software Engineer',
      dob: new Date
    });
    
    
    schemaGen.keyUsed('users', 'name' function (status) {
      console.log(status); // logs true
    });
});
  • stats(collectionName, callBack)

The stats function returns stats object of specified collection which include document count, collection size, average document size, capped status, etc... All sizes are in KiloByte.


// connect to mongoDB and populate collection with dummy data
schemaGen.connect(mongoUrl, function (db) {

    var Packages = db.collection('packages');
    Packages.insertMany([{
      name: 'mongo-schema-gen',
      purpose: 'Simple mongoDB collections schema generator',
      stars: 125000,
      forks: 99000,
      createdDate: new Date
    }, {
      name: 'mongo-schema-gen',
      purpose: 'Simple mongoDB collections schema generator',
      forks: 99000,
      contributors: 8727373
    }]);
    
    
    schemaGen.stats('packages', function (stat) {
        console.log(stat);
        // would log something like what's below
         /*
         {
            ns: 'mongo-schema-gen.packages',
            count: 2,
            size: 4,
            avgObjSize: 240,
            numExtents: 1,
            storageSize: 80,
            userFlags: 1,
            capped: false,
            indexDetails: {},
            totalIndexSize: 80,
            indexSizes: { _id_: 80 },
            ok: 1 
         }
         */
    });
});

Contributing

Contributions are welcome and will be fully credited.

We accept contributions via Pull Requests on Github.

Pull Requests

  • Document any change in behaviour - Make sure the README.md and any other relevant documentation are kept up-to-date.

  • Consider our release cycle - We try to follow SemVer v2.0.0. Randomly breaking public APIs is not an option.

  • Create feature branches - Don't ask us to pull from your master branch.

  • One pull request per feature - If you want to do more than one thing, send multiple pull requests.

  • Send coherent history - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.

Issues

Check issues for current issues.

Credits

License

The MIT License (MIT). Please see LICENSE for more information.