Skip to main content

Generator script

To interface with your database from within a JavaScript or TypeScript application you need an Electric client (see Usage -> Data access -> Client for context). To generate an Electric client, make sure that your database and the Electric sync service are up and running. Then, from within the root folder of your application run:

npx electric-sql generate

This will download all migrations from the database, bundle them with your application, and generate the Electric client. Note that you can use an optional --watch flag to automatically re-generate the client on every database migration.


The generate script accepts a number of arguments:

npx electric-sql generate [--service <url>] [--out <path>] [--watch [<pollingInterval>]]

All arguments are optional and are described below:

--service<url>Provides the url to connect to the Electric sync service. If not provided, it uses the url set in the ELECTRIC_URL environment variable. If that variable is not set, it resorts to the default url which is http://localhost:5133.
--out<path>Specifies where to output the generated client. Defaults to ./src/generated/client
--watch<pollingInterval>Run the generator in watch mode. Accepts an optional polling interval (in milliseconds) which defines how often to poll Electric for database migrations. The default polling interval is 1000ms.

Note that the --watch flag can be used with or without a polling interval:

npx electric-sql --watch
# or with an explicit polling interval
npx electric-sql --watch 5000