Skip to main content

Persist Tool

The bal persist tool generates type-safe client APIs for data persistence across multiple data stores. You define your data model using Ballerina record types, and the tool generates the client code to perform CRUD operations without writing store-specific queries.

Supported data stores

Data storeModule
In-memory tablesBuilt-in
MySQLballerinax/persist.sql
PostgreSQLballerinax/persist.sql
MSSQLballerinax/persist.sql
Google Sheetsballerinax/persist.googlesheets
Redisballerinax/persist.redis

Connect to a database

The Visual Designer provides a guided wizard to connect to an existing database, introspect its tables, and generate a type-safe connection.

Step 1: Add a connection

  1. In the Artifacts page, click Connection under Other Artifacts.

  2. In the Add Connection panel, select Connect to a Database. The supported database systems are MySQL, MSSQL, and PostgreSQL.

    Add connection panel

Step 2: Introspect database

  1. Select the Database System (MySQL, MSSQL, or PostgreSQL).

  2. Enter the database credentials:

    • Host — Database server host address.
    • Port — Database server port (for example, 3306 for MySQL).
    • Database — Name of the database to connect.
    • User — Database username.
    • Password — Database user password.
  3. Click Connect & Introspect Database.

    Introspect database credentials

Step 3: Select tables

  1. Choose which tables to include in this connector. You can search for tables or click Select All.

  2. Click Continue to Connection Details.

    Select database tables

Step 4: Create connection

  1. Enter a Connection Name (for example, MySQLDatabase).

  2. Review the Connection Configurables — configurable variables are generated for the connection host, port, username, password, and database name with default values.

  3. Click Save.

    Connection name and configurables

Working with the connection

Once the connection is saved, it appears in the design view as a connection artifact.

Design view showing database connection

Click Edit on the connection to update connection details. From the edit panel, you can:

  • Click Edit Connector to modify the underlying database connector.

  • Click View ER Diagram to visualize the entity relationships between the selected tables.

  • Update connection settings such as Host, Port, User, Password, and Database.

    Edit connection panel

Use connection functions in integration logic

When designing integration logic in the flow diagram, the connection functions appear in the Connections panel. These provide pre-built CRUD operations for each selected table, such as:

  • Get rows / Get row

  • Insert rows

  • Update row

  • Delete row

    Connection functions in flow designer

Each function provides advanced configuration options where you can set Where Clause, Order By Clause, Limit Clause, Result variable name, and Target Type to select specific fields from the table.

Migrate the schema

Create database migration scripts based on changes to the data model. This command compares the current data model with the previous state and generates incremental migration scripts.

bal persist migrate --datastore <datastore> --module <module> <migration-label>
Argument / FlagRequiredDefaultDescription
migration-labelYesA descriptive label for the migration (for example, add_email_column)
--datastoreYesTarget data store: mysql, mssql, or postgresql
--moduleYesName of the module containing the data model
# Create a migration after modifying the data model
bal persist migrate --datastore mysql --module db add_phone_column

The command generates a timestamped migration directory under persist/migrations/:

persist/
migrations/
20240115120000_add_phone_column/
script.sql # Incremental SQL migration script
model.bal # Snapshot of the data model at this point

Push the schema

Pushes the current data model schema directly to the target data store. Useful for development and testing environments where migration tracking is not needed.

bal persist push --datastore <datastore> --module <module>
FlagRequiredDefaultDescription
--datastoreYesTarget data store: mysql, mssql, postgresql, or sqlite
--moduleYesName of the module containing the data model
warning

bal persist push drops and recreates tables. Use this only in development environments. For production, use bal persist migrate.

Command reference

bal persist init

bal persist init --datastore <datastore> --module <module>
FlagRequiredDefaultDescription
--datastoreNoinmemoryTarget data store: mysql, mssql, postgresql, sqlite, googlesheets, redis, inmemory
--moduleNoName of the submodule for persistence definitions

bal persist generate

bal persist generate --datastore <datastore> --module <module>
FlagRequiredDefaultDescription
--datastoreYesTarget data store: mysql, mssql, postgresql, sqlite, googlesheets, redis, inmemory
--moduleYesName of the module containing the data model

bal persist migrate

bal persist migrate --datastore <datastore> --module <module> <migration-label>
FlagRequiredDefaultDescription
--datastoreYesTarget data store: mysql, mssql, or postgresql
--moduleYesName of the module containing the data model
migration-labelYesDescriptive label for the migration

bal persist push

bal persist push --datastore <datastore> --module <module>
FlagRequiredDefaultDescription
--datastoreYesTarget data store: mysql, mssql, postgresql, or sqlite
--moduleYesName of the module containing the data model

Data store migration support

Data store--datastore valueMigrationsNotes
MySQLmysqlYesFull SQL support
Microsoft SQL ServermssqlYesFull SQL support
PostgreSQLpostgresqlYesFull SQL support
SQLitesqliteNoFile-based, no migration support
Google SheetsgooglesheetsNoRequires OAuth2 configuration
RedisredisNoKey-value store
In-memoryinmemoryNoFor testing and prototyping

What's next