TypeScript client
Install the electric-sql
library and npx electric-sql generate
your database Client.
Install
Npm
Install the published TypeScript client and generator:
npm install electric-sql
GitHub
The source code is in the clients/typescript subfolder of the electric-sql/electric repo.
Generate
Use the Generator command to generate a type-safe database client. First, make sure your sync service is running.
Then run:
npx electric-sql generate
By default this will connect to the sync service on http://localhost:5133
and output generated files to a ./src/generated/client
folder.
See API -> CLI & Generator for the command-line options.
strict
modeThe generated type-safe client assumes that your TypeScript compiler will use strict
mode for stronger correctness guarantees. Disabling it, or disabling similar checks like strictNullChecks
, might lead to unexpected type errors - see the Zod requirements for more info.
Outputs
The generator command outputs a database schema, type-safe database client and bundled SQL migrations. See Usage -> Data access -> Client for details on how to import and use these when instantiating a database client.
Pre-build script
You can wire up the generator to run every time you build your app by adding a prebuild
script to your package.json
:
"scripts": {
"prebuild": "npx electric-sql generate"
// ...
}
Watch mode
Or you can run the generator in --watch
mode:
npx electric-sql generate --watch
This monitors the sync service to pick up on database schema changes and automatically re-generate the client whenever the DDL schema or DDLX rules change.