Skip to content

Commit

Permalink
perf: remove dbManager middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurarpit Singh committed Dec 13, 2019
1 parent 846d48b commit a50eff1
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 423 deletions.
80 changes: 0 additions & 80 deletions docs/middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

- [cache](#cache)
- [cors](#cors)
- [dbManager](#dbManager)
- [doNotWaitForEmptyEventLoop](#donotwaitforemptyeventloop)
- [functionShield](#functionshield)
- [httpContentNegotiation](#httpcontentnegotiation)
Expand Down Expand Up @@ -135,85 +134,6 @@ handler({}, {}, (_, response) => {
})
```

## [dbManager](/src/middlewares/dbManager.js)

dbManager provides seamless connection with database of your choice. By default it uses knex.js but you can use any tool that you want.

After initialization your database connection is accessible under:
```javascript
middy((event, context) => {
const { db } = context;
});
```

Mind that if you use knex you will also need driver of your choice ([check docs](http://knexjs.org/#Installation-node)), for PostgreSQL that would be:
```
yarn add pg
// or
npm install pg
```

### Options

- `config`: configuration object passed as is to client (knex.js by default), for more details check [knex documentation](http://knexjs.org/#Installation-client)
- `client` (optional): client that you want to use when connecting to database of your choice. By default knex.js is used but as long as your client is run as `client(config)` or you create wrapper to conform, you can use other tools. Due to node6 support in middy, knex is capped at version `0.17.3`. If you wish to use newer features, provide your own knex client here.
- `secretsPath` (optional): if for any reason you want to pass credentials using context, pass path to secrets laying in context object - good example is combining this middleware with [ssm](#ssm)
- `removeSecrets` (optional): By default is true. Works only in combination with `secretsPath`. Removes sensitive data from context once client is initialized.
- `forceNewConnection` (optional): Creates new connection on every run and destroys it after. Database client needs to have `destroy` function in order to properly clean up connections.

### Sample usage

Minimal configuration

```javascript
const handler = middy(async (event, context) => {
const { db } = context;
const records = await db.select('*').from('my_table');
console.log(records);
});

handler.use(dbManager({
config: {
client: 'pg',
connection: {
host: '127.0.0.1',
user: 'your_database_user',
password: 'your_database_password',
database: 'myapp_test'
}
},
}));
```

Credentials as secrets object

```javascript
const handler = middy(async (event, context) => {
const { db } = context;
const records = await db.select('*').from('my_table');
console.log(records);
});

handler.use(secretsManager({
secrets: {
[secretsField]: 'my_db_credentials' // { user: 'your_database_user', password: 'your_database_password' }
},
throwOnFailedCall: true
}));

handler.use(dbManager({
config: {
client: 'pg',
connection: {
host : '127.0.0.1',
database : 'myapp_test'
}
},
secretsPath: secretsField
}));
```


## [doNotWaitForEmptyEventLoop](/src/middlewares/doNotWaitForEmptyEventLoop.js)

Sets `context.callbackWaitsForEmptyEventLoop` property to `false`.
Expand Down
10 changes: 0 additions & 10 deletions middlewares.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SSM } from 'aws-sdk'
import { Options as AjvOptions } from 'ajv'
import { HttpError } from 'http-errors'
import Knex from 'knex'
import middy from './'

interface ICorsOptions {
Expand Down Expand Up @@ -116,17 +115,8 @@ interface IHTTPSecurityHeadersOptions {
xssFilter?: Object
}

interface IDbManagerOptions {
client?: Knex | Function,
config: Knex.Config | Object,
forceNewConnection?: boolean,
secretsPath?: string,
removeSecrets?: boolean
}

declare const cache: middy.Middleware<ICacheOptions>;
declare const cors: middy.Middleware<ICorsOptions>;
declare const dbManager: middy.Middleware<IDbManagerOptions>;
declare const doNotWaitForEmptyEventLoop: middy.Middleware<IDoNotWaitForEmtpyEventLoopOptions>;
declare const httpContentNegotiation: middy.Middleware<IHTTPContentNegotiationOptions>;
declare const httpErrorHandler: middy.Middleware<IHTTPErrorHandlerOptions>;
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"content-type": "^1.0.4",
"http-errors": "^1.7.1",
"json-mask": "^0.3.8",
"knex": "^0.17.6",
"negotiator": "^0.6.1",
"once": "^1.4.0",
"qs": "^6.6.0",
Expand Down

0 comments on commit a50eff1

Please sign in to comment.