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

mongool

v1.1.2

Published

API Library Wrapper for node.js MongoDB Driver

Downloads

7

Readme

Introduction

Example

User.js

'use strict'

var Mongool = require('mongool')
var ObjectID = require('mongodb').ObjectID

var User = Mongool.Model()

User.db = "test"
User.collection = 'Users'
User.schema = {
    "_id": {
        type: ObjectID,
    },
    "username": {
        type: String,
        unique: true,
        required: true,
        max: 20
    },
    "password": {
        type: String,
        min: 6,
        setter: function(value){
            //HASH FUNCTION HERE
            return value
        },
        required: true
    },
}

module.exports = User

App.js

var User = require('./User')
var Mongool = require('mongool')

//...

var connection = Mongool.connect() //reuse connection for each model

User.connection = connection

var currentuser = new User({
    username: "HelloWorld",
    password: "123456"
})


async function doStuff(){
    console.log(currentuser.get()) // { username: 'HelloWorld', password: '123' }
    await currentuser.validate()
    await currentuser.save()
    await currentuser.remove()
}

doStuff()

Coming Soon

  • Indexes
  • Optimisations
  • Mass Assignability
  • Hidden Variables (for .get()'s)
  • Better Getter/Setters
  • Better Validation
  • Better documentation
  • .save only on changes

Schema Rules

Types

// List of valid types
String
Number
ObjectID
Date
Boolean
[/*...*/]
{/*...*/}

Stacking Schemas

var schema = {
    "profile": {
        type: {
            "first-name": {
                type: String,
                max: 16
            }
            "last-name": {
                type: String,
                max: 16
            }
            "nationalities": {
                type: [{
                    type: String,
                    max: 30 //string max length
                }],
                max: 5 //array max length
            }
        }
    }
}

//valid document
{
    "profile":{
        "first-name": "James"
        "last-name": "Smith"
        "Nationalities": ["Australian", "British"]
    }
}

Validation Rules

{
    type: Type, //value must be one of the above types
    min: Number, //value must be this or above
    max: Number, //value must be this or below
    size: Number, //value must be exact
    required: Boolean, //value must not be empty if true. 
    //Empty means: '', [], {}, undefined
    present: Boolean, //value must be present if true
    //Present means: anything except undefined
    is: Array, //value must be any of the values in the array
    is: *, //value must equal exactly this
    after: Date, //value must be after a certain date
    before: Date, //value must be before a certain date
    regex: RegExp, //value must pass RegExp test
    alpha: Boolean, //value must be alpha /^[A-Za-z]+$/
    alphaNumeric: Boolean, //value must be alpha-numeric /^[A-Za-z0-9]+$/
    alphaDash: Boolean, //value must be alpha-dash /^[\w-]+$/
    custom: Function, //value determined by function, more below
    unique: Boolean, //value must not be in database
}

Other Schema Rules

{
    default: var //default var if value undefined
    custom: function(document, item, schema, level, model){
        var errors = {}

        //...

        return errors
    }
}

Model Functions

Statics

var count = await User.count(query)
var user = await User.findOne(query)
var connection = await User.CollectionConnection()
var users = await User.find(query)

State

var user = new User()

user.get()
await user.validate()
await user.save()
await user.remove()

Custom Statics

User.findByUsername = async function findByUsername(username){
    return await this.findOne({"username": username})
}