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

mongoose-babelmodel

v0.1.7

Published

ES7 Model base for MongooseJS Models

Downloads

30

Readme

mongoose-babelmodel

A library for using babeljs to create mongoose models from classes

Installation

npm i --save mongoose-babelmodel

Requirements

BabelJs for compiling to ES5 using --stage 0 flag for decorators

Usage Example

import {Model, pre} from 'mongoose-babelmodel'

class User extends Model {
    name = String;
    password = String;
    email = String;
    roles = {type: Array, default: ['Subscriber']};
    _posts: Array;
    
    // added to schema using .method()
    verifyPassword(password) {
        let encrypted = yourEncryptionFunc(password);
        return encrypted === this.password;
    }
    
    // added to schema using .method()
    hasRole(role) {
        return role.name in this.roles;
    }
    
    // added to schema using .pre()
    @pre('save')
    static encryptPass(next) {
        this.password = yourEncryptionFunc(this.password);
    }
    
    // added to schema using .virtual().get()
    get posts() {
        return this._posts;
    }
    
    // added to schema using .virtual().set()
    set posts(posts) {
        this._posts = posts;
    }
    
    // added to schema using .static
    static usersWithPosts() {
        return this.find({$where: 'this.posts.length > 0'});
    }
}

let user = new User();

export default user.generateModel();

Declaring Schema

You can declare your schema as class properties ( ES7 stage 0 candidate ) in the following fashion:

class Example extends Model {
    _schema = {
        name: String,
        type; String
    };
    
    // OR 
    
    name = String;
    type = String;
}

You can also declare your _schema in the constructor.

class Example extends Model {
    constructor () {
        super();
        this._schema = { name: String, type: String };
    }
}

Or you can rely on the super constructor to pass in your schema upon initialization

class Example extends Model {
    instanceMethod () {
        return this.name;
    }
}

let example = new Example({name: String, type: String});

You can add schema paths easily

example.newPath = {type: Boolean, default: true};

You can remove schema paths easily

delete example.newPath;

How it works: the _schema property is what is used during model generation. however all instance items are added to the _schema object during generateSchema(). Please note that _schema is the base. If you declare a path in _schema and then add the same path to the instance object it will overwrite. Like so:

class Example extends Model {
    _schema = {
        name: String,
        type; String
    };
}

let example = new Example();

// During generateSchema _schema.type will be set to {type: String, default: 'Test'}
example.type = {type: String, default: 'Test'};

API Documentation

Model

The Model class is the base class that contains the core functions for generating the Mongoose model. It has the following methods available:

model.extend(modelInstance)

You can call extend and pass in another instance of model extended from babelmodel in order to combine all methods, Schema paths, pre/post middleware, static functions and validators into the calling instance.

class Post extends Model {
    @pre('save')
    static generateSlug(next) {
        this.slug = this.title.toLowerCase().replace(' ', '-');
        next();
    }
}

class Tag extends Model {
    static withTag(tag) {
        return this.find({tags: {$in: tag}}).exec();
    }
}

// String 'ObjectId' as schema type will get replaced with Schema.Types.ObjectId
let post = new Post({
    title: {type: String, required: true},
    content: {type: String, required: true},
    author: {type: 'ObjectId', ref: 'User'}
});

let tag = new Tag({
    tags: {type: Array, default: []}
});

post.extend(tag);

export default post.generateModel();
model.generateSchema()

generates the mongoose schema and adds all functions/middleware/validations/virtuals to the appropriate places. Getter and Setter functions are added using .virtual(name).get() and .set() respectively. Static functions get added using .static() Regular functions added through .method() calls Specially annotated functions (@validation, @pre, @post) get added using appropriate markup

model.generateModel()

returns mongoose.model() call, with your Model's name as the name and the generated schema

model.schema

Getter/setter functions. schema.add(schema, overwrite = false) allows you to add schema paths to a model instance before generateModel() is called schema.remove(schema) - as you would expect, removes schema paths that are found in the schema parameter schema.list() - returns the schema object schema = {} - sets the schema object overwriting anything previously set.

Method Decorators

All of the following decorators are also exported by the module. They must be declared on static methods.

pre(action, priority = 10)

Decorators that wrap the decorated function, creating a new function that allows it to be automatically added to the schema during model.generateSchema(). action accepts any hook action defined in the mongoose documentation. Priority allows you to control the order in which hooks are executed. the lowest priority hook is executed last, highest first. Defaults to 10. Hooks added through extensions will honor this order as well so if you add a hook in a extension with a very high priority it will happen first as long as there isn't a hook with a higher priority on the base model.

post(action)

Decorators that wrap the decorated function, creating a new function that allows it to be automatically added to the schema during model.generateSchema(). action accepts any hook action defined in the mongoose documentation

validation(path, message)

Decorator that wraps the function, creating a new function that allows it to be automatically added to the schema during model.generateSchema(). path accepts a string referencing the path to validate, message is the error message returned if the validation fails.

plugin(plugin, options = {})

Decorator for the class to use plugins. Just pass in the plugin function and options. Can stack multiple plugins. Plugins are added right before the schema is returned so they will be the last things added to the stack.

@plugin(autopopulate)
@plugin(findorcreate)
class Document {
    // Your class definition
}