Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/database raw #8

Merged
merged 3 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/environments/.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ AWS_LAMBDA_MEMORY_SIZE=128

AWS_LOG_RETENTION_DAYS=7

DB_HOST=127.0.0.1
DB_HOST_WRITE=127.0.0.1
DB_HOST_READ=127.0.0.2
DB_PORT=3306
DB_USER="app_user"
DB_PASSWORD="app_password"
DB_NAME="app_db"

AWS_APIGATEWAY_SECRET_KEY="d41d8cd98f00b204e9800998ecf8427e"
AWS_APIGATEWAY_COMPRESSION_MAX_BYTES=100

Expand Down
8 changes: 8 additions & 0 deletions config/environments/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ AWS_LAMBDA_MEMORY_SIZE=128

AWS_LOG_RETENTION_DAYS=7

DB_HOST=127.0.0.1
DB_HOST_WRITE=127.0.0.1
DB_HOST_READ=127.0.0.2
DB_PORT=3306
DB_USER="app_user"
DB_PASSWORD="app_password"
DB_NAME="app_db"

AWS_APIGATEWAY_SECRET_KEY="d41d8cd98f00b204e9800998ecf8427e"
AWS_APIGATEWAY_COMPRESSION_MAX_BYTES=100

Expand Down
8 changes: 8 additions & 0 deletions config/environments/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ AWS_LAMBDA_MEMORY_SIZE=128

AWS_LOG_RETENTION_DAYS=7

DB_HOST=127.0.0.1
DB_HOST_WRITE=127.0.0.1
DB_HOST_READ=127.0.0.2
DB_PORT=3306
DB_USER="app_user"
DB_PASSWORD="app_password"
DB_NAME="app_db"

AWS_APIGATEWAY_SECRET_KEY="d41d8cd98f00b204e9800998ecf8427e"
AWS_APIGATEWAY_COMPRESSION_MAX_BYTES=100

Expand Down
8 changes: 8 additions & 0 deletions config/environments/.env.staging
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ AWS_LAMBDA_MEMORY_SIZE=128

AWS_LOG_RETENTION_DAYS=7

DB_HOST=127.0.0.1
DB_HOST_WRITE=127.0.0.1
DB_HOST_READ=127.0.0.2
DB_PORT=3306
DB_USER="app_user"
DB_PASSWORD="app_password"
DB_NAME="app_db"

AWS_APIGATEWAY_SECRET_KEY="d41d8cd98f00b204e9800998ecf8427e"
AWS_APIGATEWAY_COMPRESSION_MAX_BYTES=100

Expand Down
150 changes: 81 additions & 69 deletions config/functions/samples.yml
Original file line number Diff line number Diff line change
@@ -1,78 +1,90 @@
sampleErrorHandling:
handler: ${self:custom.path.app}/handlers/samples/errorHandling.handler
description: Error Handling sample test
reservedConcurrency: 1
events:
- http:
path: samples/error-handling
method: get
private: false
cors: ${self:custom.http.cors}
# sampleErrorHandling:
# handler: ${self:custom.path.app}/handlers/samples/errorHandling.handler
# description: Error Handling sample test
# reservedConcurrency: 1
# events:
# - http:
# path: samples/error-handling
# method: get
# private: false
# cors: ${self:custom.http.cors}

# sampleJwt:
# handler: ${self:custom.path.app}/handlers/samples/jwt.handler
# description: Sample jwt test
# timeout: 5
# reservedConcurrency: 1
# events:
# - http:
# path: samples/jwt
# method: get
# private: false
# cors: ${self:custom.http.cors}

# sampleObjectStore:
# handler: ${self:custom.path.app}/handlers/samples/objectStore.handler
# description: S3 Sample
# reservedConcurrency: 1
# events:
# - http:
# path: samples/object-store
# method: get
# private: false
# cors: ${self:custom.http.cors}

sampleJwt:
handler: ${self:custom.path.app}/handlers/samples/jwt.handler
description: Sample jwt test
timeout: 5
reservedConcurrency: 1
events:
- http:
path: samples/jwt
method: get
private: false
cors: ${self:custom.http.cors}
# sampleCache:
# handler: ${self:custom.path.app}/handlers/samples/cache.handler
# description: Cache sample test
# timeout: 5
# reservedConcurrency: 1
# events:
# - http:
# path: samples/cache
# method: get
# private: false
# cors: ${self:custom.http.cors}

