@brevity-builder/postgres
v0.0.22
Published
## Getting Started
Downloads
317
Readme
Serverless Postgres.js for Neon
Getting Started
SQL-over-HTTP
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
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 docs for more information about the api.
License
MIT