25 lines
824 B
TypeScript
25 lines
824 B
TypeScript
import { Client, QueryResult, QueryResultRow } from 'pg';
|
|
|
|
const client = new Client({
|
|
// connectionString: process.env.DATABASE_URL,
|
|
host: process.env.POSTGRES_HOST,
|
|
user: process.env.POSTGRES_USER,
|
|
password: process.env.POSTGRES_PASSWORD,
|
|
database: process.env.POSTGRES_DB
|
|
});
|
|
|
|
client.connect();
|
|
|
|
/** an adapter function which simulates @vercel/postgres `sql` function */
|
|
export function sql<T extends QueryResultRow>(strings: TemplateStringsArray, ...values: any[]): Promise<QueryResult<T>> {
|
|
// string values need to be wrapped in single quotes
|
|
const fixedValues = values.map((value) => {
|
|
if (typeof value === 'string') {
|
|
return `'${value}'`;
|
|
}
|
|
return value;
|
|
});
|
|
|
|
const query = String.raw(strings, ...fixedValues);
|
|
return client.query<T>(query);
|
|
} |