-
Notifications
You must be signed in to change notification settings - Fork 11
PostgreSQL node example #26
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4c2738a
feat: add handler and helper function
39ae2bb
feat: add package json description
a9af24b
feat: add serverless config
6b9e470
doc: add readme
70109d9
doc: update readme
922d8bb
doc: update project readme
7499c47
fix: change var to const
redanrd 872d502
fix: update sql query example
edc6fbb
fix: pass error directly to callback
redanrd eec3bd2
fix: change function namespace
redanrd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Interact with a PostgreSQL database using functions | ||
|
||
## Requirements | ||
|
||
This example assumes that you are familiar with some products of Scaleway's ecosystem: | ||
* how serverless functions work. If needed, you can check [Scaleway official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/). | ||
* how a managed database for PostgreSQL works, and especially how to create a database and create users with appropriate permissions. Please refer to scaleway's documentation [here](https://www.scaleway.com/en/docs/managed-databases/postgresql-and-mysql/quickstart/). | ||
|
||
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the [Scaleway Serverless Framework Plugin](https://github.com/scaleway/serverless-scaleway-functions) before trying out the example. | ||
|
||
|
||
## Context | ||
|
||
This example shows how to connect to a managed PostgreSQL database and perform a query on it. This example can be extended to adding, deleting, or processing data within a database. | ||
|
||
|
||
## Description | ||
|
||
The function connects to a PostgreSQL database and performs an example query on it. This example uses Node 18 runtime. Used packages are specified in `package.json`. | ||
|
||
## Setup | ||
|
||
### Create a managed PostgreSQL database | ||
|
||
Create a PostgreSQL database and a user profile with appropriate access permissions. | ||
|
||
### Fill environment variables | ||
|
||
Fill your secrets within `serverless.yml` file: | ||
|
||
``` | ||
secret: | ||
PG_HOST: "your host IP address" | ||
PG_USER: "your database username" | ||
PG_DATABASE: "your database name" | ||
PG_PASSWORD: "your databse user password" | ||
PG_PORT: "your database port" | ||
``` | ||
|
||
### Install npm modules | ||
|
||
Once your environment is set up, you can install `npm` dependencies from `package.json` file using: | ||
|
||
``` | ||
npm install | ||
``` | ||
|
||
### Deploy and call the function | ||
|
||
Then deploy your function and get its URL using: | ||
|
||
``` | ||
serverless deploy | ||
``` | ||
|
||
From the given function URL, you can run your function using: | ||
|
||
``` | ||
curl <function URL> | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const pg = require('pg') | ||
|
||
const PG_HOST=process.env.PG_HOST; | ||
const PG_USER=process.env.PG_USER; | ||
const PG_DATABASE=process.env.PG_DATABASE; | ||
const PG_PASSWORD=process.env.PG_PASSWORD; | ||
const PG_PORT=parseInt(process.env.PG_PORT,10); | ||
|
||
const pool = new pg.Pool({ | ||
user: PG_USER, | ||
host: PG_HOST, | ||
database: PG_DATABASE, | ||
password: PG_PASSWORD, | ||
port: PG_PORT | ||
}) | ||
|
||
exports.handle = async (event, context, callback) => { | ||
try { | ||
const { rows } = await query("SELECT * FROM table LIMIT 10") | ||
console.log(JSON.stringify(rows[0])) | ||
const response = { | ||
"statusCode": 200, | ||
"headers": { | ||
"Content-Type" : "application/json" | ||
}, | ||
"body": JSON.stringify(rows), | ||
"isBase64Encoded": false | ||
}; | ||
callback(null, response); | ||
} catch (err) { | ||
console.log('Database ' + err) | ||
callback(err); | ||
} | ||
}; | ||
|
||
async function query (q) { | ||
const client = await pool.connect() | ||
let res | ||
try { | ||
await client.query('BEGIN') | ||
try { | ||
res = await client.query(q) | ||
await client.query('COMMIT') | ||
} catch (err) { | ||
await client.query('ROLLBACK') | ||
throw err | ||
} | ||
} finally { | ||
client.release() | ||
} | ||
|
||
return res | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "postgreSQL-node-example", | ||
"version": "1.0.0", | ||
"main": "handler.js", | ||
"type": "commonjs", | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"pg": "^8.10.0" | ||
}, | ||
"devDependencies": { | ||
"serverless-scaleway-functions": "^0.4.6" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
service: scaleway-node18 | ||
configValidationMode: off | ||
singleSource: false | ||
provider: | ||
name: scaleway | ||
runtime: node18 | ||
|
||
secret: | ||
PG_HOST: "your host IP address" | ||
PG_USER: "your database username" | ||
PG_DATABASE: "your database name" | ||
PG_PASSWORD: "your databse user password" | ||
PG_PORT: "your database port" | ||
|
||
plugins: | ||
- serverless-scaleway-functions | ||
|
||
package: | ||
patterns: | ||
- '!.gitignore' | ||
- '!.git/**' | ||
|
||
functions: | ||
get-all-from-table | ||
handler: handler.handle |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.