Add examples for gen
This commit is contained in:
parent
b62f26a0af
commit
f2dce78104
3 changed files with 247 additions and 0 deletions
112
angel_orm_generator/example/main.dart
Normal file
112
angel_orm_generator/example/main.dart
Normal file
|
@ -0,0 +1,112 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:angel_model/angel_model.dart';
|
||||
import 'package:angel_orm/angel_orm.dart';
|
||||
import 'package:angel_orm/src/query.dart';
|
||||
import 'package:angel_serialize/angel_serialize.dart';
|
||||
part 'main.g.dart';
|
||||
part 'main.serializer.g.dart';
|
||||
|
||||
main() async {
|
||||
var query = new EmployeeQuery()
|
||||
..where.firstName.equals('Rich')
|
||||
..where.lastName.equals('Person')
|
||||
..orWhere((w) => w.salary.greaterThanOrEqualTo(75000))
|
||||
..join('companies', 'company_id', 'id');
|
||||
|
||||
var richPerson = await query.getOne(new _FakeExecutor());
|
||||
print(richPerson.toJson());
|
||||
}
|
||||
|
||||
class _FakeExecutor extends QueryExecutor {
|
||||
const _FakeExecutor();
|
||||
|
||||
@override
|
||||
Future<List<List>> query(
|
||||
String query, Map<String, dynamic> substitutionValues,
|
||||
[returningFields]) async {
|
||||
var now = new DateTime.now();
|
||||
print(
|
||||
'_FakeExecutor received query: $query and values: $substitutionValues');
|
||||
return [
|
||||
[1, 'Rich', 'Person', 100000.0, now, now]
|
||||
];
|
||||
}
|
||||
|
||||
@override
|
||||
Future<T> transaction<T>(FutureOr<T> Function() f) {
|
||||
throw new UnsupportedError('Transactions are not supported.');
|
||||
}
|
||||
}
|
||||
|
||||
@orm
|
||||
@serializable
|
||||
abstract class _Employee extends Model {
|
||||
String get firstName;
|
||||
|
||||
String get lastName;
|
||||
|
||||
double get salary;
|
||||
}
|
||||
|
||||
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
|
||||
@override
|
||||
final QueryValues values = new MapQueryValues();
|
||||
|
||||
EmployeeQueryWhere _where;
|
||||
|
||||
EmployeeQuery() {
|
||||
_where = new EmployeeQueryWhere(this);
|
||||
}
|
||||
|
||||
@override
|
||||
EmployeeQueryWhere get where => _where;
|
||||
|
||||
@override
|
||||
String get tableName => 'employees';
|
||||
|
||||
@override
|
||||
List<String> get fields =>
|
||||
['id', 'first_name', 'last_name', 'salary', 'created_at', 'updated_at'];
|
||||
|
||||
@override
|
||||
EmployeeQueryWhere newWhereClause() => new EmployeeQueryWhere(this);
|
||||
|
||||
@override
|
||||
Employee deserialize(List row) {
|
||||
return new Employee(
|
||||
id: row[0].toString(),
|
||||
firstName: row[1] as String,
|
||||
lastName: row[2] as String,
|
||||
salary: row[3] as double,
|
||||
createdAt: row[4] as DateTime,
|
||||
updatedAt: row[5] as DateTime);
|
||||
}
|
||||
}
|
||||
|
||||
class EmployeeQueryWhere extends QueryWhere {
|
||||
EmployeeQueryWhere(EmployeeQuery query)
|
||||
: id = new NumericSqlExpressionBuilder(query, 'id'),
|
||||
firstName = new StringSqlExpressionBuilder(query, 'first_name'),
|
||||
lastName = new StringSqlExpressionBuilder(query, 'last_name'),
|
||||
salary = new NumericSqlExpressionBuilder(query, 'salary'),
|
||||
createdAt = new DateTimeSqlExpressionBuilder(query, 'created_at'),
|
||||
updatedAt = new DateTimeSqlExpressionBuilder(query, 'updated_at');
|
||||
|
||||
@override
|
||||
Iterable<SqlExpressionBuilder> get expressionBuilders {
|
||||
return [id, firstName, lastName, salary, createdAt, updatedAt];
|
||||
}
|
||||
|
||||
final NumericSqlExpressionBuilder<int> id;
|
||||
|
||||
final StringSqlExpressionBuilder firstName;
|
||||
|
||||
final StringSqlExpressionBuilder lastName;
|
||||
|
||||
final NumericSqlExpressionBuilder<double> salary;
|
||||
|
||||
final DateTimeSqlExpressionBuilder createdAt;
|
||||
|
||||
final DateTimeSqlExpressionBuilder updatedAt;
|
||||
}
|
71
angel_orm_generator/example/main.g.dart
Normal file
71
angel_orm_generator/example/main.g.dart
Normal file
|
@ -0,0 +1,71 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'main.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonModelGenerator
|
||||
// **************************************************************************
|
||||
|
||||
@generatedSerializable
|
||||
class Employee extends _Employee {
|
||||
Employee(
|
||||
{this.id,
|
||||
this.firstName,
|
||||
this.lastName,
|
||||
this.salary,
|
||||
this.createdAt,
|
||||
this.updatedAt});
|
||||
|
||||
@override
|
||||
final String id;
|
||||
|
||||
@override
|
||||
final String firstName;
|
||||
|
||||
@override
|
||||
final String lastName;
|
||||
|
||||
@override
|
||||
final double salary;
|
||||
|
||||
@override
|
||||
final DateTime createdAt;
|
||||
|
||||
@override
|
||||
final DateTime updatedAt;
|
||||
|
||||
Employee copyWith(
|
||||
{String id,
|
||||
String firstName,
|
||||
String lastName,
|
||||
double salary,
|
||||
DateTime createdAt,
|
||||
DateTime updatedAt}) {
|
||||
return new Employee(
|
||||
id: id ?? this.id,
|
||||
firstName: firstName ?? this.firstName,
|
||||
lastName: lastName ?? this.lastName,
|
||||
salary: salary ?? this.salary,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
updatedAt: updatedAt ?? this.updatedAt);
|
||||
}
|
||||
|
||||
bool operator ==(other) {
|
||||
return other is _Employee &&
|
||||
other.id == id &&
|
||||
other.firstName == firstName &&
|
||||
other.lastName == lastName &&
|
||||
other.salary == salary &&
|
||||
other.createdAt == createdAt &&
|
||||
other.updatedAt == updatedAt;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return hashObjects([id, firstName, lastName, salary, createdAt, updatedAt]);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return EmployeeSerializer.toMap(this);
|
||||
}
|
||||
}
|
64
angel_orm_generator/example/main.serializer.g.dart
Normal file
64
angel_orm_generator/example/main.serializer.g.dart
Normal file
|
@ -0,0 +1,64 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'main.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// SerializerGenerator
|
||||
// **************************************************************************
|
||||
|
||||
abstract class EmployeeSerializer {
|
||||
static Employee fromMap(Map map) {
|
||||
return new Employee(
|
||||
id: map['id'] as String,
|
||||
firstName: map['first_name'] as String,
|
||||
lastName: map['last_name'] as String,
|
||||
salary: map['salary'] as double,
|
||||
createdAt: map['created_at'] != null
|
||||
? (map['created_at'] is DateTime
|
||||
? (map['created_at'] as DateTime)
|
||||
: DateTime.parse(map['created_at'].toString()))
|
||||
: null,
|
||||
updatedAt: map['updated_at'] != null
|
||||
? (map['updated_at'] is DateTime
|
||||
? (map['updated_at'] as DateTime)
|
||||
: DateTime.parse(map['updated_at'].toString()))
|
||||
: null);
|
||||
}
|
||||
|
||||
static Map<String, dynamic> toMap(Employee model) {
|
||||
if (model == null) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
'id': model.id,
|
||||
'first_name': model.firstName,
|
||||
'last_name': model.lastName,
|
||||
'salary': model.salary,
|
||||
'created_at': model.createdAt?.toIso8601String(),
|
||||
'updated_at': model.updatedAt?.toIso8601String()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
abstract class EmployeeFields {
|
||||
static const List<String> allFields = const <String>[
|
||||
id,
|
||||
firstName,
|
||||
lastName,
|
||||
salary,
|
||||
createdAt,
|
||||
updatedAt
|
||||
];
|
||||
|
||||
static const String id = 'id';
|
||||
|
||||
static const String firstName = 'first_name';
|
||||
|
||||
static const String lastName = 'last_name';
|
||||
|
||||
static const String salary = 'salary';
|
||||
|
||||
static const String createdAt = 'created_at';
|
||||
|
||||
static const String updatedAt = 'updated_at';
|
||||
}
|
Loading…
Reference in a new issue