axxerion-api/modules/zz-test.js

58 lines
1.2 KiB
JavaScript

const { Client } = require("pg");
const myFunction = async (req, res) => {
const client = new Client({
host: "localhost",
database: "axdb"
});
await client.connect();
const tableName = "osi_acct";
const result = await client.query(`SELECT * FROM pg_tables WHERE schemaname = 'public'`);
console.log("result.rows:", result.rows);
/*
const data = [{id: '123', other: 'else'},{id: 'asd', other: 'else'}];
const deleteQuery = `DELETE FROM ${tableName} WHERE id = $1`;
for (item of data) {
await client.query(deleteQuery, [item.id]);
}
const result2 = await client.query(`SELECT COUNT(*) FROM ${tableName}`);
console.log("result2.rows:", result2.rows);
const totalRows = result2.rows[0].count;
console.log("totalRows:", totalRows);
const tableExists = `
SELECT EXISTS (
SELECT 1
FROM pg_tables
WHERE tablename = $1
) AS table_exists`;
const result = await client.query(tableExists, [tableName]);
console.log("result.rows:", result.rows);
if (result.rows[0]["table_exists"]) {
console.log("The table exists");
} else {
console.log("The table does not exist");
}
*/
await client.end();
};
myFunction();