Skip to content

Commit a08450a

Browse files
committed
🎉 Initial release of the package
0 parents  commit a08450a

File tree

9 files changed

+205
-0
lines changed

9 files changed

+205
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true

.github/workflows/deploy.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Release
2+
on:
3+
release:
4+
types: [created]
5+
jobs:
6+
release:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout 🛎️
10+
uses: actions/checkout@v2
11+
# Setup .npmrc file to publish to npm
12+
- uses: actions/setup-node@v1
13+
with:
14+
node-version: '12.x'
15+
registry-url: 'https://registry.npmjs.org'
16+
# Defaults to the user or organization that owns the workflow file
17+
scope: '@octocat'
18+
- name: Install ⚙️
19+
run: yarn install
20+
- name: Build 🔧
21+
run: yarn build
22+
- name: Deploy 🚀
23+
run: yarn publish
24+
env:
25+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
lib/
3+
yarn.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Matiss Janis Aboltins
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# knex-serverless-mysql
2+
3+
[![npm](https://img.shields.io/npm/v/knex-serverless-mysql.svg)](https://www.npmjs.com/package/knex-serverless-mysql)
4+
[![npm](https://img.shields.io/npm/l/knex-serverless-mysql.svg)](https://www.npmjs.com/package/knex-serverless-mysql)
5+
6+
Minimalistic knex.js dialect for [serverless-mysql].
7+
8+
## Motivation
9+
10+
[serverless-mysql] persists database connections across multiple
11+
AWS Lambda function execution contexts. This reduces the load on
12+
the database. However, this client is not natively supported by
13+
Knex. This library solves the problem.
14+
15+
## Simple Example
16+
17+
```js
18+
const Knex = require('knex');
19+
const knexServerlessMysql = require('knex-serverless-mysql');
20+
21+
const mysql = require('serverless-mysql')({
22+
config: {
23+
host : process.env.DB_HOST,
24+
database : process.env.DB_DATABASE,
25+
user : process.env.DB_USERNAME,
26+
password : process.env.DB_PASSWORD,
27+
},
28+
});
29+
30+
const knex = Knex({
31+
client: knexServerlessMysql,
32+
mysql,
33+
});
34+
35+
exports.run = function () {
36+
return knex('table_name').where('id', 1);
37+
}
38+
```
39+
40+
## Usage with [datasource-sql]
41+
42+
```js
43+
const Knex = require('knex');
44+
const knexServerlessMysql = require('knex-serverless-mysql');
45+
const { SQLDataSource } = require('datasource-sql');
46+
47+
const mysql = require('serverless-mysql')({
48+
config: {
49+
host : process.env.DB_HOST,
50+
database : process.env.DB_DATABASE,
51+
user : process.env.DB_USERNAME,
52+
password : process.env.DB_PASSWORD,
53+
},
54+
});
55+
56+
const knex = Knex({
57+
client: knexServerlessMysql,
58+
mysql,
59+
});
60+
61+
class TableNameDataSource extends SQLDataSource {
62+
getRowById(id) {
63+
return this.knex('table_name').where('id', id);
64+
}
65+
}
66+
67+
const dataSource = new TableNameDataSource(knex);
68+
69+
exports.run = function () {
70+
return dataSource.getRowById(1);
71+
}
72+
```
73+
74+
## Installation
75+
76+
```
77+
yarn add knex-serverless-mysql
78+
79+
# or
80+
81+
npm install knex-serverless-mysql
82+
```
83+
84+
[serverless-mysql]: https://github.com/jeremydaly/serverless-mysql
85+
[datasource-sql]: https://github.com/cvburgess/SQLDataSource

babel.config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/env",
5+
{
6+
"targets": {
7+
"node": "10"
8+
}
9+
}
10+
]
11+
]
12+
}

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/client').default;

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "knex-serverless-mysql",
3+
"version": "1.0.0",
4+
"description": "Knex.js dialect for serverless-mysql",
5+
"repository": "git@github.com:MatissJanis/knex-serverless-mysql.git",
6+
"author": "Matiss Janis Aboltins <matiss@mja.lv>",
7+
"license": "MIT",
8+
"main": "index.js",
9+
"scripts": {
10+
"build": "babel -D src/ --out-dir lib/"
11+
},
12+
"devDependencies": {
13+
"@babel/cli": "^7.13.16",
14+
"@babel/core": "^7.13.16",
15+
"@babel/preset-env": "^7.13.15"
16+
},
17+
"peerDependencies": {
18+
"knex": ">=0.11.4"
19+
},
20+
"dependencies": {
21+
"knex": "^0.95.4"
22+
}
23+
}

src/client.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import MysqlClient from 'knex/lib/dialects/mysql';
2+
3+
export default class ServerlessMysqlClient extends MysqlClient {
4+
constructor(config) {
5+
super(config);
6+
7+
this.mysql = config.mysql;
8+
}
9+
10+
get dialect() {
11+
return 'serverlessMysql';
12+
}
13+
get driverName() {
14+
return 'serverlessMysql';
15+
}
16+
17+
acquireConnection() {
18+
return Promise.resolve(this.mysql);
19+
}
20+
21+
releaseConnection() {
22+
return this.mysql.end();
23+
}
24+
}

0 commit comments

Comments
 (0)