# omysql

> Easy way to use mysql

Latest version **0.0.13** (published 2019-11-18) · 0 weekly downloads

## Install

```sh
npm install omysql
pnpm add omysql
yarn add omysql
bun add omysql
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.13 |
| Published | 2019-11-18 |
| First published | 2019-05-24 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 28.2 KB |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | BottleLiu |
| Maintainers | bottleliu, chenmnkken, lijiafang |
| Keywords | node, mysql, omysql |

## Links

- npm: https://www.npmjs.com/package/omysql
- Repository: https://github.com/jsCONFIG/omysql
- Homepage: https://github.com/jsCONFIG/omysql#readme
- Issues: https://github.com/jsCONFIG/omysql/issues
- npm.io page: https://npm.io/package/omysql

## Dependencies (1)

- [mysql2](https://npm.io/package/mysql2.md) 2.0.0

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 0.0.13 (latest) — 2019-11-18
- 0.0.12 — 2019-11-14
- 0.0.11 — 2019-10-30
- 0.0.10 — 2019-10-30
- 0.0.9 — 2019-06-11
- 0.0.8 — 2019-06-10
- 0.0.7 — 2019-06-06
- 0.0.6 — 2019-06-06
- 0.0.5 — 2019-06-05
- 0.0.4 — 2019-06-05
- 0.0.3 — 2019-06-04
- 0.0.2 — 2019-05-30
- 0.0.1 — 2019-05-24

## README

# omysql

[![defaultExtname](http://img.shields.io/npm/v/omysql.svg)](https://www.npmjs.org/package/omysql)
[![defaultExtname](http://img.shields.io/npm/dm/omysql.svg)](https://www.npmjs.org/package/omysql)

Easy way to use mysql

## How to use.

```powershell
# Install
npm install omysql
```

```javascript
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

```javascript
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)

```javascript
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` = ? and `age` = ?`, 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

```javascript
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

---
_Source: https://npm.io/package/omysql · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
