Scala Usage
Installation
Section titled “Installation”Add to your build.sbt:
libraryDependencies += "io.github.edadma" %%% "petradb-engine" % "1.0.0"The %%% operator selects the correct artifact for your platform (JVM, Scala.js, or Scala Native).
In-Memory Database
Section titled “In-Memory Database”import io.github.edadma.petradb.*
given Session = new MemoryDB().connect()
val results = executeSQL(""" CREATE TABLE products ( id SERIAL, name TEXT NOT NULL, price NUMERIC(10,2), category TEXT );
INSERT INTO products (name, price, category) VALUES ('Laptop', 999.99, 'Electronics'), ('Coffee', 4.50, 'Food'), ('Book', 19.99, 'Education');
SELECT category, COUNT(*), AVG(price) FROM products GROUP BY category ORDER BY category;""")
results.foreach(println)Each MemoryDB instance is isolated and independent. All data lives in memory.
Persistent Database
Section titled “Persistent Database”PetraDB supports crash-safe durable storage on JVM and Native via stow.
Creating a New Database
Section titled “Creating a New Database”import io.github.edadma.petradb.*
val db = PersistentDB.create("mydata.db", 4096)given Session = db.connect()
executeSQL(""" CREATE TABLE users ( id SERIAL, name TEXT NOT NULL, email TEXT, PRIMARY KEY (id) );
INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');""")
db.close()Reopening an Existing Database
Section titled “Reopening an Existing Database”val db = PersistentDB.open("mydata.db")given Session = db.connect()
val results = executeSQL("SELECT * FROM users")results.foreach(println)
db.close()All tables, data, enum types, and auto-increment state are restored when you reopen.
Persistent databases use copy-on-write pages and double-buffered headers for crash safety. All DDL and DML operations are durable.
Executing SQL
Section titled “Executing SQL”executeSQL(sql) runs one or more semicolon-separated statements and returns a Seq[Result].
val results: Seq[Result] = executeSQL("SELECT * FROM users; SELECT * FROM products;")Result Types
Section titled “Result Types”sealed trait Resultcase class QueryResult(table: TableValue) extends Resultcase class InsertResult(obj: Map[String, Value], table: TableValue) extends Resultcase class CreateTableResult(table: String) extends Resultcase class DropTableResult(table: String) extends Resultcase class TruncateResult(table: String) extends Resultcase class UpdateResult(rows: Int) extends Resultcase class DeleteResult(rows: Int) extends ResultAccessing Query Data
Section titled “Accessing Query Data”val QueryResult(table) = executeQuery("SELECT * FROM users")
// Access rowsval 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")}Testing
Section titled “Testing”# Run tests for all platformssbt test
# Run JavaScript tests onlysbt engineJS/test
# Run JVM tests onlysbt engineJVM/test
# Run Native tests onlysbt engineNative/testPlatform Notes
Section titled “Platform Notes”- JVM — thread-safe, integrates with Spring Boot, Play Framework, Akka, etc.
- Scala.js — runs in Node.js and browsers; no persistent storage
- Scala Native — compiles to native executables; ideal for CLI tools and embedded systems