Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/postgres/mod.ts>Transaction#queryObject

PostgreSQL driver for Deno
Extremely Popular
Latest
method Transaction.prototype.queryObject
import { Transaction } from "https://deno.land/x/postgres@v0.19.3/mod.ts";

Executed queries and retrieve the data as object entries. It supports a generic in order to type the entries retrieved by the query

import { Client } from "https://deno.land/x/postgres/mod.ts";
const my_client = new Client();

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

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

Parameters

query: string
optional
args: QueryArguments

Use the configuration object for more advance options to execute the query

import { Client } from "https://deno.land/x/postgres/mod.ts";
const my_client = new Client();

const { rows: rows1 } = await my_client.queryObject(
  "SELECT ID, NAME FROM CLIENTS"
);
console.log(rows1); // [{id: 78, name: "Frank"}, {id: 15, name: "Sarah"}]

const { rows: rows2 } = await my_client.queryObject({
  text: "SELECT ID, NAME FROM CLIENTS",
  fields: ["personal_id", "complete_name"],
});
console.log(rows2); // [{personal_id: 78, complete_name: "Frank"}, {personal_id: 15, complete_name: "Sarah"}]

Execute prepared statements with template strings

import { Client } from "https://deno.land/x/postgres/mod.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: TemplateStringsArray
...args: unknown[]