File size: 3,270 Bytes
bb9381b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | import waitPort from 'wait-port';
import fs from 'fs';
import pkg from 'pg';
const { Client } = pkg;
const {
POSTGRES_HOST: HOST,
POSTGRES_HOST_FILE: HOST_FILE,
POSTGRES_USER: USER,
POSTGRES_USER_FILE: USER_FILE,
POSTGRES_PASSWORD: PASSWORD,
POSTGRES_PASSWORD_FILE: PASSWORD_FILE,
POSTGRES_DB: DB,
POSTGRES_DB_FILE: DB_FILE,
} = process.env;
let client;
async function init() {
const host = HOST_FILE ? fs.readFileSync(HOST_FILE) : HOST;
const user = USER_FILE ? fs.readFileSync(USER_FILE) : USER;
const password = PASSWORD_FILE ? fs.readFileSync(PASSWORD_FILE, 'utf8') : PASSWORD;
const database = DB_FILE ? fs.readFileSync(DB_FILE) : DB;
await waitPort({
host,
port: 5432,
timeout: 10000,
waitForDns: true,
});
client = new Client({
host,
user,
password,
database
});
return client.connect().then(async () => {
console.log(`Connected to postgres db at host ${HOST}`);
// Run the SQL instruction to create the table if it does not exist
await client.query('CREATE TABLE IF NOT EXISTS todo_items (id varchar(36), name varchar(255), completed boolean)');
console.log('Connected to db and created table todo_items if it did not exist');
}).catch(err => {
console.error('Unable to connect to the database:', err);
});
}
// Get all items from the table
async function getItems() {
return client.query('SELECT * FROM todo_items').then(res => {
return res.rows.map(row => ({
id: row.id,
name: row.name,
completed: row.completed
}));
}).catch(err => {
console.error('Unable to get items:', err);
});
}
// End the connection
async function teardown() {
return client.end().then(() => {
console.log('Client ended');
}).catch(err => {
console.error('Unable to end client:', err);
});
}
// Get one item by id from the table
async function getItem(id) {
return client.query('SELECT * FROM todo_items WHERE id = $1', [id]).then(res => {
return res.rows.length > 0 ? res.rows[0] : null;
}).catch(err => {
console.error('Unable to get item:', err);
});
}
// Store one item in the table
async function storeItem(item) {
return client.query('INSERT INTO todo_items(id, name, completed) VALUES($1, $2, $3)', [item.id, item.name, item.completed]).then(() => {
console.log('Stored item:', item);
}).catch(err => {
console.error('Unable to store item:', err);
});
}
// Update one item by id in the table
async function updateItem(id, item) {
return client.query('UPDATE todo_items SET name = $1, completed = $2 WHERE id = $3', [item.name, item.completed, id]).then(() => {
console.log('Updated item:', item);
}).catch(err => {
console.error('Unable to update item:', err);
});
}
// Remove one item by id from the table
async function removeItem(id) {
return client.query('DELETE FROM todo_items WHERE id = $1', [id]).then(() => {
console.log('Removed item:', id);
}).catch(err => {
console.error('Unable to remove item:', err);
});
}
export default {
init,
teardown,
getItems,
getItem,
storeItem,
updateItem,
removeItem,
};
|