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

@soulmaneller-dev/nodejs-model-mysql

v0.0.6

Published

This is a simple mysql model

Downloads

4

Readme

node-mysql-model

Build Status npm

This is a simple model for MySQL which you can manage your own connection as you want. Support a single connection, pool connection or cluster connection.

This module support callback and promise

For example, you create a mysql connection with mysql module and you want to use pool connection. You just create the connection then pass that connection to this module. you will never need to create the mysql connection again and again.

Note The connection need to have method query

Installation

npm install @soulmaneller-dev/nodejs-model-mysql

Usage

Simple use

cosnt mysql     = require( 'mysql' );
const { Model } = require( '@soulmaneller-dev/nodejs-model-mysql' );

const db = mysql.createConnection({
    host     : 'host',
    user     : 'user',
    password : 'secret'
});

const users = new Model( db, options );

/*********************
 * Using method find
 *********************/
users.find( id ).then( result => {
    // do something
});

// or callback
users.find( id, ( err, result ) => {
    // do something
});

/*********************
 * Using method select
 *********************/
users.select( 'id', 'name' ).exec().then( result => {
    // do something
});

// or callback
users.select( 'id', 'name' ).exec(( err, result ) => {
    // do something
});

Extending

You can also use extend

cosnt mysql     = require( 'mysql' );
const { MySQLModel } = require( '@soulmaneller-dev/nodejs-model-mysql' );

const db = mysql.createConnection({
    host     : 'host',
    user     : 'user',
    password : 'secret'
});
const Model = new MySQLModel( db );

const users = Model.extend({ table: 'users', primaryKey: 'user_id' });
users.find( 1 ).then( result => {
    // do something
});

const movies = Model.extend({ table: 'movies', primaryKey: 'movie_id' });
movies.select( 'movie_id', 'movie_name' ).exec().then( result => {
    // do something
});

Options

  • table: Table name [Required]
  • primaryKey: Primary key name

Methods

find( value [, callback ] )

Find a row from primary key [Required primaryKey].

Note: This method no need to call exec()

sql( queryString [, values ] [, callback ] )

Query data by custom query string

Note: This method no need to call exec()

select([ fieldname [, fieldname... ] ]).exec([ callback ])

Select only some field or leave it empty for *

insert([ fieldname [, fieldname... ] ]).values( value [, value... ]).exec([ callback ])

Insert 1 or more row to database. You can leave fieldname as empty for insert follow table schema sequence.

If you want to insert more than 1 row you can add .values( ... )

update( fieldname, [, op ], value ).exec([ callback ])

Update row(s). You can use .set( fieldname, [, op ], value ) for many fields

op is an operator if you don't set it will be = by default.

delete( fieldname, [, op ], value ).exec([ callback ])

Delete row(s). op is an operator if you don't set it will be = by default.

Additional

where( fieldname, [, op ], value )

Add condition in WHERE.

op is an operator if you don't set it will be = by default.

whereOr( fieldname, [, op ], value )

Add condition in WHERE with OR

op is an operator if you don't set it will be = by default.

whereIn( fieldname, value )

Add condition in WHERE using operator IN

Note: This must be the first condition

orderBy( fieldname [, fieldname... ])

Query by order by fields

limit( number [, to ])

Query by limit the rows

Example

const users = new Model( db, { table: 'users', primaryKey: 'user_id' });

// Select user where user_id = 1 and user_status = 'active'
users.select()
    .where( 'user_id', 1 )
    .where( 'user_status', 'active' )
    .exec()
    .then( result => {
        // do something
    });

// Select user where user_status = 'active' and user_age > 10 order by user_name limit 5
users.select()
    .where( 'user_status', 'active' )
    .where( 'user_age', '>', 10 )
    .orderBy( 'user_name' )
    .limit( 5 )
    .exec()
    .then( result => {
        // do something
    });