@databases lets you read and write data to Postgres, MySQL, SQLite and other databases in Node.js using ordinary SQL, with parameters automatically escaped to prevent SQL injection.
100% free and open source
await tables.users(db).insert({
email: `janet@example.com`,
active: true,
});
active | ||
---|---|---|
adam@example.com | true | |
forbes@example.com | true | |
dee@example.com | true | |
+ | janet@example.com | true |
await tables.users(db).update(
{email: `dee@example.com`},
{active: false},
);
active | ||
---|---|---|
adam@example.com | true | |
forbes@example.com | true | |
⇄ | dee@example.com | false |
janet@example.com | true |
await tables.users(db).delete({
email: `forbes@example.com`,
});
active | ||
---|---|---|
adam@example.com | true | |
- | forbes@example.com | true |
dee@example.com | false | |
janet@example.com | true |
await tables.users(db)
.find({
email: anyOf([
`adam@example.com`,
`janet@example.com`,
]),
})
.all();
active | ||
---|---|---|
· | adam@example.com | true |
dee@example.com | false | |
· | janet@example.com | true |
await db.query(sql`
SELECT
users.email,
count(*) AS posts
FROM users
INNER JOIN posts
ON (users.email = posts.author)
GROUP BY users.email
WHERE users.active = true
`);
posts | ||
---|---|---|
adam@example.com | 4 | |
janet@example.com | 16 |
await db.tx(async (db) => {
await tables.users(db).update(
{email: `dee@example.com`},
{active: true},
);
return await db.query(sql`
SELECT
users.email,
count(*) AS posts
FROM users
INNER JOIN posts
ON (users.email = posts.author)
GROUP BY users.email
WHERE users.active = true
`);
});
posts | ||
---|---|---|
adam@example.com | 4 | |
dee@example.com | 3 | |
janet@example.com | 16 |
Type Safe
Modular
Promises