Skip to content

Latest commit

 

History

History

docs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

deno-postgres

Build Status Gitter chat

PostgreSQL driver for Deno.

deno-postgres is being developed based on excellent work of node-postgres and pq.

Example

import { Client } from "https://deno.land/x/postgres/mod.ts";

async function main() {
  const client = new Client({
    user: "user",
    database: "test",
    hostname: "localhost",
    port: "5432"
  });
  await client.connect();
  const result = await client.query("SELECT * FROM people;");
  console.log(result.rows);
  await client.end();
}

main();

API

deno-postgres follows node-postgres API to make transition for Node devs as easy as possible.

Connecting to DB

If any of parameters is missing it is read from environmental variable.

import { Client } from "https://deno.land/x/postgres/mod.ts";

let config;

config = {
  hostname: "localhost",
  port: "5432",
  user: "user",
  database: "test",
  applicationName: "my_custom_app"
};
// alternatively
config = "postgres://user@localhost:5432/test?application_name=my_custom_app";

const client = new Client(config);
await client.connect();
await client.end();

Queries

Simple query

const result = await client.query("SELECT * FROM people;");
console.log(result.rows);

Parametrized query

const result = await client.query(
  "SELECT * FROM people WHERE age > $1 AND age < $2;",
  10,
  20
);
console.log(result.rows);

// equivalent using QueryConfig interface
const result = await client.query({
  text: "SELECT * FROM people WHERE age > $1 AND age < $2;",
  args: [10, 20]
});
console.log(result.rows);