Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

SQLite

To use Payload with SQLite, install the package @payloadcms/db-sqlite. It leverages Drizzle ORM and libSQL to interact with a SQLite database that you provide.

It automatically manages changes to your database for you in development mode, and exposes a full suite of migration controls for you to leverage in order to keep other database environments in sync with your schema. DDL transformations are automatically generated.

To configure Payload to use SQLite, pass the sqliteAdapter to your Payload Config as follows:

1
import { sqliteAdapter } from '@payloadcms/db-sqlite'
2
3
export default buildConfig({
4
// Your config goes here
5
collections: [
6
// Collections go here
7
],
8
// Configure the SQLite adapter here
9
db: sqliteAdapter({
10
// SQLite-specific arguments go here.
11
// `client.url` is required.
12
client: {
13
url: process.env.DATABASE_URL,
14
authToken: process.env.DATABASE_AUTH_TOKEN,
15
}
16
}),
17
})

Options

OptionDescription
client *Client connection options that will be passed to createClient from @libsql/client.
pushDisable Drizzle's db push in development mode. By default, push is enabled for development mode only.
migrationDirCustomize the directory that migrations are stored.
loggerThe instance of the logger to be passed to drizzle. By default Payload's will be used.
transactionOptionsA SQLiteTransactionConfig object for transactions, or set to false to disable using transactions. More details
localesSuffixA string appended to the end of table names for storing localized fields. Default is '_locales'.
relationshipsSuffixA string appended to the end of table names for storing relationships. Default is '_rels'.
versionsSuffixA string appended to the end of table names for storing versions. Defaults to '_v'.
beforeSchemaInitDrizzle schema hook. Runs before the schema is built. More Details
afterSchemaInitDrizzle schema hook. Runs after the schema is built. More Details

Access to Drizzle

After Payload is initialized, this adapter will expose the full power of Drizzle to you for use if you need it.

You can access Drizzle as follows:

1
payload.db.drizzle

Tables and relations

In addition to exposing Drizzle directly, all of the tables and Drizzle relations are exposed for you via the payload.db property as well.

  • Tables - payload.db.tables
  • Relations - payload.db.relations

Prototyping in development mode

Drizzle exposes two ways to work locally in development mode.

The first is db push, which automatically pushes changes you make to your Payload Config (and therefore, Drizzle schema) to your database so you don't have to manually migrate every time you change your Payload Config. This only works in development mode, and should not be mixed with manually running migrate commands.

You will be warned if any changes that you make will entail data loss while in development mode. Push is enabled by default, but you can opt out if you'd like.

Alternatively, you can disable push and rely solely on migrations to keep your local database in sync with your Payload Config.

Migration workflows

In SQLite, migrations are a fundamental aspect of working with Payload and you should become familiar with how they work.

For more information about migrations, click here.

Drizzle schema hooks

beforeSchemaInit

Runs before the schema is built. You can use this hook to extend your database structure with tables that won't be managed by Payload.

1
import { sqliteAdapter } from '@payloadcms/db-sqlite'
2
import { integer, sqliteTable } from 'drizzle-orm/sqlite-core'
3
4
sqliteAdapter({
5
beforeSchemaInit: [
6
({ schema, adapter }) => {
7
return {
8
...schema,
9
tables: {
10
...schema.tables,
11
addedTable: sqliteTable('added_table', {
12
id: integer('id').primaryKey({ autoIncrement: true }),
13
}),
14
},
15
}
16
},
17
],
18
})

One use case is preserving your existing database structure when migrating to Payload. By default, Payload drops the current database schema, which may not be desirable in this scenario. To quickly generate the Drizzle schema from your database you can use Drizzle Introspection You should get the schema.ts file which may look like this:

1
import { sqliteTable, text, uniqueIndex, integer } from 'drizzle-orm/sqlite-core'
2
3
export const users = sqliteTable('users', {
4
id: integer('id').primaryKey({ autoIncrement: true }),
5
fullName: text('full_name'),
6
phone: text('phone', {length: 256}),
7
})
8
9
export const countries = sqliteTable(
10
'countries',
11
{
12
id: integer('id').primaryKey({ autoIncrement: true }),
13
name: text('name', { length: 256 }),
14
},
15
(countries) => {
16
return {
17
nameIndex: uniqueIndex('name_idx').on(countries.name),
18
}
19
},
20
)

You can import them into your config and append to the schema with the beforeSchemaInit hook like this:

1
import { sqliteAdapter } from '@payloadcms/db-sqlite'
2
import { users, countries } from '../drizzle/schema'
3
4
sqliteAdapter({
5
beforeSchemaInit: [
6
({ schema, adapter }) => {
7
return {
8
...schema,
9
tables: {
10
...schema.tables,
11
users,
12
countries
13
},
14
}
15
},
16
],
17
})

Make sure Payload doesn't overlap table names with its collections. For example, if you already have a collection with slug "users", you should either change the slug or dbName to change the table name for this collection.

afterSchemaInit

Runs after the Drizzle schema is built. You can use this hook to modify the schema with features that aren't supported by Payload, or if you want to add a column that you don't want to be in the Payload config. To extend a table, Payload exposes extendTable utillity to the args. You can refer to the Drizzle documentation. The following example adds the extra_integer_column column and a composite index on country and city columns.

1
import { sqliteAdapter } from '@payloadcms/db-sqlite'
2
import { index, integer } from 'drizzle-orm/sqlite-core'
3
import { buildConfig } from 'payload'
4
5
export default buildConfig({
6
collections: [
7
{
8
slug: 'places',
9
fields: [
10
{
11
name: 'country',
12
type: 'text',
13
},
14
{
15
name: 'city',
16
type: 'text',
17
},
18
],
19
},
20
],
21
db: sqliteAdapter({
22
afterSchemaInit: [
23
({ schema, extendTable, adapter }) => {
24
extendTable({
25
table: schema.tables.places,
26
columns: {
27
extraIntegerColumn: integer('extra_integer_column'),
28
},
29
extraConfig: (table) => ({
30
country_city_composite_index: index('country_city_composite_index').on(
31
table.country,
32
table.city,
33
),
34
}),
35
})
36
37
return schema
38
},
39
],
40
}),
41
})
Next

Fields Overview