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

mycro-sequelize

v0.1.0

Published

sequelize adapter for mycro apps

Downloads

3

Readme

mycro-sequelize

a sequelize adapter for mycro apps.

Installing

Install sequelize and the adapter

npm install --save sequelize mycro-sequelize

Install appropriate dialects

npm install --save mysql|pg & pg-hstore|tedious|sqlite3

Getting Started

Define a sequelize supported connection:

// in config/connections.js
const sequelizeAdapter = require('mycro-sequelize');

module.exports = {
    //..
    mysql: {
        adapter: sequelizeAdapter,
        config: {
            // include a connection url, or
            url: '<connection uri>'

            // include username, password, and database
            username: 'bob',
            password: 'password',
            database: 'test',

            // define additional options to pass to sequelize
            options: {
                dialect: 'mysql',
                host: 'localhost',
                port: 3306,
                pool: {
                    // ..
                },
                //..
            }
        }
    }
    //..
}

Define one or more models. Each model module must export an object that defines a factory function. This factory function receives the sequelize instance, the Sequelize constructor, the current model name, and the mycro instance as arguments. It should return a sequelize model.

// in app/models/user.js

module.exports = {
    factory(sequelize, Sequelize, name, mycro) {
        return  sequelize.define(name, {
            first: {
                type: Sequelize.STRING
            },
            last: {
                type: Sequelize.STRING
            }
        });
    }
}

Use them in your app!

// in some controller
const async = require('async');
const joi = require('joi');

module.exports = function(mycro) {
    return {
        /**
         * Create user endpoint
         * @param  {Object} req
         * @param  {Object} req.body
         * @param  {String} req.body.first
         * @param  {String} req.body.last
         * @param  {Object} res
         */
        create(req, res) {
            const Users = mycro.models.user;
            async.waterfall([
                function validateRequest(fn) {
                    const schema = joi.object({
                        first: joi.string().alphanum().trim().lowercase().required(),
                        last: joi.string().alphanum().trim().lowercase().required()
                    }).unknown(false).required();
                    joi.validate(req.body, schema, function(err, validated) {
                        if (err) {
                            res.json(400, {errors: [err]});
                            return fn(err);
                        }
                        fn(null, validated);
                    });
                },

                function createUser(validated, fn) {
                    Users.create(validated).nodeify(function(err, user) {
                        if (err) {
                            res.json(500, {errors: [err]});
                            return fn(err);
                        }
                        res.json(200, user.get({ plain: true }));
                        fn();
                    });
                }
            ]);
        }
    }
}

Assocations

Currently, associations must be defined separately from this hook. You can easily accomplish this by defining your own hook and including it in the hook config.

// in hooks/associations.js

module.exports = function(done) {
    const models = this.models;
    const Users = models.user;
    const Groups = models.groups;

    Users.belongsToMany(Groups, {as: 'groups', through: 'user_groups', foreignKey: 'userId'});
    Groups.belongsToMany(Users, {as: 'users', through: 'user_groups', foreignKey: 'groupId'});

    process.nextTick(done);
}

Testing

  1. Make sure you have an up to date docker toolkit installed
  2. Build the container
docker-compose build
  1. Run the test suite
docker-compose up

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Copyright (c) 2016 Chris Ludden. Licensed under the MIT icense