sampleObjectStore:
handler: ${self:custom.path.app}/handlers/samples/objectStore.handler
description: S3 Sample
reservedConcurrency: 1
events:
- http:
path: samples/object-store
method: get
private: false
cors: ${self:custom.http.cors}
# sampleQueue:
# handler: ${self:custom.path.app}/handlers/samples/queue.handler
# description: Queue sample test
# memorySize: 192
# reservedConcurrency: 1
# events:
# - http:
# path: samples/queue
# method: get
# private: true
# cors: ${self:custom.http.cors}

sampleCache:
handler: ${self:custom.path.app}/handlers/samples/cache.handler
description: Cache sample test
timeout: 5
reservedConcurrency: 1
events:
- http:
path: samples/cache
method: get
private: false
cors: ${self:custom.http.cors}
# sampleQueueProcessor:
# handler: ${self:custom.path.app}/handlers/samples/queueProcessor.handler
# description: Process sample queued test
# memorySize: 128
# reservedConcurrency: 3
# events:
# - sqs: arn:aws:sqs:${self:provider.region}:${env:AWS_ACCOUNT_ID}:${self:provider.stackName}-sampleQueue

sampleQueue:
handler: ${self:custom.path.app}/handlers/samples/queue.handler
description: Queue sample test
memorySize: 192
reservedConcurrency: 1
events:
- http:
path: samples/queue
method: get
private: true
cors: ${self:custom.http.cors}
# sampleScheduledEvent:
# handler: ${self:custom.path.app}/handlers/samples/scheduledEvent.handler
# description: Ping event triggered from Scheduled Cloudwatch Event
# events:
# - schedule:
# name: ${self:provider.stackName}-sampleScheduledEvent
# description: Trigger sampleScheduledEvent lambda function every 5 minute
# rate: rate(5 minutes)
# enabled: false

sampleQueueProcessor:
handler: ${self:custom.path.app}/handlers/samples/queueProcessor.handler
description: Process sample queued test
memorySize: 128
reservedConcurrency: 3
events:
- sqs: arn:aws:sqs:${self:provider.region}:${env:AWS_ACCOUNT_ID}:${self:provider.stackName}-sampleQueue
# sampleDb:
# handler: ${self:custom.path.app}/handlers/samples/db.handler
# description: Direct db connection without Knex ORM sample
# reservedConcurrency: 1
# events:
# - http:
# path: samples/db
# method: get
# private: true
# cors: ${self:custom.http.cors}

