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: `janet@example.com`,
  active: true,
});
emailactive
adam@example.comtrue
forbes@example.comtrue
dee@example.comtrue
+janet@example.comtrue
await tables.users(db).update(
  {email: `dee@example.com`},
  {active: false},
);
emailactive
adam@example.comtrue
forbes@example.comtrue
dee@example.comfalse
janet@example.comtrue
await tables.users(db).delete({
  email: `forbes@example.com`,
});
emailactive
adam@example.comtrue
-forbes@example.comtrue
dee@example.comfalse
janet@example.comtrue

Type safe single-table queries

await tables.users(db)
  .find({
    email: anyOf([
      `adam@example.com`,
      `janet@example.com`,
    ]),
  })
  .all();
emailactive
·adam@example.comtrue
dee@example.comfalse
·janet@example.comtrue

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
adam@example.com4
janet@example.com16

Wrap any operations into an atomic transaction

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
  `);
});
emailposts
adam@example.com4
dee@example.com3
janet@example.com16

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