Skip to main content
Extremely Popular
Go to Latest
method QueryClient.prototype.queryObject
import { QueryClient } from "https://deno.land/x/postgres@v0.17.0/client.ts";

This method allows executed queries to be retrieved as object entries. It supports a generic interface in order to type the entries retrieved by the query

import { Client } from "./client.ts";

const my_client = new Client();

{
	 const { rows } = await my_client.queryObject(
    "SELECT ID, NAME FROM CLIENTS"
	 ); // Record<string, unknown>
}

{
	 const { rows } = await my_client.queryObject<{id: number, name: string}>(
    "SELECT ID, NAME FROM CLIENTS"
  ); // Array<{id: number, name: string}>
}

You can also map the expected results to object fields using the configuration interface. This will be assigned in the order they were provided

import { Client } from "./client.ts";

const my_client = new Client();

{
  const {rows} = await my_client.queryObject(
    "SELECT ID, NAME FROM CLIENTS"
  );

	 console.log(rows); // [{id: 78, name: "Frank"}, {id: 15, name: "Sarah"}]
}

{
	 const {rows} = await my_client.queryObject({
    text: "SELECT ID, NAME FROM CLIENTS",
 	 fields: ["personal_id", "complete_name"],
	  });

	 console.log(rows); // [{personal_id: 78, complete_name: "Frank"}, {personal_id: 15, complete_name: "Sarah"}]
}

It also allows you to execute prepared statements with template strings

import { Client } from "./client.ts";

const my_client = new Client();
const id = 12;
// Array<{id: number, name: string}>
const { rows } = await my_client.queryObject<{id: number, name: string}>`SELECT ID, NAME FROM CLIENTS WHERE ID = ${id}`;

Parameters

query: string
optional
args: QueryArguments

Parameters

query: TemplateStringsArray
...args: unknown[]