Config file
Extensions, setup code and anything else that cannot be written as plain JSON in nuxt.config.ts go in server/pglite.config.ts. It is optional; without it the instance uses the nuxt.config options.
import { citext } from '@electric-sql/pglite/contrib/citext'
import { vector } from '@electric-sql/pglite-pgvector'
export default definePGliteServerConfig({
extensions: { citext, vector },
init: async (pg) => {
await pg.exec('CREATE EXTENSION IF NOT EXISTS vector')
},
dispose: async (pg) => {
// runs before the instance is closed on server shutdown
},
})
definePGliteServerConfig is auto-imported. It accepts PGlite's own options (dataDir, extensions, debug, relaxedDurability, …) plus init, dispose and devtoolsActions.
Extensions
Import an extension from its package and add it to extensions, as you would with PGlite.create(). usePGlite() is typed from the config, so an extension that adds a namespace shows up on the instance:
import { live } from '@electric-sql/pglite/live'
export default definePGliteServerConfig({
extensions: { live },
})
export default defineEventHandler(async () => {
const pg = await usePGlite()
pg.live // typed, because `live` is in the config
return pg.query('SELECT count(*) FROM todos')
})
Postgres extensions such as vector still need CREATE EXTENSION, which is what init is for.
Prepare the instance with init
init runs once per created instance, before any usePGlite() call receives it: the place for CREATE EXTENSION, schema setup or seeding.
export default definePGliteServerConfig({
init: async (pg) => {
await pg.exec(`
CREATE TABLE IF NOT EXISTS todos (
id serial PRIMARY KEY,
title text NOT NULL
)
`)
},
})
To build the schema from a folder of SQL migration files instead, call applyMigrations from init.
Use the pg it receives: calling usePGlite() from inside init fails with a pointer instead of waiting for itself. If init throws, the instance is closed and the next usePGlite() tries again. For the socket's instance, see When init fails.
dispose runs before the instance is closed through the module, which happens when the Nitro server shuts down.
Per-environment settings
$development, $production and $test keys work in the config file as they do in nuxt.config.ts. In that environment, the keys they set replace the top-level ones (a shallow merge):
import { vector } from '@electric-sql/pglite-pgvector'
export default definePGliteServerConfig({
extensions: { vector },
$development: { dataDir: 'memory://' },
$test: { dataDir: 'memory://' },
})
$test wins over the other two while testing.