Back to Sequelize

SQLite

docs/databases/sqlite.mdx

latest3.7 KB
Original Source

Sequelize for SQLite

To use Sequelize with SQLite, you need to install the @sequelize/sqlite dialect package:

bash
npm i @sequelize/sqlite3

Then use the SqliteDialect class as the dialect option in the Sequelize constructor:

ts
import { Sequelize } from '@sequelize/core';
import { SqliteDialect } from '@sequelize/sqlite3';

const sequelize = new Sequelize({
  dialect: SqliteDialect,
  storage: 'sequelize.sqlite',
});

Connection Options

import ConnectionOptions from './_connection-options.md';

<ConnectionOptions />

The following options are accepted by the SQLite dialect:

OptionDescription
storagePath to the SQLite database file.
modeAn integer bit flag that represents the mode to open the database connection with.

The mode can be a union (using \|) of OPEN_READONLY, OPEN_READWRITE, OPEN_CREATE, OPEN_FULLMUTEX, OPEN_URI, OPEN_SHAREDCACHE, OPEN_PRIVATECACHE, each of which are exposed by @sequelize/sqlite.

Refer to the SQLite documentation to learn what each of these flags do. | | password | The "PRAGMA KEY" password to use for the connection, if using plugins like sqlcipher. |

Temporary Storages

SQLite supports two types of temporary storages:

  • Set storage to an empty string to use a disk-based temporary storage.
  • Set storage to ':memory:' to use a memory-based temporary storage.

In both cases, the database will be destroyed when the connection is closed. As such, using temporary storage requires configuring the Connection Pool to keep a single connection open, using the following configuration:

ts
const sequelize = new Sequelize({
  dialect: SqliteDialect,
  storage: ':memory:', // or ''
  pool: { max: 1, idle: Infinity, maxUses: Infinity },
});

Other SQLite Options

The following options are also available for SQLite:

OptionDescription
foreignKeysIf set to false, SQLite will not enforce foreign keys.