Aqueduct ORM Snippets

Filter Query by Column/Property (WHERE clause)

var query = new Query<Employee>()
  ..where((e) => e.title).equalTo("Programmer");
var employees = await query.fetch();

Fetching Only Some Columns/Properties

var query = new Query<Employee>()
  ..resultingProperties((e) => [e.id, e.name]);
var employees = await query.fetch();

Sorting Rows/Objects

var query = new Query<Employee>()
  ..sortBy((e) => e.salary, QuerySortOrder.ascending);
var employees = await query.fetch();

Fetching Only One Row/Object

var query = new Query<Employee>()
  ..where((e) => e.id).equalTo(1);
var employee = await query.fetchOne();

Executing a Join (Has-One)

var query = new Query<Team>()
  ..join(object: (e) => e.league);
var teamsAndTheirLeague = await query.fetch();

Executing a Join (Has-Many)

var query = new Query<Team>()
  ..join(set: (e) => e.players);
var teamsAndTheirPlayers = await query.fetch();

Filtering Joined Rows/Objects

var query = new Query<Team>();

var subquery = query.join(set: (e) => e.players)
  ..where((p) => p.yearsPlayed).lessThanOrEqualTo(1);

var teamsAndTheirRookiePlayers = await query.fetch();

Filter Rows/Objects by Relationship Property

var query = new Query<Team>()
  ..where((t) => t.players.haveAtLeastOneWhere.yearsPlayed).lessThanOrEqualTo(1);

var teamsWithRookies = await query.fetch();

Complex/Unsupported WHERE Clause (using 'OR')

var query = new Query<Team>()
  ..predicate = new QueryPredicate("name = '@name1' OR name = '@name2'", {
      "name1": "Badgers",
      "name2": "Gophers"
    });

var badgerAndGopherTeams = await query.fetch();

Updating a Row/Object

var query = new Query<Team>()
  ..where((t) => t.id).equalTo(10)
  ..values.name = "Badgers";

var team = await query.updateOne();

Configure a Database Connection from Configuration File

class AppChannel extends ApplicationChannel {
  @override
  Future prepare() async {
    context = contextWithConnectionInfo(options.configurationFilePath.database);
  }

  ManagedContext context;

  @override
  Controller get entryPoint {
    final router = new Router();
    ...
    return router;
  }

  ManagedContext contextWithConnectionInfo(
      DatabaseConnectionConfiguration connectionInfo) {
    var dataModel = new ManagedDataModel.fromCurrentMirrorSystem();
    var psc = new PostgreSQLPersistentStore.fromConnectionInfo(
        connectionInfo.username,
        connectionInfo.password,
        connectionInfo.host,
        connectionInfo.port,
        connectionInfo.databaseName);

    return new ManagedContext(dataModel, psc);
  }
}

class MyAppConfiguration extends ConfigurationItem {
  MyAppConfiguration(String fileName) : super.fromFile(fileName);

  DatabaseConnectionConfiguration database;
}