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

omysql

v0.0.13

Published

Easy way to use mysql

Downloads

4

Readme

omysql

defaultExtname defaultExtname

Easy way to use mysql

How to use.

# Install
npm install omysql
const OMysql = require('omysql');

// init
const mysqlHub = new OMysql({
    connectionLimit: 20,
    host: '127.0.0.1',
    password: '^-^lucky',
    port: 3306,
    user: 'root',
    database: 'testDB'
});

// 1、Query: `where gender = 'male' and age = 18`
const data = await mysqlHub.query(['id', 'name'], 'user_info_table', {
    gender: 'male',
    age: 18
});

OMysql

const OMysql = require('omysql');

// Generate Sql string
const sqlStr = OMysql.sqlBuilder.query(['id', 'name'], 'user_info_table', {
    gender: 'male',
    age: 18
});

console.log(sqlStr);
/**
 * {
 *      sqlStr: 'SELECT id, name from user_info_table where `gender` = ? and `age` = ?',
 *      params: ['male', 18]
 * }
 */

new OMysql(config)

const OMysql = require('omysql');
// init
const omysqlInst = new OMysql({
    connectionLimit: 20,
    host: '127.0.0.1',
    password: '^-^lucky',
    port: 3306,
    user: 'root',
    database: 'testDB'
});

API

omysqlInst.setConfig(newConfig)

Update config. Will merge with current config.

omysqlInst.createPool(db)

Create mysql pool.

  • db: Optional. Default this.config.database

omysqlInst.createConnection(db)

Create mysql connection.

  • db: Optional. Default this.config.database

omysqlInst.queryCore(sqlStr, sqlParams, keepConnection)

Execute SQL.

  • sqlStr: Like SELECT id, name from user_info_table where gender= ? andage = ?, You can use OMysql.sqlBuilder.query to generated. String
  • sqlParams: Like ['male', 18]. Array;
  • keepConnection: Optional, Default false. Boolean;

omysqlInst.query(keys, tableName, condisions, extra)

Query all data by some condisions. Return false / data list

  • keys: The keys you want to query(Set * to query all keys). Like ['name', 'age']. Array;
  • tableName: Mysql table name. String;
  • condisions: Query rules. You can use the following ways:
    1. {[key1]: value1, [key2]: value2}: Equal to where key1 = value1 AND key2 = value2;
    2. {key: 'id', desc: {rule: '=/</>/<=/>=/!=/like/not like/between/in/not in', params: String|Array}, rel: 'AND/OR'}: A complex set of conditions;
    3. [conditions1, condisions2]: Multiple sets of conditional combinations;
  • extra: Additional sql string. Like LIMIT 100;

omysqlInst.queryAll(keys, tableName, extra)

Query all data without condisions. Return false / data list

  • keys: The keys you want to query(Set * to query all keys). Like ['name', 'age']. Array;
  • tableName: Mysql table name. String;
  • extra: Additional sql string. Like LIMIT 100;

omysqlInst.queryOne(keys, tableName, condisions, extra)

The same with query, but only return one data. Return false / data

omysqlInst.del(tableName, condisions)

Delete data by some condisions. Return true/false

omysqlInst.insert(tableName, dataset, schema)

Insert data.

omysqlInst.insertBatch(tableName, itemKeys, valuesGroup, schema)

Batch insert data.

omysqlInst.update(tableName, condisions, dataset, schema)

Update data.

omysqlInst.updateOrInsert(tableName, condisions, dataset, schema)

If the target exist, update it, otherwise, insert a new record.

omysqlInst.insertIfNeed(tableName, condisions, dataset, schema)

If the target can't find, insert a new record.

omysqlInst.beginTransaction(taskCoreFn)

Mysql transaction. Wrap the taskCoreFn(connection, {sqlBuilder}) with transaction.

Return true if 'COMMIT'

The taskCoreFn return true to commit, or false to rollback. If you want to control by your self, return undefined.

Schema

Used to inset/modify data.

For example: usr_table

usr|create_time|update_item ---|---|--- usr1|2019-01-01 01:00|2019-01-01 01:00

const moment = require('moment');

const getCurrentTimestamp = () => {
    return moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
};

const USR_TABLE_SCHEMA = {
    // key name
    create_time: {
        // Can't be modified.
        frozen: true,
        // Generate default value.
        createDef: getCurrentTimestamp
    },
    update_time: {
        createDef: getCurrentTimestamp
    }
};

// insert data(Time: 2019-02-25 02:25)
await omysqlInst.insert(
    'usr_table',
    {usr: 'ocean'},
    USR_TABLE_SCHEMA
);

// update data(Time: 2019-06-08 06:08)
await omysqlInst.update(
    'usr_table',
    // condition for target.
    {usr: 'usr1'},
    // new data
    {usr: 'bottle'},
    USR_TABLE_SCHEMA
);

The latest data.

usr|create_time|update_item ---|---|--- bottle|2019-01-01 01:00|2019-06-08 06:08 ocean|2019-02-25 02:25|2019-02-25 02:25