Execute Query and return results

Executes an arbitrary query and return the row results

Script postgresql Verified

by rossmccrann ยท 8/4/2022

The script

Submitted by rossmccrann Deno
Verified 374 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
  • Submitted by kungyc337 Python3
    Created 1096 days ago
    1
    #import wmill
    2
    
    
    3
    def main(x: str):
    4
        return x