platform/packages/orm/angel_orm/example/main.dart

54 lines
1.3 KiB
Dart
Raw Normal View History

2018-12-08 02:03:03 +00:00
import 'dart:async';
2022-01-08 05:41:02 +00:00
//import 'package:angel3_migration/angel3_migration.dart';
2021-05-16 15:11:12 +00:00
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_serialize/angel3_serialize.dart';
2021-05-04 15:03:15 +00:00
import 'package:optional/optional.dart';
2018-12-01 17:21:34 +00:00
part 'main.g.dart';
2021-05-02 07:32:24 +00:00
void main() async {
2019-07-04 21:30:21 +00:00
var query = EmployeeQuery()
2021-05-02 07:32:24 +00:00
..where?.firstName.equals('Rich')
..where?.lastName.equals('Person')
..where?.raw('COM.deleted = false')
2021-05-04 04:18:39 +00:00
..orWhere((w) => w.salary.greaterThanOrEqualTo(75000))
..join('companies', 'company_id', 'id', alias: 'COM');
2018-12-01 18:23:50 +00:00
2019-07-04 21:30:21 +00:00
var richPerson = await query.getOne(_FakeExecutor());
2021-05-03 13:27:12 +00:00
if (richPerson.isPresent) {
print(richPerson.first.toJson());
}
}
2018-12-01 17:21:34 +00:00
class _FakeExecutor extends QueryExecutor {
const _FakeExecutor();
2018-08-24 12:30:38 +00:00
2018-12-01 17:21:34 +00:00
@override
Future<List<List>> query(
2021-05-02 07:32:24 +00:00
String tableName, String? query, Map<String, dynamic> substitutionValues,
2021-05-03 13:27:12 +00:00
[returningFields = const []]) async {
2019-07-04 21:30:21 +00:00
var now = DateTime.now();
print(
'_FakeExecutor received query: $query and values: $substitutionValues');
2018-12-01 17:21:34 +00:00
return [
[1, now, now, 'Rich', 'Person', 100000.0]
2018-12-01 17:21:34 +00:00
];
}
2018-12-08 02:03:03 +00:00
@override
Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) {
2019-07-04 21:30:21 +00:00
throw UnsupportedError('Transactions are not supported.');
2018-12-08 02:03:03 +00:00
}
}
2018-12-01 17:21:34 +00:00
@orm
@serializable
2018-08-24 12:30:38 +00:00
abstract class _Employee extends Model {
2021-05-02 07:32:24 +00:00
String? get firstName;
2021-05-02 07:32:24 +00:00
String? get lastName;
2021-05-02 07:32:24 +00:00
double? get salary;
2018-12-01 17:21:34 +00:00
}