Skip to content

Latest commit

 

History

History
94 lines (68 loc) · 2.06 KB

README.md

File metadata and controls

94 lines (68 loc) · 2.06 KB

knex-serverless-mysql

npm npm

Minimalistic knex.js dialect for serverless-mysql.

Motivation

serverless-mysql persists database connections across multiple AWS Lambda function execution contexts. This reduces the load on the database. However, this client is not natively supported by Knex. This library solves the problem.

Getting Started

Installation

yarn add knex-serverless-mysql

# or

npm install knex-serverless-mysql

Simple Example

const Knex = require('knex');
const knexServerlessMysql = require('knex-serverless-mysql');

const mysql = require('serverless-mysql')({
  config: {
    host     : process.env.DB_HOST,
    database : process.env.DB_DATABASE,
    user     : process.env.DB_USERNAME,
    password : process.env.DB_PASSWORD,
  },
});

const knex = Knex({
  client: knexServerlessMysql,
  mysql,
});

exports.run = function () {
  return knex('table_name').where('id', 1);
}

Usage with datasource-sql

const Knex = require('knex');
const knexServerlessMysql = require('knex-serverless-mysql');
const { SQLDataSource } = require('datasource-sql');

const mysql = require('serverless-mysql')({
  config: {
    host     : process.env.DB_HOST,
    database : process.env.DB_DATABASE,
    user     : process.env.DB_USERNAME,
    password : process.env.DB_PASSWORD,
  },
});

const knex = Knex({
  client: knexServerlessMysql,
  mysql,
});

class TableNameDataSource extends SQLDataSource {
  getRowById(id) {
    return this.knex('table_name').where('id', id);
  }
}

const dataSource = new TableNameDataSource(knex);

exports.run = function () {
  return dataSource.getRowById(1);
}

Contributing

All contributions are welcome!

Please open an issue or pull request.