sampleScheduledEvent:
handler: ${self:custom.path.app}/handlers/samples/scheduledEvent.handler
description: Ping event triggered from Scheduled Cloudwatch Event
events:
- schedule:
name: ${self:provider.stackName}-sampleScheduledEvent
description: Trigger sampleScheduledEvent lambda function every 5 minute
rate: rate(5 minutes)
enabled: false
#
# TODO REVIEW ALL THE SAMPLES BELOW
#
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "lesgo",
"version": "0.3.3",
"version": "0.3.5",
"description": "A lightweight node.js serverless framework",
"scripts": {
"start": "sls offline start --stage local --env local",
"start": "sls offline start --stage local --env local --skipCacheInvalidation",
"server": "sls offline start --stage local --env local",
"build": "lesgo-scripts -t build",
"deploy": "lesgo-scripts -t deploy",
Expand All @@ -22,7 +22,7 @@
"dependencies": {
"@sentry/node": "^5.6.2",
"knex": "^0.20.1",
"lesgo": "^0.3.4",
"lesgo": "^0.3.5",
"middy": "^0.29.0",
"mysql": "^2.17.1",
"objection": "^1.6.11",
Expand Down
2 changes: 1 addition & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ custom:
# Add all the functions here
functions:
- ${file(${self:custom.path.functions}/ping.yml)}
#- ${file(${self:custom.path.functions}/samples.yml)}
- ${file(${self:custom.path.functions}/samples.yml)}

# Add all the additional resources here
resources:
Expand Down
37 changes: 37 additions & 0 deletions src/config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export default {
/*
*--------------------------------------------------------------------------
* Default Database Connection Name
*--------------------------------------------------------------------------
*
* Here you may specify which of the database connections below you wish
* to use as your default connection for all database work.
*/

default: process.env.DB_CONNECTION || 'mysql',

/*
*--------------------------------------------------------------------------
* Database Connections
*--------------------------------------------------------------------------
*
* Here are each of the database connections setup for your application.
* Of course, examples of configuring each database platform that is
* supported by Lesgo is shown below to make development simple.
*/

connections: {
mysql: {
connection: {
host: process.env.DB_HOST_WRITE || process.env.DB_HOST || '127.0.0.1',
host_read:
process.env.DB_HOST_READ || process.env.DB_HOST || '127.0.0.1',
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME || 'myapp_test',
charset: 'utf8',
},
},
},
};
26 changes: 26 additions & 0 deletions src/handlers/samples/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable no-console */
import middy from 'middy';
import httpMiddleware from 'Middlewares/httpMiddleware';
import app from 'Config/app';
import { connectSentry } from 'Utils/sentry';
import { db, dbRead, connectDb } from 'Utils/db';

connectDb();
connectSentry();

const originalHandler = async () => {
// Fetch data from READ HOST
const dataRead = await dbRead.query('SELECT * FROM users LIMIT 1');
await dbRead.end();

// Fetch data from WRITE HOST
const dataWrite = await db.query('SELECT * FROM users LIMIT 1');
await db.end();

return [{ dataRead, dataWrite }];
};

// eslint-disable-next-line import/prefer-default-export
export const handler = middy(originalHandler);

handler.use(httpMiddleware({ debugMode: app.debug }));
25 changes: 21 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,13 @@
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==

"@types/mysql@^2.15.6":
version "2.15.8"
resolved "https://registry.yarnpkg.com/@types/mysql/-/mysql-2.15.8.tgz#305172ee6ec530acd16a713f680dbfb4399960a8"
integrity sha512-l0TUdg6KDEaLO75/yjdjksobJDRWv8iZlpRfv/WW1lQZCQDKdTDnKCkeH10oapzP/JTuKiTy6Cvq/sm/0GgcUw==
dependencies:
"@types/node" "*"

"@types/node@*":
version "12.7.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.5.tgz#e19436e7f8e9b4601005d73673b6dc4784ffcc2f"
Expand Down Expand Up @@ -5430,16 +5437,17 @@ left-pad@^1.3.0:
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==

lesgo@^0.3.4:
version "0.3.4"
resolved "https://registry.yarnpkg.com/lesgo/-/lesgo-0.3.4.tgz#2357a843ff511ec49d1afdf8be7e0b8b5f009dd7"
integrity sha512-DxELWLLR9116T5XmKaryXtnyYHrbvBHD7zJs6aOvrsn4jchSJna8pnJqyyhAJHBnjJyNHzQElMaymuaUAH70ig==
lesgo@^0.3.5:
version "0.3.5"
resolved "https://registry.yarnpkg.com/lesgo/-/lesgo-0.3.5.tgz#aa7fffbe89bd2fe5aadcc454159d0c02142e3b19"
integrity sha512-ZEaEJlnl2gT+70Ytnr9D1yTeKcNoEgCNWmAjKM0OyZZo1XephV340uZyBBAU48bPIsGGWOFJOhPv8R0OgKv0JA==
dependencies:
"@elastic/elasticsearch" "^7.4.0"
"@sentry/minimal" "^5.6.1"
jsonwebtoken "^8.5.1"
knex "^0.20.1"
memcached-elasticache "^1.1.1"
serverless-mysql "^1.5.2"

leven@^3.1.0:
version "3.1.0"
Expand Down Expand Up @@ -7525,6 +7533,15 @@ serverless-dotenv-plugin@^2.1.1:
dotenv "^4.0.0"
dotenv-expand "^4.0.1"

serverless-mysql@^1.5.2:
version "1.5.2"
resolved "https://registry.yarnpkg.com/serverless-mysql/-/serverless-mysql-1.5.2.tgz#69f4cd058511089a15f07d383b8d34bf48d883d4"
integrity sha512-r06MzaT/OShCP8rem5SIm6OdmQ0+L7vGEjDpWyjAlsvCAKoxrC+aKlyzfEIzbeqAdfPzTKNA5NnWiI7MlX6RSA==
dependencies:
mysql "^2.17.1"
optionalDependencies:
"@types/mysql" "^2.15.6"

serverless-offline@^5.10.1:
version "5.10.1"
resolved "https://registry.yarnpkg.com/serverless-offline/-/serverless-offline-5.10.1.tgz#983c517acd078a4dc7374dcad4463c6cc89d563f"
Expand Down