PikaORM Web QuickStart
This page is your one-stop shop for getting started with Pika ๐! The writing convention is to write descriptions and references to code below code blocks unless specified otherwise.
You are encouraged to play around to get comfortable with the method-builder design style we have chosen. Almost everything can be stumbled into with Pika, and all core elements of the API use an object method to execute features โ no external configs.
Getting your project migrated
Below is a standard implementation of a migration setup done entirely in code. We use simple extension classes to allow users to customize their migration setup and have full control over migrations with method tools.
public class MigrationsFile1 extends PikaORM.Migrations {
@Override
public void migrations() {
add(this::addMigrationDemoTable);
}
public PikaMigration addMigrationDemoTable() {
return makeMigration("migration1")
.up("""
CREATE TABLE IF NOT EXISTS migration_demo_models (
id INTEGER PRIMARY KEY,
str_val TEXT,
int_val INTEGER,
bool_val BOOLEAN,
date_val DATETIME
);
""")
.down("""
DROP TABLE migration_demo_models;
""");
}
}
Let's break this down ๐ง. We override the migrations() method which is left blank in the ORM by default,
so we can add each new migration as a PikaMigration object โ enabling us to easily
manipulate our migrations with methods when we need to.
Default Database Mapping
You probably notice a discrepancy in the naming conventions between the POJO model names and our migration DDL. Not to fret! We have a default mapping that follows a standard database format:
ClassName→class_namefieldName→field_nameid→id(column)
Basic CRUD with PikaORM
public static void main(String[] args) throws Exception {
// init the ORM
PikaORM orm = new PikaORM("jdbc:sqlite:web.db")
.withLogLevel(TRACE)
.makeDefaultORM()
.withMigrations(new WebAppMigrations())
.applyMigrations();
// insert some data
orm.insertAll(
new Todo("Todo 1", "This is todo 1"),
new Todo("Todo 2", "This is todo 2"),
new Todo("Todo 3", "This is todo 3")
);
orm.insert(new Todo("Todo 4", "This is todo 4"));
// Querying data
var query = orm.query(Todo.class)
.where("title = :val")
.withVar("val", "Todo 1");
PikaList<Todo> result = query.toList();
for (Todo todo : result) {
System.out.println(todo.getTitle());
}
}
The incredibly helpful toList() knows our class from the query we issued, and grabs the
map and proper formatting to turn all the rows returned by the DB into our Todo objects.
Joins in PikaORM
Last but not least โ one more essential aspect to a good ORM.
String title = "Learn about PikaORM";
PikaList<Todo> result = orm.query(Todo.class)
.join(Checklist.class)
.thenJoin(Calendar.class)
.where("Todo.Title = :title")
.withVar("title", title)
.fetchList();
Imagining we have another table and class of Checklist and Calendar we can
easily join the tables with an additional method using our query method. We can
subsequently chain any other connected tables using thenJoin.