# Serverless [Postgres.js](https://github.com/porsager/postgres) for [Neon](https://neon.tech)

## Getting Started

### SQL-over-HTTP

```js
import postgres from "@brevity-builder/postgres/http";

const sql = postgres(connectionString);

async function getUsersOver(age) {
  const users = await sql`
    select
      name,
      age
    from users
    where age > ${age}
  `;
  // users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
  return users;
}

async function insertUser({ name, age }) {
  const users = await sql`
    insert into users
      (name, age)
    values
      (${name}, ${age})
    returning name, age
  `;
  // users = Result [{ name: "Murray", age: 68 }]
  return users;
}

async function transaction() {
  const showLatestN = 10;

  // Note: that neon's sql-over-http does not support interactive transactions
  // so we are passing an arary to begin (and not a callback).
  const [posts, tags] = await sql.begin([
    sql`SELECT * FROM posts ORDER BY posted_at DESC LIMIT ${showLatestN}`,
    sql`SELECT * FROM tags`,
  ]);
}
```

### SQL-over-Websoket

```js
import postgres from "@brevity-builder/postgres";

const sql = postgres(connectionString);

async function getUsersOver(age) {
  const users = await sql`
    select
      name,
      age
    from users
    where age > ${age}
  `;
  // users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
  return users;
}

async function insertUser({ name, age }) {
  const users = await sql`
    insert into users
      (name, age)
    values
      (${name}, ${age})
    returning name, age
  `;
  // users = Result [{ name: "Murray", age: 68 }]
  return users;
}

async function transaction() {
  const [user, account] = await sql.begin(async (sql) => {
    const [user] = await sql`
    insert into users (
      name
    ) values (
      'Murray'
    )
    returning *
  `;

    const [account] = await sql`
    insert into accounts (
      user_id
    ) values (
      ${user.user_id}
    )
    returning *
  `;

    return [user, account];
  });
}
```

## Docs

Please see the [Postgres.js](https://github.com/porsager/postgres) docs for more information about the api.

## License

MIT
