ORM Client

orm:Client is the entry point for database operations.

What the client wraps

Ballerina ORM wraps the official Ballerina SQL drivers:

For advanced connectivity options, refer to those docs for:

  • mysql:Options
  • postgresql:Options
  • sql:ConnectionPool

ClientConfig

ballerina
public type ClientConfig record {|
    Engine? provider = ();
    string? url = ();
    string? host = ();
    int? port = ();
    string? user = ();
    string? password = ();
    string? database = ();
    mysql:Options? mysqlOptions = ();
    postgresql:Options? postgresqlOptions = ();
    sql:ConnectionPool? connectionPool = ();
|};

Initialize with explicit fields

ballerina
orm:Client db = check new ({
    provider: orm:MYSQL,
    host: "localhost",
    port: 3306,
    user: "root",
    password: "password",
    database: "myapp"
});

Initialize with URL

ballerina
orm:Client db = check new ({
    url: "postgresql://postgres:password@localhost:5432/myapp"
});

Start queries

ballerina
var rows = check db.'from(User)
    .'where({status: {equals: "ACTIVE"}})
    .findMany();

Equivalent builder entry:

ballerina
var rows = check db.model(User)
    .'where({status: {equals: "ACTIVE"}})
    .findMany();

Raw SQL escape hatches

ballerina
stream<record {}, error?>|error rowStream = db.rawQuery(
    "SELECT * FROM users WHERE status = ?",
    ["ACTIVE"]
);

int|error affected = db.rawExecute(
    "UPDATE users SET status = ? WHERE id = ?",
    ["ACTIVE", 101]
);

Lifecycle

ballerina
check db.close();

Next step

Continue with Query Builder.