Database / ORM Overview

Models

Archery includes a lightweight, ultra-flexible Object Relational Mapper (ORM) that makes interacting with your database a breeze.

Archery's ORM is uniquely designed with a multi-driver architecture. Out of the box, it supports true relational databases like PostgreSQL and SQLite, while also fully supporting server-less JSON storage mechanisms like local file drops and AWS S3 objects.

The Model

Every database entity should extend the Archery Model core class. The core Model class supplies common properties like id and uuid, as well as standard timestamp metadata (createdAt and updatedAt).

When creating a model, you simply need to define its specific payload properties, and instruct the framework how to serialize the model back into a raw JSON map for storage:

class Flight extends Model {
  String name;
  String destination;

  Flight({required this.name, required this.destination});

  @override
  Map<String, dynamic> toJson() => {
    'name': name,
    'destination': destination,
  };
}

Basic Operations

The Archery ORM leverages Dart's powerful static types to deduce the tables and storage schemas for your classes under the hood automatically. Simply invoke the static core modifiers, passing your model Type as the generic argument.

Retrieving Records

// Fetch all models
final flights = await Model.all<Flight>();

// Fetch by primary key or UUID
final user = await Model.find<User>(id: 1);

// Filter records
final activeFlights = await Model.where<Flight>(
  field: 'status', 
  value: 'active'
);

// Fetch a single record
final flight = await Model.firstWhere<Flight>(
  field: 'name', 
  value: 'Flight 10', 
  comp: '=='
);

Storing & Updating Records

final flight = Flight(name: 'Flight 10', destination: 'JFK');

// Save the model to the database
await flight.save();

// Update specific fields intelligently
await flight.update(withJson: {'destination': 'LAX'});

// Delete the model
await flight.delete();