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

schematize

v0.1.2

Published

An encapsulation of 'Sequelize' and 'treble' packages to handle 3 databases using external schema files.

Downloads

23

Readme

Schematize

schematize is a node.js package that is an encapsulation of Sequelize and treble packages to handle 3 databases using external schema files.

Having access to 3 databases simultaneously and using external schemas and promise-like syntax permit to write business apps lot more easily.

To use schematize you should add schematize, sequelize, mysql and treble packages to your project.

How does it work?

    var db3 = require("treble")( Sequelize ,"mysql://login:password@host:port/webase");
    var schematize = require("schematize");

    schematize("admin","users",db3)
        .then( function(tUsers){ // success
             ...
            },
            function(){ // failor
                console.log("An error occured");
            }
        );

schematize is called with 3 parameters:

  • db : database admin|modele|user.
  • schema : Is the table name, described by a schema file
  • db3 is a connection object created with treble package.

Where to put schemas ?

To configure the path where to find schema files, you should use one of the two syntaxes:

syntax 1:

    var schematize = require("schematize").config({path:__dirname+"/schemas/"})

In this case, schemas are in 3 subdirectories named "admin/","modele/","user/" from the specified path.

syntax 2:

    var schematize = schematize.config({path:{admin:__dirname+"/schemas/admin/", modele:__dirname+"/schemas/modele/",user:__dirname+"/schemas/user/"})

Example of content for users.js:

module.exports = function(DataTypes) {
  return ['users',{
     userID:{type:DataTypes.INTEGER, autoIncrement: true, allowNull: false, primaryKey:true},
     login:{type:DataTypes.STRING(25),  allowNull:false, unique:true},
     email:{type:DataTypes.STRING(150), allowNull:true,  unique:true, validate:{isEmail:true}},
     password:{type:DataTypes.STRING(50),allowNull:false},
    },
    {
     charset: 'utf8',
     collate: 'utf8_general_ci',
     comment:"Comprehensive list of all users"
    }];
}

The object given by the success handler offers the following methods:

  • tUsers.find( {options}) -> promise,function
  • tUsers.findAll( {options})
  • tUsers.count( {options} )
  • tUsers.update(values , where , fields)
  • tUsers.destroy({where:{filter}})
  • tUsers.create({values},[fields])

These methods are almost the same methods offered by sequelize. They can be managed using handlers:

  • .success(handler)
  • .error(handler)
  • or a promise-like syntaxe .then( successHandler , errorHandler )

The success handler has a argument that is a function. Calling it permits to have the result data in an array or in an object directly.

Example

    var db3 = require("treble")( Sequelize ,"mysql://login:password@host:port/webase");
    var schematize = require("schematize");

    schematize("admin","users",db3)
        .then( function(tUsers){
            tUsers.findAll({limit:10}).then(
                    function(data){console.log("Users list:",data(["login","email"]));
                        console.log("Users list as an object:",data(["login","email"],"login"));
                        tUsers.create({login:'mylogin',password:'mypassword',email:'[email protected]'},['login','password','email']).then(
                            function(e){ 
                                console.log("New user inserted: ",e(['userID','login','password','email']));
                            },
                            function(){
                                console.log("User insertion failed..");
                            }
                        );
                    },
                    function(err){
                        console.log("Error reading data.");
                    });
            },
            function(e){
                console.log("Error when opening table.")
            }
        );

An other example with destroy:

    var db3 = require("treble")( Sequelize ,"mysql://login:password@host:port/webase");

    require("schematize")("admin","users",db3)
        .then( function(tUsers){
            tUsers.destroy({where:{userID:6}}).then(
                function(e){
                    console.log("destroy success.."));
                },
                function(e){
                    console.log("destroy fail!");
                });
            })

    }
}

Batch processing

When you have to open more than ONE table to do a task, you have to use the batch style of the command by giving an array as 'schema' argument. The succes event is fired when ALL the tables are opened. If ONE schema at least does not exist, an error is fired:

schematize("admin",["users","companies","databases"],db3)
    .then( function( tables ){
        tables.users.find( {login,"myLogin"}).then(
            function( oneLogin ){
                console.log( "User credencials: ",oneLogin(['login','password','email']) );
            },
            function(  ){
                console.log( "User not found!" );
            },
        )
    },
    function(tables){
        console.log("An error occured..");
    });

Force table drop

If you want to open one or more new tables and drop them if they already exist, you can add a 4th argument (force) for schematize(...) :

// tables dropped if exist
schematize("user",["products","custumers","bills"],db3,true)
    .then( function( tables ){
    }