Skip to content

Commit

Permalink
Update example to use npm package for postgres (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
philhawksworth authored Jan 30, 2025
1 parent d060f13 commit 7fd2b02
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions examples/scripts/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
* @difficulty intermediate
* @tags cli, deploy
* @run --allow-net --allow-env <url>
* @resource {https://deno-postgres.com/} Deno Postgres docs
* @resource {https://deno.land/x/postgres} Deno Postgres on deno.land/x
* @group Databases
*
* Using the Deno Postgres client, you can connect to a Postgres database
* Using the npm Postgres client, you can connect to a Postgres database
* running anywhere.
*/

// Import the Client constructor from deno.land/x
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
// Import the Postgres package from
import postgres from "npm:postgres";

// Initialize the client with connection information for your database, and
// create a connection.
const client = new Client({
const sql = postgres({
user: "user",
database: "test",
hostname: "localhost",
port: 5432,
});
await client.connect();

// Execute a SQL query
const result = await client.queryArray("SELECT ID, NAME FROM PEOPLE");
console.log(result.rows); // [[1, 'Carlos'], [2, 'John'], ...]
const result = await sql`
SELECT ID, NAME FROM PEOPLE
`;
console.log(result);

// Close the connection to the database
await client.end();
await sql.end();

0 comments on commit 7fd2b02

Please sign in to comment.