This is an automated email from the ASF dual-hosted git repository. bsalzano pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/openserverless-tutorial.git
commit 6e8eaafadfe098b51f0f280bb920fe6dadd64d78 Author: Bruno Salzano <[email protected]> AuthorDate: Tue Mar 18 00:39:57 2025 +0100 use-database --- packages/contact/create-table.js | 65 ++++++++++++++++++++++++++++++++++++++++ packages/contact/write.js | 51 +++++++++++++++++++++++++++++++ web/index.html | 2 +- 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/packages/contact/create-table.js b/packages/contact/create-table.js new file mode 100644 index 0000000..144c50b --- /dev/null +++ b/packages/contact/create-table.js @@ -0,0 +1,65 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//--kind nodejs:default +//--param POSTGRES_URL $POSTGRES_URL + +const { Client } = require('pg') + +async function main(args) { + console.log('Starting create-table action') + const client = new Client({ connectionString: args.POSTGRES_URL }); + + const createSchema = `CREATE SCHEMA IF NOT EXISTS demo;` + + const createTable = ` + CREATE TABLE IF NOT EXISTS demo.contacts ( + id serial PRIMARY KEY, + name varchar(50), + email varchar(50), + phone varchar(50), + message varchar(300) + ); + ` + + try { + console.log(`Connecting to ${args.POSTGRES_URL}`); + await client.connect(); + console.log('Connected to database'); + await client.query(createSchema); + console.log('Schema demo created'); + await client.query(createTable); + console.log('Contact table created'); + return { result: 'OK' }; + } catch (e) { + if (e instanceof AggregateError) { + for (const err of e.errors) { + console.error('[ERROR] - ', err.message || err); + } + } else if (e instanceof Error) { + console.error('[ERROR] - ', e.message); + } else { + console.error('[ERROR] - ', e); + } + return { result: 'ERROR' }; + } finally { + console.log('Closing connection'); + if (client) { + await client.end(); + } + } +} diff --git a/packages/contact/write.js b/packages/contact/write.js new file mode 100644 index 0000000..c98c2ba --- /dev/null +++ b/packages/contact/write.js @@ -0,0 +1,51 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//--kind nodejs:default +//--param POSTGRES_URL $POSTGRES_URL + +const {Client} = require('pg') + +async function main(args) { + const client = new Client({connectionString: args.POSTGRES_URL}); + + // Connect to database server + await client.connect(); + + const {name, email, phone, message} = args; + + try { + let res = await client.query( + 'INSERT INTO demo.contacts(name,email,phone,message) VALUES($1,$2,$3,$4)', + [name, email, phone, message] + ); + console.log(res); + } catch (e) { + console.log(e); + throw e; + } finally { + client.end(); + } + + return { + body: args.body, + name, + email, + phone, + message + }; +} diff --git a/web/index.html b/web/index.html index b00a1d0..2521b40 100644 --- a/web/index.html +++ b/web/index.html @@ -33,7 +33,7 @@ <div class="w-50 p-4 border rounded bg-light shadow"> <h2 class="text-center mb-4">Get In Touch</h2> - <form method="POST" action="/api/v1/web/opstutorial/contact/submit" enctype="application/x-www-form-urlencoded"> + <form method="POST" action="/api/v1/web/opstutorial/contact/submit-write" enctype="application/x-www-form-urlencoded"> <div class="mb-3"> <label for="name" class="form-label">Name</label> <input type="text" class="form-control" id="name" name="name" placeholder="Insert your name">
