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

View file

@ -28,7 +28,7 @@ void main() async {
var todo = await query.insert(executor);
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);
print(todo2.value.toJson());
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
# 1.0.0
* First version.
## 1.0.0
* 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)
[![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)
[![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
In your `pubspec.yaml`:
```yaml
dependencies:
angel3_orm_service: ^2.0.0-beta.1
angel3_orm_service: ^2.0.0
```
## Usage
Brief snippet (check `example/main.dart` for setup, etc.):
```dart
@ -30,4 +33,4 @@ Brief snippet (check `example/main.dart` for setup, etc.):
// **
app.use<int, Todo, OrmService<int, Todo, TodoQuery>>(
'/api/todos', todoService);
```
```

View file

@ -1,25 +1,22 @@
name: angel3_orm_service
version: 2.0.0-beta.1
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:
sdk: '>=2.12.0 <3.0.0'
dependencies:
angel3_framework: ^4.0.0
angel3_orm: ^4.0.0-beta.1
postgres: ^2.3.2
optional: ^6.0.0-nullsafety.2
angel3_orm: ^4.0.0
postgres: ^2.4.0
optional: ^6.0.0
dev_dependencies:
angel3_migration: ^4.0.0-beta.1
angel3_migration_runner: ^4.0.0-beta.1
angel3_orm_generator: ^4.0.0-beta.1
angel3_orm_postgres: ^3.0.0-beta.1
angel3_migration: ^4.0.0
angel3_migration_runner: ^4.0.0
angel3_orm_generator: ^4.0.0
angel3_orm_postgres: ^3.0.0
angel3_serialize: ^4.0.0
angel3_orm_test:
git:
url: https://github.com/dukefirehawk/angel.git
ref: angel3
path: packages/orm/angel_orm_test
build_runner: ^2.0.4
logging: ^1.0.1
pedantic: ^1.11.0

View file

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

View file

@ -115,7 +115,7 @@ void manyToManyTests(FutureOr<QueryExecutor> Function() createExecutor,
test('fetch users for role', () async {
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);
expect(rOpt.isPresent, true);
rOpt.ifPresent((r) async {
@ -139,7 +139,7 @@ void manyToManyTests(FutureOr<QueryExecutor> Function() createExecutor,
expect(user.roles, isEmpty);
// 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);
expect(fetchedOpt.isPresent, true);
fetchedOpt.ifPresent((fetched) {