Skip to content

API Reference

Creates a new isolated in-memory database instance.

OptionTypeDefaultDescription
rowMode'object' | 'array''object'Default row format for SELECT results

Executes one or more SQL statements separated by ;. Returns an array of results.

OptionTypeDefaultDescription
rowMode'object' | 'array'constructor defaultRow format for this call
const [{ rows, fields }] = db.execute('SELECT * FROM users');

Creates a prepared statement with $1, $2, … parameter placeholders. Returns a statement object with an execute(params, options?) method.

const stmt = db.prepare('SELECT * FROM users WHERE id = $1');
const [{ rows }] = stmt.execute([42]);
// With options
const [{ rows }] = stmt.execute([42], { rowMode: 'array' });
interface ConnectSQLOptions {
rowMode?: 'object' | 'array';
}
interface ExecuteOptions {
rowMode?: 'object' | 'array';
}
class ConnectSQL {
constructor(options?: ConnectSQLOptions)
execute(sql: string, options?: ExecuteOptions): ExecuteResult[]
}

Every result has a command field for discrimination:

// DDL
{ command: 'create table', table: string }
{ command: 'drop table', table: string }
{ command: 'create type', type: string }
{ command: 'drop type', type: string }
{ command: 'create index', index: string }
{ command: 'drop index', index: string }
{ command: 'truncate table', table: string }
{ command: 'alter table' }
// DML
{ command: 'insert', result: Record<string, any> }
{ command: 'select', rows: T[], fields: { name: string, dataType: string }[] }
{ command: 'update', rows: number }
{ command: 'delete', rows: number }
// Transactions
{ command: 'begin' }
{ command: 'commit' }
{ command: 'rollback' }
// Prepared statements
{ command: 'prepare', name: string }
{ command: 'deallocate', name: string }
SQL TypeJavaScript Type
INT, BIGINT, DOUBLE, NUMERICnumber
TEXT, CHAR, VARCHARstring
BOOLEANboolean
UUIDstring
TIMESTAMPDate
ENUMstring (label)
JSON arrayArray
JSON objectObject
NULLnull

import io.github.edadma.petradb.*
given Session = new MemoryDB().connect()
import io.github.edadma.petradb.*
// Create new
val db = PersistentDB.create("path/to/db", pageSize = 4096)
given Session = db.connect()
// Reopen existing
val db = PersistentDB.open("path/to/db")
given Session = db.connect()
// Close when done
db.close()

executeSQL(sql: String)(using Session): Seq[Result]

Section titled “executeSQL(sql: String)(using Session): Seq[Result]”

Executes one or more semicolon-separated SQL statements and returns a sequence of results.

val results: Seq[Result] = executeSQL("SELECT * FROM users")
sealed trait Result
case class QueryResult(table: TableValue) extends Result
case class InsertResult(obj: Map[String, Value], table: TableValue) extends Result
case class CreateTableResult(table: String) extends Result
case class DropTableResult(table: String) extends Result
case class TruncateResult(table: String) extends Result
case class UpdateResult(rows: Int) extends Result
case class DeleteResult(rows: Int) extends Result
val QueryResult(table) = executeQuery("SELECT * FROM users")
// Access rows
val rows: IndexedSeq[Row] = table.data
for (row <- table.data) {
val id: Int = row.getInt("id")
val name: String = row.getString("name")
val email: Option[String] = row.getStringOption("email")
}
val row: Row = table.data.head
// Type-safe extraction
val id: Int = row.getInt("id")
val name: String = row.getString("name")
val email: Option[String] = row.getStringOption("email")
val isActive: Boolean = row.getBoolean("is_active")
// Direct access
val value: Value = row("column_name")