Updated orm service

This commit is contained in:
thomashii 2021-08-12 11:39:01 +08:00
parent b65af20843
commit ae4e82f55b
7 changed files with 107 additions and 60 deletions

View file

@ -11,12 +11,13 @@ class EmployeeMigration extends Migration {
void up(Schema schema) { void up(Schema schema) {
schema.create('employees', (table) { schema.create('employees', (table) {
table.serial('id').primaryKey(); table.serial('id').primaryKey();
table.varChar('error', length: 256);
table.timeStamp('created_at'); table.timeStamp('created_at');
table.timeStamp('updated_at'); table.timeStamp('updated_at');
table.varChar('unique_id'); table.varChar('unique_id', length: 256).unique();
table.varChar('first_name'); table.varChar('first_name', length: 256);
table.varChar('last_name'); table.varChar('last_name', length: 256);
table.declare('salary', ColumnType('decimal')); table.declareColumn('salary', Column(type: ColumnType('decimal')));
}); });
} }
@ -31,8 +32,9 @@ class EmployeeMigration extends Migration {
// ************************************************************************** // **************************************************************************
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> { class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
EmployeeQuery({Set<String>? trampoline}) { EmployeeQuery({Query? parent, Set<String>? trampoline})
trampoline ??= {}; : super(parent: parent) {
trampoline ??= <String>{};
trampoline.add(tableName); trampoline.add(tableName);
_where = EmployeeQueryWhere(this); _where = EmployeeQueryWhere(this);
} }
@ -56,6 +58,7 @@ class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
List<String> get fields { List<String> get fields {
return const [ return const [
'id', 'id',
'error',
'created_at', 'created_at',
'updated_at', 'updated_at',
'unique_id', 'unique_id',
@ -75,30 +78,30 @@ class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
return EmployeeQueryWhere(this); return EmployeeQueryWhere(this);
} }
static Optional<Employee> parseRow(List row) { static Employee? parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) return null;
return Optional.empty();
}
var model = Employee( var model = Employee(
id: (row[0] as String?), id: row[0].toString(),
createdAt: (row[1] as DateTime?), error: (row[1] as String?),
updatedAt: (row[2] as DateTime?), createdAt: (row[2] as DateTime?),
uniqueId: (row[3] as String?), updatedAt: (row[3] as DateTime?),
firstName: (row[4] as String?), uniqueId: (row[4] as String?),
lastName: (row[5] as String?), firstName: (row[5] as String?),
salary: double.tryParse(row[6].toString())); lastName: (row[6] as String?),
return Optional.of(model); salary: double.tryParse(row[7].toString()));
return model;
} }
@override @override
Optional<Employee> deserialize(List row) { Optional<Employee> deserialize(List row) {
return parseRow(row); return Optional.ofNullable(parseRow(row));
} }
} }
class EmployeeQueryWhere extends QueryWhere { class EmployeeQueryWhere extends QueryWhere {
EmployeeQueryWhere(EmployeeQuery query) EmployeeQueryWhere(EmployeeQuery query)
: id = StringSqlExpressionBuilder(query, 'id'), : id = NumericSqlExpressionBuilder<int>(query, 'id'),
error = StringSqlExpressionBuilder(query, 'error'),
createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'),
updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'),
uniqueId = StringSqlExpressionBuilder(query, 'unique_id'), uniqueId = StringSqlExpressionBuilder(query, 'unique_id'),
@ -106,7 +109,9 @@ class EmployeeQueryWhere extends QueryWhere {
lastName = StringSqlExpressionBuilder(query, 'last_name'), lastName = StringSqlExpressionBuilder(query, 'last_name'),
salary = NumericSqlExpressionBuilder<double>(query, 'salary'); salary = NumericSqlExpressionBuilder<double>(query, 'salary');
final StringSqlExpressionBuilder id; final NumericSqlExpressionBuilder<int> id;
final StringSqlExpressionBuilder error;
final DateTimeSqlExpressionBuilder createdAt; final DateTimeSqlExpressionBuilder createdAt;
@ -122,7 +127,16 @@ class EmployeeQueryWhere extends QueryWhere {
@override @override
List<SqlExpressionBuilder> get expressionBuilders { List<SqlExpressionBuilder> get expressionBuilders {
return [id, createdAt, updatedAt, uniqueId, firstName, lastName, salary]; return [
id,
error,
createdAt,
updatedAt,
uniqueId,
firstName,
lastName,
salary
];
} }
} }
@ -137,6 +151,11 @@ class EmployeeQueryValues extends MapQueryValues {
} }
set id(String? value) => values['id'] = value; set id(String? value) => values['id'] = value;
String? get error {
return (values['error'] as String?);
}
set error(String? value) => values['error'] = value;
DateTime? get createdAt { DateTime? get createdAt {
return (values['created_at'] as DateTime?); return (values['created_at'] as DateTime?);
} }
@ -168,7 +187,7 @@ class EmployeeQueryValues extends MapQueryValues {
set salary(double? value) => values['salary'] = value.toString(); set salary(double? value) => values['salary'] = value.toString();
void copyFrom(Employee model) { void copyFrom(Employee model) {
id = model.id; error = model.error;
createdAt = model.createdAt; createdAt = model.createdAt;
updatedAt = model.updatedAt; updatedAt = model.updatedAt;
uniqueId = model.uniqueId; uniqueId = model.uniqueId;
@ -186,6 +205,7 @@ class EmployeeQueryValues extends MapQueryValues {
class Employee extends _Employee { class Employee extends _Employee {
Employee( Employee(
{this.id, {this.id,
this.error,
this.createdAt, this.createdAt,
this.updatedAt, this.updatedAt,
this.uniqueId, this.uniqueId,
@ -197,6 +217,9 @@ class Employee extends _Employee {
@override @override
String? id; String? id;
@override
String? error;
/// The time at which this item was created. /// The time at which this item was created.
@override @override
DateTime? createdAt; DateTime? createdAt;
@ -209,16 +232,17 @@ class Employee extends _Employee {
String? uniqueId; String? uniqueId;
@override @override
final String? firstName; String? firstName;
@override @override
final String? lastName; String? lastName;
@override @override
final double? salary; double? salary;
Employee copyWith( Employee copyWith(
{String? id, {String? id,
String? error,
DateTime? createdAt, DateTime? createdAt,
DateTime? updatedAt, DateTime? updatedAt,
String? uniqueId, String? uniqueId,
@ -227,6 +251,7 @@ class Employee extends _Employee {
double? salary}) { double? salary}) {
return Employee( return Employee(
id: id ?? this.id, id: id ?? this.id,
error: error ?? this.error,
createdAt: createdAt ?? this.createdAt, createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt, updatedAt: updatedAt ?? this.updatedAt,
uniqueId: uniqueId ?? this.uniqueId, uniqueId: uniqueId ?? this.uniqueId,
@ -239,6 +264,7 @@ class Employee extends _Employee {
bool operator ==(other) { bool operator ==(other) {
return other is _Employee && return other is _Employee &&
other.id == id && other.id == id &&
other.error == error &&
other.createdAt == createdAt && other.createdAt == createdAt &&
other.updatedAt == updatedAt && other.updatedAt == updatedAt &&
other.uniqueId == uniqueId && other.uniqueId == uniqueId &&
@ -249,13 +275,21 @@ class Employee extends _Employee {
@override @override
int get hashCode { int get hashCode {
return hashObjects( return hashObjects([
[id, createdAt, updatedAt, uniqueId, firstName, lastName, salary]); id,
error,
createdAt,
updatedAt,
uniqueId,
firstName,
lastName,
salary
]);
} }
@override @override
String toString() { String toString() {
return 'Employee(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, uniqueId=$uniqueId, firstName=$firstName, lastName=$lastName, salary=$salary)'; return 'Employee(id=$id, error=$error, createdAt=$createdAt, updatedAt=$updatedAt, uniqueId=$uniqueId, firstName=$firstName, lastName=$lastName, salary=$salary)';
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
@ -292,26 +326,28 @@ class EmployeeSerializer extends Codec<Employee, Map> {
EmployeeDecoder get decoder => const EmployeeDecoder(); EmployeeDecoder get decoder => const EmployeeDecoder();
static Employee fromMap(Map map) { static Employee fromMap(Map map) {
return Employee( return Employee(
id: map['id'] as String?, id: map['id'] as String,
error: map['error'] as String,
createdAt: map['created_at'] != null createdAt: map['created_at'] != null
? (map['created_at'] is DateTime ? (map['created_at'] is DateTime
? (map['created_at'] as DateTime?) ? (map['created_at'] as DateTime)
: DateTime.parse(map['created_at'].toString())) : DateTime.parse(map['created_at'].toString()))
: null, : null,
updatedAt: map['updated_at'] != null updatedAt: map['updated_at'] != null
? (map['updated_at'] is DateTime ? (map['updated_at'] is DateTime
? (map['updated_at'] as DateTime?) ? (map['updated_at'] as DateTime)
: DateTime.parse(map['updated_at'].toString())) : DateTime.parse(map['updated_at'].toString()))
: null, : null,
uniqueId: map['unique_id'] as String?, uniqueId: map['unique_id'] as String,
firstName: map['first_name'] as String?, firstName: map['first_name'] as String,
lastName: map['last_name'] as String?, lastName: map['last_name'] as String,
salary: map['salary'] as double?); salary: map['salary'] as double);
} }
static Map<String, dynamic> toMap(_Employee model) { static Map<String, dynamic> toMap(_Employee model) {
return { return {
'id': model.id, 'id': model.id,
'error': model.error,
'created_at': model.createdAt?.toIso8601String(), 'created_at': model.createdAt?.toIso8601String(),
'updated_at': model.updatedAt?.toIso8601String(), 'updated_at': model.updatedAt?.toIso8601String(),
'unique_id': model.uniqueId, 'unique_id': model.uniqueId,
@ -325,6 +361,7 @@ class EmployeeSerializer extends Codec<Employee, Map> {
abstract class EmployeeFields { abstract class EmployeeFields {
static const List<String> allFields = <String>[ static const List<String> allFields = <String>[
id, id,
error,
createdAt, createdAt,
updatedAt, updatedAt,
uniqueId, uniqueId,
@ -335,6 +372,8 @@ abstract class EmployeeFields {
static const String id = 'id'; static const String id = 'id';
static const String error = 'error';
static const String createdAt = 'created_at'; static const String createdAt = 'created_at';
static const String updatedAt = 'updated_at'; static const String updatedAt = 'updated_at';

View file

@ -28,7 +28,7 @@ void main() async {
var todo = await query.insert(executor); var todo = await query.insert(executor);
print(todo.value.toJson()); print(todo.value.toJson());
var query2 = TodoQuery()..where!.id.equals(todo.value.idAsInt!); var query2 = TodoQuery()..where!.id.equals(todo.value.idAsInt);
var todo2 = await query2.getOne(executor); var todo2 = await query2.getOne(executor);
print(todo2.value.toJson()); print(todo2.value.toJson());
print(todo == todo2); print(todo == todo2);

View file

@ -1,5 +1,13 @@
# 2.0.0-beta.1 # Change Log
## 2.0.0
* Fixed NNBD issues
## 2.0.0-beta.1
* Migrated to support Dart SDK 2.12.x NNBD * Migrated to support Dart SDK 2.12.x NNBD
# 1.0.0 ## 1.0.0
* First version. * First version.

View file

@ -1,21 +1,24 @@
# angel3_orm_service # Angel3 ORM Service
[![version](https://img.shields.io/badge/pub-v2.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_orm_service) [![version](https://img.shields.io/badge/pub-v2.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_orm_service)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_service/LICENSE) [![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_service/LICENSE)
Service implementation that wraps over Angel ORM Query classes. Service implementation that wraps over Angel3 ORM Query classes.
## Installation ## Installation
In your `pubspec.yaml`: In your `pubspec.yaml`:
```yaml ```yaml
dependencies: dependencies:
angel3_orm_service: ^2.0.0-beta.1 angel3_orm_service: ^2.0.0
``` ```
## Usage ## Usage
Brief snippet (check `example/main.dart` for setup, etc.): Brief snippet (check `example/main.dart` for setup, etc.):
```dart ```dart

View file

@ -1,25 +1,22 @@
name: angel3_orm_service name: angel3_orm_service
version: 2.0.0-beta.1 version: 2.0.0-beta.1
description: Service implementation that wraps over Angel3 ORM Query classes. description: Service implementation that wraps over Angel3 ORM Query classes.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_service homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_service
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
angel3_framework: ^4.0.0 angel3_framework: ^4.0.0
angel3_orm: ^4.0.0-beta.1 angel3_orm: ^4.0.0
postgres: ^2.3.2 postgres: ^2.4.0
optional: ^6.0.0-nullsafety.2 optional: ^6.0.0
dev_dependencies: dev_dependencies:
angel3_migration: ^4.0.0-beta.1 angel3_migration: ^4.0.0
angel3_migration_runner: ^4.0.0-beta.1 angel3_migration_runner: ^4.0.0
angel3_orm_generator: ^4.0.0-beta.1 angel3_orm_generator: ^4.0.0
angel3_orm_postgres: ^3.0.0-beta.1 angel3_orm_postgres: ^3.0.0
angel3_serialize: ^4.0.0 angel3_serialize: ^4.0.0
angel3_orm_test: angel3_orm_test:
git:
url: https://github.com/dukefirehawk/angel.git
ref: angel3
path: packages/orm/angel_orm_test
build_runner: ^2.0.4 build_runner: ^2.0.4
logging: ^1.0.1 logging: ^1.0.1
pedantic: ^1.11.0 pedantic: ^1.11.0

View file

@ -114,8 +114,8 @@ void hasOneTests(FutureOr<QueryExecutor> Function() createExecutor,
test('sets null on false subquery', () async { test('sets null on false subquery', () async {
var legQuery = LegQuery() var legQuery = LegQuery()
..where!.id.equals(originalLeg!.idAsInt!) ..where!.id.equals(originalLeg!.idAsInt)
..foot!.where!.legId.equals(originalLeg!.idAsInt! + 1024); ..foot!.where!.legId.equals(originalLeg!.idAsInt + 1024);
var legOpt = await (legQuery.getOne(executor)); var legOpt = await (legQuery.getOne(executor));
expect(legOpt.isPresent, true); expect(legOpt.isPresent, true);
legOpt.ifPresent((leg) { legOpt.ifPresent((leg) {

View file

@ -115,7 +115,7 @@ void manyToManyTests(FutureOr<QueryExecutor> Function() createExecutor,
test('fetch users for role', () async { test('fetch users for role', () async {
for (var role in [canPub, canSub]) { for (var role in [canPub, canSub]) {
var query = RoleQuery()..where!.id.equals(role!.idAsInt!); var query = RoleQuery()..where!.id.equals(role!.idAsInt);
var rOpt = await query.getOne(executor); var rOpt = await query.getOne(executor);
expect(rOpt.isPresent, true); expect(rOpt.isPresent, true);
rOpt.ifPresent((r) async { rOpt.ifPresent((r) async {
@ -139,7 +139,7 @@ void manyToManyTests(FutureOr<QueryExecutor> Function() createExecutor,
expect(user.roles, isEmpty); expect(user.roles, isEmpty);
// Fetch again, just to be doubly sure. // Fetch again, just to be doubly sure.
var query = UserQuery()..where!.id.equals(user.idAsInt!); var query = UserQuery()..where!.id.equals(user.idAsInt);
var fetchedOpt = await query.getOne(executor); var fetchedOpt = await query.getOne(executor);
expect(fetchedOpt.isPresent, true); expect(fetchedOpt.isPresent, true);
fetchedOpt.ifPresent((fetched) { fetchedOpt.ifPresent((fetched) {