Building SQL QueriesDocumentation

Query SQL Databases
using Node.js and TypeScript

@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.

Get started

100% free and open source

Used in production at these companies, and more

MavenoidQuandooRolling VersionsSave WillpowerThreads StylingJepso

Type safe API for simple operations

await tables.users(db).insert({
  email: `[email protected]`,
  active: true,
});
emailactive
[email protected]true
[email protected]true
[email protected]true
+[email protected]true
await tables.users(db).update(
  {email: `[email protected]`},
  {active: false},
);
emailactive
[email protected]true
[email protected]true
[email protected]false
[email protected]true
await tables.users(db).delete({
  email: `[email protected]`,
});
emailactive
[email protected]true
-[email protected]true
[email protected]false
[email protected]true

Type safe single-table queries

await tables.users(db)
  .find({
    email: anyOf([
      `[email protected]`,
      `[email protected]`,
    ]),
  })
  .all();
emailactive
·[email protected]true
[email protected]false
·[email protected]true

Use the full power of SQL to write complex queries

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
`);
emailposts
[email protected]4
[email protected]16

Wrap any operations into an atomic transaction

await db.tx(async (db) => {
  await tables.users(db).update(
    {email: `[email protected]`},
    {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
  `);
});
emailposts
[email protected]4
[email protected]3
[email protected]16

We support your database

Each supported database engine has a separate driver. This means that @databases can take advantage of the features unique to each database. Core elements are shared. This makes it quick to add support for a new database engine.

Postgres
Postgres
MySQL
MySQL
SQLite
SQLite
Expo
Expo
Google BigQuery
BigQuery
More Soon...

Key features

TypeScript Logo

Type Safe

@databases is written in TypeScript, so every module has type safety and type definitions built in. In addition to that, we also have CLIs that let you generate types for your database tables.
NPM Logo

Modular

Each database driver is published as a separate module. Each problem we solve get its own reusable package. This means that even if you don't want to use @databases to connect to your SQL database, you can still use our locking implementation, or our queue implementation.
Promises Logo

Promises

All the @databases APIs are designed with promises in mind from the get go. You won't need to mess around with old callback styles. This keeps your code clean and simple.
Get started