service_commons.db
Modules
Module service_commons.db
Declarations
commons/service_commons.db
Functions
atomic
Runs work in a database transaction: committed when it returns a value, rolled back when it returns an error.
Services use it instead of their own transaction blocks, because each package that contains one starts the
transaction coordinator, and a second start in one program fails on the port.
Parameters
- work
function() returns (anydata|error)- The statements to run; clients it uses join the transaction
Return Type
- anydata|error - What
workreturned, or its error
connect
Opens a pooled JDBC client. The application supplies the driver, e.g. import ballerinax/h2.driver as _;.
Parameters
- config DbConfig - Connection settings
ident
function ident(string name) returns ParameterizedQueryReturns a query fragment holding an SQL identifier, for table names built from a validated prefix.
Parameters
- name string - The identifier; never pass user input
Return Type
- ParameterizedQuery - The fragment, to combine with
sql:queryConcat
isDuplicateKey
Reports whether an error is a unique or primary key violation (SQL state class 23).
Parameters
- err error - The error
Return Type
- boolean -
truefor an integrity constraint violation
migrate
Applies the migrations newer than the recorded schema version, each in order, and records them.
Parameters
- db Client - The client
- dbType DbType - Dialect, selecting
dialectStatements
- prefix string - The service's table prefix; the version table is
<prefix>schema_version
- migrations Migration[] - All of the service's migrations
Return Type
- error? - An error if a statement fails
raw
function raw(string statement) returns ParameterizedQueryReturns a query holding a literal SQL string with no parameters.
Parameters
- statement string - The SQL
Return Type
- ParameterizedQuery - The query
validatePrefix
Checks a table prefix so it can be spliced into SQL as an identifier.
Parameters
- prefix string - The prefix, e.g.
notification_
Return Type
- error? - An error if it is not a letter followed by letters, digits or underscores
Enums
service_commons.db: DbType
Databases every commons service supports.
Members
Records
service_commons.db: DbConfig
JDBC connection settings for a commons service.
Fields
- dbType DbType(default H2) - Database dialect; selects the migration scripts
- url string(default "jdbc:h2:mem:commons;DB_CLOSE_DELAY=-1") - JDBC URL
- user string(default "sa") - Database user
- password string(default "") - Database password
- maxOpenConnections int(default 10) - Connection pool size
- initSchema boolean(default true) - Runs the versioned migration scripts on startup
service_commons.db: Migration
One schema version of a service. Statements use {prefix} for the service's table prefix.
Fields
- version int - Version number; migrations run in ascending order, each once
- description string - What this version changes
- statements string[] - Statements for every dialect without an entry in
dialectStatements
Import
import commons/service_commons.db;Other versions
0.1.0
Metadata
Released date: 27 days ago
Version: 0.1.0
License: Apache-2.0
Compatibility
Platform: any
Ballerina version: 2201.13.4
GraalVM compatible: Yes
Pull count
Total: 2
Current verison: 2
Weekly downloads
Keywords
commons
service
auth
jdbc
sse
webhook
Contributors