Sequelize for SQLite
To use Sequelize with SQLite, you need to install the @sequelize/sqlite
dialect package:
- npm
- Yarn
- pnpm
npm i @sequelize/sqlite3
yarn add @sequelize/sqlite3
pnpm add @sequelize/sqlite3
Then use the SqliteDialect
class as the dialect option in the Sequelize constructor:
import { Sequelize } from '@sequelize/core';
import { SqliteDialect } from '@sequelize/sqlite3';
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'sequelize.sqlite',
});
Connection Options
Connection Options are used to configure a connection to the database.
The simplest way to use them is at the root of the configuration object. These options can also be
used in the replication
option to customize the connection for each replica,
and can be modified by the beforeConnect
hook on a connection-by-connection basis.
The following options are accepted by the SQLite dialect:
Option | Description |
---|---|
storage | Path to the SQLite database file. |
mode | An 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:
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:
Option | Description |
---|---|
foreignKeys | If set to false, SQLite will not enforce foreign keys. |