Skip to content

Commit 63788e9

Browse files
redanrdthomas-tacquetcyclimse
authored
PostgreSQL node example (#26)
Add a function to perform a query on a PostgreSQL database using Node. --------- Co-authored-by: Thomas TACQUET <thomas@tacquet.net> Co-authored-by: Andy Méry <amery@scaleway.com>
1 parent 330a4c3 commit 63788e9

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Table of Contents:
4242
| **[Redis TLS](functions/redis-tls/README.md)** <br/> How to connect a function to a Scaleway Redis cluster with TLS enabled. | python310 | [Serverless Framework] |
4343
| **[Rust MNIST](functions/rust-mnist/README.md)** <br/> A Rust function to recognize hand-written digits with a simple neural network. | rust165 | [Serverless Framework] |
4444
| **[Terraform Python](functions/terraform-python-example/README.md)** <br/> A Python function deployed with Terraform. | python310 | [Terraform] |
45+
| **[PostgeSQL Node](functions/postgre-sql-node/README.md)** <br/> A Node function to connect and interact with PostgreSQL database. | node18 | [Serverless Framework] |
4546

4647
### 📦 Containers
4748

functions/postgre-sql-node/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Interact with a PostgreSQL database using functions
2+
3+
## Requirements
4+
5+
This example assumes that you are familiar with some products of Scaleway's ecosystem:
6+
* how serverless functions work. If needed, you can check [Scaleway official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/).
7+
* 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/).
8+
9+
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.
10+
11+
12+
## Context
13+
14+
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.
15+
16+
17+
## Description
18+
19+
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`.
20+
21+
## Setup
22+
23+
### Create a managed PostgreSQL database
24+
25+
Create a PostgreSQL database and a user profile with appropriate access permissions.
26+
27+
### Fill environment variables
28+
29+
Fill your secrets within `serverless.yml` file:
30+
31+
```
32+
secret:
33+
PG_HOST: "your host IP address"
34+
PG_USER: "your database username"
35+
PG_DATABASE: "your database name"
36+
PG_PASSWORD: "your databse user password"
37+
PG_PORT: "your database port"
38+
```
39+
40+
### Install npm modules
41+
42+
Once your environment is set up, you can install `npm` dependencies from `package.json` file using:
43+
44+
```
45+
npm install
46+
```
47+
48+
### Deploy and call the function
49+
50+
Then deploy your function and get its URL using:
51+
52+
```
53+
serverless deploy
54+
```
55+
56+
From the given function URL, you can run your function using:
57+
58+
```
59+
curl <function URL>
60+
```

functions/postgre-sql-node/handler.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const pg = require('pg')
2+
3+
const PG_HOST=process.env.PG_HOST;
4+
const PG_USER=process.env.PG_USER;
5+
const PG_DATABASE=process.env.PG_DATABASE;
6+
const PG_PASSWORD=process.env.PG_PASSWORD;
7+
const PG_PORT=parseInt(process.env.PG_PORT,10);
8+
9+
const pool = new pg.Pool({
10+
user: PG_USER,
11+
host: PG_HOST,
12+
database: PG_DATABASE,
13+
password: PG_PASSWORD,
14+
port: PG_PORT
15+
})
16+
17+
exports.handle = async (event, context, callback) => {
18+
try {
19+
const { rows } = await query("SELECT * FROM table LIMIT 10")
20+
console.log(JSON.stringify(rows[0]))
21+
const response = {
22+
"statusCode": 200,
23+
"headers": {
24+
"Content-Type" : "application/json"
25+
},
26+
"body": JSON.stringify(rows),
27+
"isBase64Encoded": false
28+
};
29+
callback(null, response);
30+
} catch (err) {
31+
console.log('Database ' + err)
32+
callback(err);
33+
}
34+
};
35+
36+
async function query (q) {
37+
const client = await pool.connect()
38+
let res
39+
try {
40+
await client.query('BEGIN')
41+
try {
42+
res = await client.query(q)
43+
await client.query('COMMIT')
44+
} catch (err) {
45+
await client.query('ROLLBACK')
46+
throw err
47+
}
48+
} finally {
49+
client.release()
50+
}
51+
52+
return res
53+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "postgreSQL-node-example",
3+
"version": "1.0.0",
4+
"main": "handler.js",
5+
"type": "commonjs",
6+
"keywords": [],
7+
"author": "",
8+
"license": "ISC",
9+
"dependencies": {
10+
"pg": "^8.10.0"
11+
},
12+
"devDependencies": {
13+
"serverless-scaleway-functions": "^0.4.6"
14+
}
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
service: scaleway-node18
2+
configValidationMode: off
3+
singleSource: false
4+
provider:
5+
name: scaleway
6+
runtime: node18
7+
8+
secret:
9+
PG_HOST: "your host IP address"
10+
PG_USER: "your database username"
11+
PG_DATABASE: "your database name"
12+
PG_PASSWORD: "your databse user password"
13+
PG_PORT: "your database port"
14+
15+
plugins:
16+
- serverless-scaleway-functions
17+
18+
package:
19+
patterns:
20+
- '!.gitignore'
21+
- '!.git/**'
22+
23+
functions:
24+
get-all-from-table
25+
handler: handler.handle

0 commit comments

Comments
 (0)