Client Reference

The orm:Client is the database execution boundary in Ballerina ORM.

Type: 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 = ();
|};

provider and URL/host fields are used to normalize connectivity for MySQL and PostgreSQL.

Constructor

ballerina
public function init(ClientConfig config) returns error?

Core methods

  • getConfig()
ballerina
() returns NormalizedClientConfig
  • getNativeClient()
ballerina
() returns NativeDbClient
  • query(plan)
ballerina
(QueryPlan) returns stream<record {}, sql:Error?>|SchemaError|ClientError|sql:Error
  • execute(plan)
ballerina
(QueryPlan) returns sql:ExecutionResult|SchemaError|ClientError|sql:Error
  • rawQuery(text, params)
ballerina
(string, anydata[] = []) returns stream<record {}, sql:Error?>|ClientError|sql:Error
  • rawExecute(text, params)
ballerina
(string, anydata[] = []) returns int|ClientError|sql:Error
  • model(modelType) and 'from(modelType)
ballerina
(typedesc<anydata>) returns ExecutingQueryBuilder
  • close()
ballerina
() returns error?

Error shape

ClientError uses the following detail payload:

ballerina
public type ClientErrorDetail record {|
    string code;
    string message;
    string? fieldName = ();
|};

Example

ballerina
orm:Client db = check new ({
    provider: orm:POSTGRESQL,
    host: "localhost",
    user: "postgres",
    password: "password",
    database: "myapp"
});

var rows = check db.'from(User).findMany();

check db.close();