initial import
This commit is contained in:
25
app/lib/sql-shim.ts
Normal file
25
app/lib/sql-shim.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user