knex-mysql-middleware

Set a MySQL flavored Knex connection that can be accessed via Koa or Express middleware.

Usage no npm install needed!

<script type="module">
  import knexMysqlMiddleware from 'https://cdn.skypack.dev/knex-mysql-middleware';
</script>

README

knex-mysql-middleware

Build Status NPM version NPM downloads LICENSE

Set a MySQL flavored Knex connection that can be accessed via Koa or Express middleware.

$ npm install knex-mysql-middleware

Usage

Quick Start (koa or express)
// koa
var knex = require('knex-mysql-middleware');
var app = require('koa')();
app.use(knex());

app.use(function *() {
  this.status = (yield this.knex('resource').where('id', this.query.id)) ? 204 : 404;
});

// express
var knex = require('knex-mysql-middleware/express');
var app = require('express')();
app.use(knex());

app.use(function () {
  this.status = this.knex('resource').where('id', req.query.id) ? 204 : 404;
});

Options

Auto-initialize via environment variables (recommended)

It is recommended that you set the parameters via environment variables and utilize a .env file via a library such as envc, dotenv, or an equivalent.

DATABASE_NAME=demo
DATABASE_USER=scott
DATABASE_PASS=tiger
DATABASE_HOST=127.0.0.1
DATABASE_PORT=3306

# optional
DATABASE_SSL=true
DATABASE_DEBUG=true
DATABASE_POOL_MIN=0
DATABASE_POOL_MAX=7
Configuration via options object
var config = {
  connection: {
    host     : '127.0.0.1',
    user     : 'scott',
    password : 'tiger',
    database : 'demo'
  },
  // optional
  debug: true,
  pool: {
    min: 0,
    max: 7
  },
  ssl: true
}

app.use(knex(config));

Alternatives

License

MIT