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

rm-mysql

v1.0.10

Published

Extends mysql npm's package and provide a cli for database schema diff and updating

Downloads

2

Readme

Description

Extends mysql npm's package and provide a cli for database schema diff and updating

Install

$> npm -g install rm-mysql

Usage

var config = require('./config.js');
var Model = require('rm-mysql');
var model = new Model(config);

config & cli

Create a config.js file at the root of the project with a mysql field containing the connection configuration :

$> emacs config.js
module.exports = {
    mysql: {
        host: 'xxx',
        user: 'xxx',
        database: 'xxx_xxx',
        password: 'xxx',
        charset: 'utf8mb4',
        /* use any config supported by npm mysql */
    },
    logger: console,
    schema: require('./schema.js'),
}

create a schema file

$> emacs schema.js
module.exports = {
    user: {
        columns: {
            id: { nullable: false, type: 'int', autoincrement: true, primary: true },
            enable: { nullable: false, type: 'tinyint', default: '0' },
            name: { nullable: false, type: 'varchar', maxlength: 50, collation: 'utf8mb4_general_ci' },
            email: { nullable: false, type: 'varchar', maxlength: 50, collation: 'utf8mb4_general_ci' },
            password: { nullable: false, type: 'varchar', maxlength: 50, collation: 'utf8mb4_general_ci' },
            groupId: {
                nullable: true,
                type: 'int',
                foreign: {
                    name: 'user_group',
                    table: 'group',
                    column: 'id',
                    delete: 'CASCADE',
                    update: 'CASCADE',
                },
            },
            updatedAt: { nullable: false, type: 'datetime' },
            createdAt: { nullable: false, type: 'datetime' },
        },
        uniques: {
            uniqueEmail: ['email'],
        },
        comment: "user table contains all data of the user",
    },
    group: {
        columns: {
            id: { nullable: false, type: 'int', autoincrement: true, primary: true },
            name: { nullable: false, type: 'varchar', maxlength: 50, collation: 'utf8mb4_general_ci' },
            updatedAt: { nullable: false, type: 'datetime' },
            createdAt: { nullable: false, type: 'datetime' },
        },
        comment: "group table",
    },
}

Will generate the proper "create / alter table" to update database to match schema.js

$> modeltools update

Will do the opposite. Generate a schema from the database schema

$> modeltools update

Execute custom sql on database

$> modeltools exec "select 1"

See more commands by typings

$> modeltools

query

model.query('select 1 as x', function(err, data) {
    // data : [{x: 1}]
});

model.queryOne('select 1 as x', function(err, data) {
    // data : {x: 1}
});

model.queryNb('select 1 as x', function(err, data) {
    // data : 1
});

escape

escape a single value

model.escape('a')
// 'a'

escape multiple ids : ensures all given ids are integers (for use in IN clause for exemple) :

model.escapeIds('1,2,3')
// 1,2,3

clean an object according to database schema. will sanitize fields for a safe insert/update and removes fields that do not exist in database.

var cleanObject = model.clean(object)

insert

data is autoescaped according to model schema. The generated id is returned automatically

model.insert('user', {
    name: 'test',
    age: 12
}, function(err, data){
    /*
    data : {
        id: 1,
        name: 'test',
        age: 12,
    }
    */
})

insertIgnore('user', object, callback);
insertMulti('user', arrayOfObjects, callback);

update

In the user table, the id is a primary key, therefore it will generate "where id = 1" and update the name

model.update('user', {
    id: 1,
    name: 'newname',
}, function(err, data){
})

if the key already exists update instead of inserting

model.insertOrUpdate('user', object, callback);

delete

model.delete('user', 1, callback)

generating where clause

var where = [];
where.push('name like '%test%')
where.push('age > 20')
model.query('select * from user ${model.where(where)}')

generating order by

var orderBy = {id: 'ASC'};
model.query('select * from user ${model.orderBy(orderBy)}')

closing connections

model.end();

log format

This package calls the logger in that way :

logger[level](key, message, obj, callback);

  • level : can be info, error, warn
  • key : the package name
  • message : blablabla
  • obj (optional) : additional data in an object
  • callback (optional) : called when the data is logged

test

In a terminal

cd test
docker-compose up

In another

cd test
node ../modeltools.js create
node ../modeltools.js update
mocha