@nuskin/connection-lifeguard
v1.2.1
Published
ConnectionLifeguard...
Downloads
23
Keywords
Readme
ConnectionLifeguard NPM Library
Overview
ConnectionLifeguard is used to determine when a client is allowed to obtain a connection with a database.
This library is used in conjunction with the oraclelib
layer.
See also ABOUT
Configuring your lambda's serverless.yml for use with ConnectionLifeguard
Before you can make use of ConnectionLifeguard, you'll need to give your lambda appropriate permission.
provider:
environment:
ACCOUNT_ID: '#{AWS::AccountId}'
TABLE_COUNTER: ConnectionLifeguardCounter
TABLE_UUID: ConnectionLifeguardUuid
QNAME: ConnectionLifeguard
iamRoleStatements:
- Effect: Allow
Action: # Gives permission to DynamoDB tables in a specific region
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.TABLE_COUNTER}*"
- Effect: Allow
Action: # Gives permission to DynamoDB tables in a specific region
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.TABLE_UUID}*"
- Effect: "Allow"
Action:
- "sqs:SendMessage"
- "sqs:GetQueueUrl"
Resource: "arn:aws:sqs:${self:provider.region}:*:${self:provider.environment.QNAME}*"
Example Usage
const {requestApproval, done} = require('@nuskin/connection-lifeguard')
const main = async () => {
const pool = 'poolName'
let uuid
let approved
try {
let count = 0
do {
count++
const response = await requestApproval(pool)
approved = response.approved
if(approved){
uuid = response.uuid
// you're allowed to get a db connection now.
// do your processing here onse.data.uuid
}
} while(!approved && count<4) // attempt to get approval 4 times
} catch(error) {
console.error("We got an error: ",error)
} finally {
// Close your connection with the database here.
// Tell the connection lifeguard you have closed your connection.
await done(pool, uuid)
}
}
main()
Example Response
{
"approved": true,
"count": 7,
"uuid": "N3ZCSMAUs2DGs1PA2ItxxX"
}