Skip to main content

better-sqlite3

ElectricSQL supports Node.js server application using the better-sqlite3 driver.

Dependencies

Add better-sqlite3 as a dependency to your app, e.g.:

npm install better-sqlite3

Usage

import Database from 'better-sqlite3'
import { electrify } from 'electric-sql/node'

// Import your generated database schema.
import { schema } from './generated/client'

// Define custom configuration if needed
const config = {
url: 'https://example.com:5133'
}

// Create the better-sqlite3 database connection. The first
// argument is your database name. Changing this will
// create/use a new local database file.
const conn = new Database('example.db')

// Follow the library recommendation to enable WAL mode to
// increase performance. As per:
// https://github.com/WiseLibs/better-sqlite3/blob/master/docs/performance.md
conn.pragma('journal_mode = WAL')

// Instantiate your electric client.
const electric = await electrify(conn, schema, config)

// Connect to Electric, passing along your authentication token
// See Usage -> Authentication for more details.
await electric.connect('your token')

You can now use the client to read, write and sync data, e.g.:

const { db } = electric

const results = await db.projects.findMany()
console.log(results)

See Usage -> Data access and Integrations -> Frontend for more information.