# mio-mysql

> MySQL storage plugin for Mio.

Latest version **0.9.0** (published 2014-08-24) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.9.0 |
| Published | 2014-08-24 |
| First published | 2013-12-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.10 |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | Alex Mingoia |
| Maintainers | amingoia |
| Keywords | sql, mysql, mio, plugin |

## Links

- npm: https://www.npmjs.com/package/mio-mysql
- Homepage: https://github.com/mio/mysql
- npm.io page: https://npm.io/package/mio-mysql

## Dependencies (6)

- [mio](https://npm.io/package/mio.md) ^0.9.0
- [async](https://npm.io/package/async.md) ^0.9.0
- [debug](https://npm.io/package/debug.md) ^1.0.2
- [lingo](https://npm.io/package/lingo.md) ^0.0.5
- [mysql](https://npm.io/package/mysql.md) ^2.3.2
- [mongo-sql](https://npm.io/package/mongo-sql.md) ^2.5.6

## Recent versions

- 0.9.0 (latest) — 2014-08-24
- 0.8.3 — 2014-06-24
- 0.8.2 — 2014-04-27
- 0.8.1 — 2014-04-22
- 0.7.8 — 2014-02-25
- 0.7.7 — 2014-02-24
- 0.7.6 — 2014-02-21
- 0.7.5 — 2014-02-19
- 0.7.4 — 2014-02-19
- 0.7.3 — 2014-02-18
- 0.7.2 — 2014-02-18
- 0.7.1 — 2014-02-17
- 0.7.0 — 2014-02-17
- 0.6.1 — 2014-02-12
- 0.6.0 — 2014-02-12
- … 12 more at https://npm.io/package/mio-mysql/versions

## README

# mio-mysql

[![Build Status](https://secure.travis-ci.org/mio/mysql.png?branch=master)](http://travis-ci.org/mio/mysql)
[![Coverage Status](https://coveralls.io/repos/mio/mysql/badge.png?branch=master)](https://coveralls.io/r/mio/mysql?branch=master)
[![NPM version](https://badge.fury.io/js/mio-mysql.png)](http://badge.fury.io/js/mio-mysql)
[![Dependency Status](https://david-dm.org/mio/mysql.png)](http://david-dm.org/mio/mysql)

MySQL storage plugin for [Mio][0].

## Installation

```sh
npm install mio-mysql
```

## API

See the [Mio][0] documentation for query methods like find, save, etc.

### exports(settings, options)

Create a new Mio mysql plugin with the given database `settings` and `options`.

`settings` same as settings for
[node-mysql](https://github.com/felixge/node-mysql/)

`options`
* `tableName` The table for this model. Defaults to singularized model name.
* `maxLimit` The maximum number of records to select at once. Default is 200.

```javascript
var User = require('mio').createModel('User');

User.server(require('mio-mysql', {
  database: 'mydb',
  user: 'root'
}));
```

### Queries

The query syntax is a subset of [mongo-sql][1]. The `type`, `columns`,
and `table` properties are handled by mio-mysql.

### Pagination

Both `offset` and `limit`, and `page` and `pageSize` query parameters are
supported.

The collection returned by `Model.findAll()` has pagination properties on the
array:

```javascript
User.findAll()
  .where({ created_at: { $lt: '2014-01-01' }})
  .sort({ created_at: 'desc' })
  .limit(25)
  .exec(function(err, users) {
    console.log(users);          // => [user1, user2, user3, ...]
    console.log(users.total);    // => 73
    console.log(users.pages);    // => 3
    console.log(users.page);     // => 1
    console.log(users.pageSize); // => 25
    console.log(users.offset);   // => 0
    console.log(users.limit);    // => 25
});
```

### Custom table names

Custom table names are specified using the `tableName` option. For example:

```javascript
User.use(require('mio-mysql')({
  database: 'mydb',
  user: 'root'
}, {
  tableName: 'users'
}));
```

### Custom column names

Custom field names are provided by a `columnName` property in the attribute
definition. For example:

```javascript
User
  .attr('id')
  .attr('firstName', {
    type: 'string',
    length: 255,
    columnName: 'first_name'
  })
  .attr('lastName', {
    type: 'string',
    length: 255,
    columnName: 'last_name'
  });
```

### Date types

Attributes with `type: "date"` will be handled based on the `columnType`
property. This property can either be "datetime", "timestamp", or "integer",
corresponding to MySQL column type. If not specified, mio-mysql will assume
"integer".

### Data formatters

If you need to control exactly how a data-type is determined, set the attribute
definition's `dataFormatter` function:

```javascript
var Event = mio.createModel('Event');

Event.attr('date', {
  dataFormatter: function(value, Event) {
    value = Math.floor(value.getTime() / 1000);
    return value;
  }
});
```

### Query timeout

Queries have a default timeout of `60000` milliseconds. If the query takes
longer, the connection will be destroyed and the callback executed with
`Error("Connection ended due to query time-out.")`.

To change the query timeout, set `settings.queryTimeout` to the desired value.

### Database connection

mio-mysql utilizes node-mysql's connection pool.

Models that share a settings object will share a connection pool, exposed via
`settings.pool`.

```javascript
var mysql = require('mio-mysql');

var settings = {
  database: 'mydb',
  user: 'root'
};

// Both User and Post models will share the same connection.
User.server(mysql(settings));
Post.server(mysql(settings));

console.log(settings.pool);
// => node-mysql connection pool object...
```

### exports.mysql

[MySQL](https://github.com/felixge/node-mysql) module.

## Tests

Tests are written with [mocha](https://github.com/visionmedia/mocha) and
[should](https://github.com/visionmedia/should.js) using BDD-style assertions.

Run the tests with npm:

```sh
npm test
```

## MIT Licensed

[0]: https://github.com/mio/mio/
[1]: https://github.com/goodybag/mongo-sql/

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