Conduit ORM Snippets

Filter Query by Column/Property (WHERE clause)

1
var query = new Query<Employee>(context)
2
..where((e) => e.title).equalTo("Programmer");
3
var employees = await query.fetch();
Copied!

Fetching Only Some Columns/Properties

1
var query = new Query<Employee>(context)
2
..resultingProperties((e) => [e.id, e.name]);
3
var employees = await query.fetch();
Copied!

Sorting Rows/Objects

1
var query = new Query<Employee>(context)
2
..sortBy((e) => e.salary, QuerySortOrder.ascending);
3
var employees = await query.fetch();
Copied!

Fetching Only One Row/Object

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

Executing a Join (Has-One)

1
var query = new Query<Team>(context)
2
..join(object: (e) => e.league);
3
var teamsAndTheirLeague = await query.fetch();
Copied!

Executing a Join (Has-Many)

1
var query = new Query<Team>(context)
2
..join(set: (e) => e.players);
3
var teamsAndTheirPlayers = await query.fetch();
Copied!

Filtering Joined Rows/Objects

1
var query = new Query<Team>(context);
2
3
var subquery = query.join(set: (e) => e.players)
4
..where((p) => p.yearsPlayed).lessThanOrEqualTo(1);
5
6
var teamsAndTheirRookiePlayers = await query.fetch();
Copied!

Filter Rows/Objects by Relationship Property

1
var query = new Query<Team>(context)
2
..where((t) => t.players.haveAtLeastOneWhere.yearsPlayed).lessThanOrEqualTo(1);
3
4
var teamsWithRookies = await query.fetch();
Copied!

Complex/Unsupported WHERE Clause (using 'OR')

1
var query = new Query<Team>(context)
2
..predicate = new QueryPredicate("name = @name1 OR name = @name2", {
3
"name1": "Badgers",
4
"name2": "Gophers"
5
});
6
7
var badgerAndGopherTeams = await query.fetch();
Copied!

Updating a Row/Object

1
var query = new Query<Team>(context)
2
..where((t) => t.id).equalTo(10)
3
..values.name = "Badgers";
4
5
var team = await query.updateOne();
Copied!

Configure a Database Connection from Configuration File

1
class AppChannel extends ApplicationChannel {
2
@override
3
Future prepare() async {
4
context = contextWithConnectionInfo(options.configurationFilePath.database);
5
}
6
7
ManagedContext context;
8
9
@override
10
Controller get entryPoint {
11
final router = new Router();
12
...
13
return router;
14
}
15
16
ManagedContext contextWithConnectionInfo(
17
DatabaseConnectionConfiguration connectionInfo) {
18
var dataModel = new ManagedDataModel.fromCurrentMirrorSystem();
19
var psc = new PostgreSQLPersistentStore(
20
connectionInfo.username,
21
connectionInfo.password,
22
connectionInfo.host,
23
connectionInfo.port,
24
connectionInfo.databaseName);
25
26
return new ManagedContext(dataModel, psc);
27
}
28
}
29
30
class MyAppConfiguration extends Configuration {
31
MyAppConfiguration(String fileName) : super.fromFile(File(fileName));
32
33
DatabaseConnectionConfiguration database;
34
}
Copied!
Last modified 6mo ago