Initial Commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
import pg from 'pg';
|
||||
|
||||
dotenv.config({ path: path.resolve(__dirname, '../.env') });
|
||||
|
||||
export function getDbClient() {
|
||||
return new pg.Client({
|
||||
host: process.env.DB_HOST,
|
||||
port: Number(process.env.DB_PORT) || 5432,
|
||||
user: process.env.DB_USERNAME,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_DATABASE,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getStorageState() {
|
||||
const client = getDbClient();
|
||||
await client.connect();
|
||||
try {
|
||||
const { rows } = await client.query(
|
||||
`SELECT value FROM settings WHERE key = $1 LIMIT 1`,
|
||||
['session']
|
||||
);
|
||||
if (!rows.length) throw new Error("No 'session' row found in settings table");
|
||||
const { value } = rows[0];
|
||||
return typeof value === 'string' ? JSON.parse(value) : value;
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user