Consider this Deno code snippet that queries a PostgreSQL database for all users:
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
const client = new Client({ user: "user", database: "testdb", hostname: "localhost", password: "pass", port: 5432 });
await client.connect();
const result = await client.queryObject("SELECT id, name FROM users ORDER BY id LIMIT 2");
await client.end();
console.log(result.rows);If the users table has rows: (1, 'Alice'), (2, 'Bob'), (3, 'Carol'), what will be logged?
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts"; const client = new Client({ user: "user", database: "testdb", hostname: "localhost", password: "pass", port: 5432 }); await client.connect(); const result = await client.queryObject("SELECT id, name FROM users ORDER BY id LIMIT 2"); await client.end(); console.log(result.rows);
Look at the LIMIT 2 clause and the queryObject method output format.
The queryObject method returns an object with a rows array of objects matching the selected columns. The query limits results to 2 rows, so only Alice and Bob appear.
Identify the option that will cause a syntax error when trying to create a PostgreSQL client in Deno.
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts"; const client = new Client({ user: "user", database: "testdb", hostname: "localhost", password: "pass", port: 5432 }); await client.connect(); await client.end();
Check for matching brackets and commas in the object literal.
Option A has a mismatched parenthesis and curly brace: port: 5432 )}; which causes a syntax error.
Given this code snippet that counts rows in a table:
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
const client = new Client({ user: "user", database: "testdb", hostname: "localhost", password: "pass", port: 5432 });
await client.connect();
const result = await client.queryObject("SELECT COUNT(*) AS count FROM users");
await client.end();
const count = result.rows[0].count;
If the users table has 5 rows, what is the type and value of count?
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts"; const client = new Client({ user: "user", database: "testdb", hostname: "localhost", password: "pass", port: 5432 }); await client.connect(); const result = await client.queryObject("SELECT COUNT(*) AS count FROM users"); await client.end(); const count = result.rows[0].count;
PostgreSQL returns count as a numeric type. Deno's queryObject parses it accordingly.
The COUNT(*) returns a numeric value. The queryObject method parses it as a number, so count is the number 5.
Look at this code snippet:
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
const client = new Client({ user: "user", database: "testdb", hostname: "localhost", password: "pass", port: 5432 });
await client.connect();
const result = await client.queryObject("SELECT id, name FROM users WHERE id = $1", ["abc"]);
await client.end();
console.log(result.rows);Which option will cause a runtime error when running this code?
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts"; const client = new Client({ user: "user", database: "testdb", hostname: "localhost", password: "pass", port: 5432 }); await client.connect(); const result = await client.queryObject("SELECT id, name FROM users WHERE id = $1", ["abc"]); await client.end(); console.log(result.rows);
Check the data type of the parameter and the column type.
The id column is likely an integer. Passing a string "abc" causes PostgreSQL to throw a runtime error about invalid input syntax.
When using the Deno PostgreSQL client library, which statement about connection pooling is correct?
Check the official Deno PostgreSQL module documentation about Client vs Pool.
The Client class manages a single connection. To use connection pooling, you must create a Pool instance explicitly.
