Recipes

Config and provider

nuxt-pglite/core, the config helper and the lazy instance provider, usable in any runtime.

The framework-agnostic parts of the module ship as their own entries: nuxt-pglite/core (this page), nuxt-pglite/socket, nuxt-pglite/migrations, which depends on Node built-ins only, and nuxt-pglite/testing, for test suites. nuxt-pglite/core imports only @electric-sql/pglite, and only when an instance is created.

import { createPGliteProvider, definePGliteConfig, resolveEnvConfig } from 'nuxt-pglite/core'

definePGliteConfig

The same helper as definePGliteServerConfig, under its plain name: PGlite's options plus init, dispose and devtools, with the extensions inferred so that they are typed on the instance.

pglite.config.ts
import { definePGliteConfig } from 'nuxt-pglite/core'
import { vector } from '@electric-sql/pglite-pgvector'

export default definePGliteConfig({
  dataDir: './data',
  extensions: { vector },
  init: async (pg) => {
    await pg.exec('CREATE EXTENSION IF NOT EXISTS vector')
  },
})

A server config written with it works in Nuxt too, which is how one file can serve a Nuxt app and a standalone script.

$development, $production and $test keys are applied by Nuxt; outside it, apply them yourself:

import { resolveEnvConfig } from 'nuxt-pglite/core'
import config from './pglite.config'

const resolved = resolveEnvConfig(config, {
  dev: process.env.NODE_ENV === 'development',
  test: process.env.NODE_ENV === 'test',
})

createPGliteProvider

Holds a single, lazily created instance:

import { createPGliteProvider } from 'nuxt-pglite/core'
import config from './pglite.config'

const pglite = createPGliteProvider(config)

const pg = await pglite.use() // created and `init`-ed on first call
pglite.instance // the open instance, or undefined
await pglite.close() // `dispose`, then close; a later use() creates a new one

It behaves like the one behind usePGlite(): concurrent use() calls share one creation, a failed creation is retried on the next call, and a closed instance is replaced.

For an instance created some other way (a PGliteWorker, custom options), pass the lower-level shape instead of a config:

import { PGlite } from '@electric-sql/pglite'
import { createPGliteProvider } from 'nuxt-pglite/core'

const pglite = createPGliteProvider({
  create: () => PGlite.create('memory://'),
  init: async (pg) => {
    await pg.exec('CREATE TABLE IF NOT EXISTS kv (k text PRIMARY KEY, v text)')
  },
  dispose: async (pg) => {
    // before close
  },
})