NodeJS
ElectricSQL supports Node.js server application using the better-sqlite3 driver.
Dependencies
Add better-sqlite3
as a dependency to your app, e.g.:
yarn add 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 your config with an least an auth token.
// See Usage -> Authentication for more details.
const config = {
auth: {
token: '...'
}
}
// 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)
You can now use the client to read, write and sync data, e.g.:
const { db } = electric
const results = db.projects.findMany()
console.log(results)
See Usage -> Data access and Integrations -> Frontend for more information.