# lia-mysql

> JavaScript library of data standards.

Latest version **1.1.0** (published 2025-01-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install lia-mysql
pnpm add lia-mysql
yarn add lia-mysql
bun add lia-mysql
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2025-01-09 |
| First published | 2023-06-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 72.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | sunlley |
| Keywords | mysql, lia, core |

## Links

- npm: https://www.npmjs.com/package/lia-mysql
- npm.io page: https://npm.io/package/lia-mysql

## Dependencies (4)

- [events](https://npm.io/package/events.md) ^3.3.0
- [mysql2](https://npm.io/package/mysql2.md) ^3.3.0
- [sqlstring](https://npm.io/package/sqlstring.md) ^2.3.3
- [@types/sqlstring](https://npm.io/package/@types/sqlstring.md) ^2.3.2

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2025-01-09
- 1.0.9 — 2025-01-09
- 1.0.8 — 2025-01-09
- 1.0.7 — 2025-01-09
- 1.0.6 — 2025-01-09
- 1.0.5 — 2025-01-09
- 1.0.4 — 2025-01-09
- 1.0.3 — 2025-01-08
- 1.0.2 — 2024-09-02
- 1.0.1 — 2023-11-13
- 1.0.0 — 2023-06-06

## README

# Mysql Installer
Simplify the use of MySQL

![](https://img.shields.io/badge/version-1.0.2-lightgrey)
![](https://img.shields.io/badge/node-18.%2B-brightgreen)

## Installer
Using npm:
```shell
$ npm install lia-mysql
```

## How to Use MysqlInstaller
### 1.1 Mysql Initial (single mode)
```typescript
import {MysqlInstaller,Config} from "core-mysql";
const TARGET = {SQL: {}};
const config = {
    host: "127.0.0.1",
    port: 3306,
    user: "root",
    password: "root12345",
    database: "test"
}
const installer = new MysqlInstaller(config as Config,TARGET,'sys|info');
await installer.load();
```
### 1.2 Mysql Use (single mode)
#### query
```typescript
const result = await TARGET.SQL.query('select * from users where id=?', [100])
console.log(result);

```

#### Select
```typescript
const rows = await db.select('table-name', {
  where: {
    type: 'javascript'
  },
  columns: ['author', 'title'],
  orders: [['id', 'desc']]
});
console.log(rows);

=> SELECT `author`, `title` FROM `table-name`
WHERE `type` = 'javascript' ORDER BY `id` DESC

```

#### Insert
```typescript
const row = {
  name: 'fengmk2',
  otherField: 'other field value',
};
const result = await TARGET.SQL.insert('table-name', row);
console.log(result);

```
- Insert multi rows

```typescript
const rows = [{
    name: 'fengmk2',
    otherField: 'other field value',
}, {
    name: 'fengmk3',
    otherField: 'other field value',
}];
const result = await TARGET.SQL.insert('table-name', rows);
console.log(result);

```

#### Update

- Update a row with primary key: `id`

```typescript
const row = {
  id: 123,
  name: 'fengmk2',
  otherField: 'other field value',
};
const result = await TARGET.SQL.update('table-name', row);
console.log(result);
```
- Update a row with `options.where` and `options.columns`

```typescript
const row = {
  name: 'fengmk2',
  otherField: 'other field value',
};
const result = await TARGET.SQL.update('table-name', row, {
  where: { name: row.name },
  columns: [ 'otherField' ]
});
console.log(result);
```

#### Update multiple rows

- Update multiple rows with primary key: `id`

```typescript
const options = [{
  id: 123,
  name: 'fengmk2',
  email: 'm@fengmk2.com',
  otherField: 'other field value',
}, {
   id: 124,
  name: 'fengmk2_2',
  email: 'm@fengmk2_2.com',
  otherField: 'other field value 2',
}]
const result = await TARGET.SQL.updateRows('table-name', options);
console.log(result);
```

- Update multiple rows with `row` and `where` properties

```typescript
const options = [{
  row: {
    email: 'm@fengmk2.com',
    otherField: 'other field value',
  },
  where: {
    id: 123,
    name: 'fengmk2',
  }
}, {
  row: {
    email: 'm@fengmk2_2.com',
    otherField: 'other field value2',
  }, 
  where: {
    id: 124,
    name: 'fengmk2_2',
  }
}]
const result = await TARGET.SQL.updateRows('table-name', options);
console.log(result);
```

#### Get

- Get a row

```typescript
const row = await TARGET.SQL.get('table-name', { name: 'fengmk2' });

=> SELECT * FROM `table-name` WHERE `name` = 'fengmk2'
```


### 2.1 Mysql Initial (multi mode)

```typescript
import {MysqlInstaller,Config,MysqlConfigs} from "core-installer";
const TARGET = {SQL: {}};
const config = {
    APP1:{
        host: "127.0.0.1",
        port: 3306,
        user: "root",
        password: "root12345",
        database: "test"
    }
}
const installer = new MysqlInstaller(config as MysqlConfigs<Config>,TARGET,'sys|info');
await installer.load();
```

### 2.2 Mysql Use (multi mode)
The multi-instance mode is basically the same as the single-instance mode, except that the database instance needs to be specified on the SQL instance, for example:
#### query
```typescript
const result = await TARGET.SQLS.APP1.query('select * from users where id=?', [100])
console.log(result);

```

For other operations, please refer to the single mode

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