Execute Query and return results
One script reply has been approved by the moderators Verified

Executes an arbitrary query and return the row results

Created by rossmccrann 1250 days ago Picked 27 times
Submitted by rossmccrann Deno
Verified 243 days ago
1
import {
2
  type Sql,
3
} from "https://deno.land/x/[email protected]/mod.ts";
4

5
import { Client } from "https://deno.land/x/[email protected]/mod.ts"
6

7
type Postgresql = {
8
  host: string;
9
  port: number;
10
  user: string;
11
  dbname: string;
12
  sslmode: string;
13
  password: string;
14
};
15
export async function main(db: Postgresql, query: Sql = "SELECT * FROM demo;") {
16
  if (!query) {
17
    throw Error("Query must not be empty.");
18
  }
19
  const { rows } = await pgClient(db).queryObject(query);
20
  return rows;
21
}
22

23
export function pgClient(db: any) {    
24
  let db2 = {
25
    ...db,
26
    hostname: db.host,
27
    database: db.dbname,
28
    tls: {
29
        enabled: false,
30
    },
31
  }
32
  return new Client(db2)
33
}
Other submissions