Published angel_orm_mysql

This commit is contained in:
thomashii 2021-06-18 18:15:23 +08:00
parent 0b507461a7
commit 286ad100f0
21 changed files with 342 additions and 300 deletions

View file

@ -0,0 +1,11 @@
# angel3_orm_mysql
[![version](https://img.shields.io/badge/pub-v4.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_orm_mysql)
[![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_mysql/LICENSE)
Mysql support for Angel's ORM.
For documentation about the ORM, head to the main project repo:
https://github.com/dukefirehawk/angel/tree/angel3/packages/orm

View file

@ -1,13 +1,14 @@
import 'package:angel_migration/angel_migration.dart';
import 'package:angel_model/angel_model.dart';
import 'package:angel_orm/angel_orm.dart';
import 'package:angel_orm_mysql/angel_orm_mysql.dart';
import 'package:angel_serialize/angel_serialize.dart';
import 'package:angel3_migration/angel3_migration.dart';
import 'package:angel3_model/angel3_model.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_orm_mysql/angel3_orm_mysql.dart';
import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:logging/logging.dart';
import 'package:sqljocky5/sqljocky.dart';
import 'package:galileo_sqljocky5/sqljocky.dart';
import 'package:optional/optional.dart';
part 'main.g.dart';
main() async {
void main() async {
hierarchicalLoggingEnabled = true;
Logger.root
..level = Level.ALL
@ -25,19 +26,19 @@ main() async {
..isComplete = false;
var todo = await query.insert(executor);
print(todo.toJson());
print(todo.value.toJson());
var query2 = TodoQuery()..where.id.equals(todo.idAsInt);
var query2 = TodoQuery()..where!.id.equals(todo.value.idAsInt!);
var todo2 = await query2.getOne(executor);
print(todo2.toJson());
print(todo2.value.toJson());
print(todo == todo2);
}
@serializable
@orm
abstract class _Todo extends Model {
String get text;
String? get text;
@DefaultsTo(false)
bool isComplete;
bool? isComplete;
}

View file

@ -29,7 +29,7 @@ class TodoMigration extends Migration {
// **************************************************************************
class TodoQuery extends Query<Todo, TodoQueryWhere> {
TodoQuery({Set<String> trampoline}) {
TodoQuery({Set<String>? trampoline}) {
trampoline ??= Set();
trampoline.add(tableName);
_where = TodoQueryWhere(this);
@ -38,7 +38,7 @@ class TodoQuery extends Query<Todo, TodoQueryWhere> {
@override
final TodoQueryValues values = TodoQueryValues();
TodoQueryWhere _where;
TodoQueryWhere? _where;
@override
get casts {
@ -56,7 +56,7 @@ class TodoQuery extends Query<Todo, TodoQueryWhere> {
}
@override
TodoQueryWhere get where {
TodoQueryWhere? get where {
return _where;
}
@ -65,20 +65,20 @@ class TodoQuery extends Query<Todo, TodoQueryWhere> {
return TodoQueryWhere(this);
}
static Todo parseRow(List row) {
static Todo? parseRow(List row) {
if (row.every((x) => x == null)) return null;
var model = Todo(
id: row[0].toString(),
isComplete: (row[1] as bool),
text: (row[2] as String),
createdAt: (row[3] as DateTime),
updatedAt: (row[4] as DateTime));
isComplete: (row[1] as bool?),
text: (row[2] as String?),
createdAt: (row[3] as DateTime?),
updatedAt: (row[4] as DateTime?));
return model;
}
@override
deserialize(List row) {
return parseRow(row);
Optional<Todo> deserialize(List row) {
return Optional.ofNullable(parseRow(row));
}
}
@ -112,31 +112,31 @@ class TodoQueryValues extends MapQueryValues {
return {};
}
int get id {
return (values['id'] as int);
int? get id {
return (values['id'] as int?);
}
set id(int value) => values['id'] = value;
bool get isComplete {
return (values['is_complete'] as bool);
set id(int? value) => values['id'] = value;
bool? get isComplete {
return (values['is_complete'] as bool?);
}
set isComplete(bool value) => values['is_complete'] = value;
String get text {
return (values['text'] as String);
set isComplete(bool? value) => values['is_complete'] = value;
String? get text {
return (values['text'] as String?);
}
set text(String value) => values['text'] = value;
DateTime get createdAt {
return (values['created_at'] as DateTime);
set text(String? value) => values['text'] = value;
DateTime? get createdAt {
return (values['created_at'] as DateTime?);
}
set createdAt(DateTime value) => values['created_at'] = value;
DateTime get updatedAt {
return (values['updated_at'] as DateTime);
set createdAt(DateTime? value) => values['created_at'] = value;
DateTime? get updatedAt {
return (values['updated_at'] as DateTime?);
}
set updatedAt(DateTime value) => values['updated_at'] = value;
set updatedAt(DateTime? value) => values['updated_at'] = value;
void copyFrom(Todo model) {
isComplete = model.isComplete;
text = model.text;
@ -159,26 +159,26 @@ class Todo extends _Todo {
this.updatedAt});
@override
final String id;
final String? id;
@override
final bool isComplete;
final bool? isComplete;
@override
final String text;
final String? text;
@override
final DateTime createdAt;
final DateTime? createdAt;
@override
final DateTime updatedAt;
final DateTime? updatedAt;
Todo copyWith(
{String id,
bool isComplete,
String text,
DateTime createdAt,
DateTime updatedAt}) {
{String? id,
bool? isComplete,
String? text,
DateTime? createdAt,
DateTime? updatedAt}) {
return new Todo(
id: id ?? this.id,
isComplete: isComplete ?? this.isComplete,
@ -218,25 +218,22 @@ abstract class TodoSerializer {
}
return new Todo(
id: map['id'] as String,
isComplete: map['is_complete'] as bool ?? false,
text: map['text'] as String,
id: map['id'] as String?,
isComplete: map['is_complete'] as bool? ?? false,
text: map['text'] 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);
}
static Map<String, dynamic> toMap(_Todo model) {
if (model == null) {
return null;
}
if (model.isComplete == null) {
throw new FormatException(
"Missing required field 'is_complete' on Todo.");

View file

@ -1,15 +1,15 @@
import 'dart:async';
import 'package:angel_orm/angel_orm.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:logging/logging.dart';
// import 'package:pool/pool.dart';
import 'package:sqljocky5/connection/connection.dart';
import 'package:sqljocky5/sqljocky.dart';
import 'package:galileo_sqljocky5/public/connection/connection.dart';
import 'package:galileo_sqljocky5/sqljocky.dart';
class MySqlExecutor extends QueryExecutor {
/// An optional [Logger] to write to.
final Logger logger;
final Logger? logger;
final Querier _connection;
final Querier? _connection;
MySqlExecutor(this._connection, {this.logger});
@ -23,7 +23,7 @@ class MySqlExecutor extends QueryExecutor {
Future<Transaction> _startTransaction() {
if (_connection is Transaction) {
return Future.value(_connection as Transaction);
return Future.value(_connection as Transaction?);
} else if (_connection is MySqlConnection) {
return (_connection as MySqlConnection).begin();
} else {
@ -34,7 +34,7 @@ class MySqlExecutor extends QueryExecutor {
@override
Future<List<List>> query(
String tableName, String query, Map<String, dynamic> substitutionValues,
[List<String> returningFields]) {
[List<String> returningFields = const []]) {
// Change @id -> ?
for (var name in substitutionValues.keys) {
query = query.replaceAll('@$name', '?');
@ -43,8 +43,8 @@ class MySqlExecutor extends QueryExecutor {
logger?.fine('Query: $query');
logger?.fine('Values: $substitutionValues');
if (returningFields?.isNotEmpty != true) {
return _connection
if (returningFields.isNotEmpty != true) {
return _connection!
.prepared(query, substitutionValues.values)
.then((results) => results.map((r) => r.toList()).toList());
} else {
@ -63,7 +63,7 @@ class MySqlExecutor extends QueryExecutor {
await tx.commit();
return mapped;
} catch (_) {
await tx?.rollback();
await tx.rollback();
rethrow;
}
});
@ -76,7 +76,7 @@ class MySqlExecutor extends QueryExecutor {
return await f(this);
}
Transaction tx;
Transaction? tx;
try {
tx = await _startTransaction();
var executor = MySqlExecutor(tx, logger: logger);

View file

@ -1,27 +1,23 @@
name: angel_orm_mysql
version: 0.0.0
description: MySQL support for Angel's ORM. Includes functionality for querying and transactions.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/orm
name: angel3_orm_mysql
version: 2.0.0-beta.1
description: MySQL support for Angel3's ORM. Includes functionality for querying and transactions.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_mysql
environment:
sdk: '>=2.10.0 <2.12.0'
sdk: '>=2.12.0 <3.0.0'
dependencies:
angel_orm: #^2.1.0-beta
path: ../angel_orm
logging: ^0.11.0
pool: ^1.0.0
sqljocky5: ^2.2.1
angel3_orm: ^4.0.0-beta.1
logging: ^1.0.0
pool: ^1.5.0
galileo_sqljocky5: ^3.0.0
#optional: ^6.0.0-nullsafety.2
dev_dependencies:
angel_migration: #^2.0.0
path: ../angel_migration
angel_orm_generator: #^2.1.0-beta
path: ../angel_orm_generator
angel_orm_test:
path: ../angel_orm_test
build_runner: ^1.0.0
test: ^1.0.0
#dependency_overrides:
# angel_migration:
# path: ../angel_migration
# angel_orm_generator:
# path: ../angel_orm_generator
angel3_migration: ^4.0.0-beta.1
angel3_orm_generator: ^4.0.0-beta.1
build_runner: ^2.0.0
test: ^1.17.0
pedantic: ^1.11.0
angel3_orm_test:
git:
url: https://github.com/dukefirehawk/angel.git
ref: angel3
path: packages/orm/angel_orm_test

View file

@ -1,4 +1,4 @@
import 'package:angel_orm_test/angel_orm_test.dart';
import 'package:angel3_orm_test/angel3_orm_test.dart';
import 'package:logging/logging.dart';
import 'package:test/test.dart';
import 'common.dart';

View file

@ -1,9 +1,9 @@
import 'dart:async';
import 'dart:io';
import 'package:angel_orm/angel_orm.dart';
import 'package:angel_orm_mysql/angel_orm_mysql.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_orm_mysql/angel3_orm_mysql.dart';
import 'package:logging/logging.dart';
import 'package:sqljocky5/sqljocky.dart';
import 'package:galileo_sqljocky5/sqljocky.dart';
FutureOr<QueryExecutor> Function() my(Iterable<String> schemas) {
return () => connectToMySql(schemas);
@ -20,9 +20,10 @@ Future<MySqlExecutor> connectToMySql(Iterable<String> schemas) async {
var connection = await MySqlConnection.connect(settings);
var logger = Logger('angel_orm_mysql');
for (var s in schemas)
for (var s in schemas) {
await connection
.execute(await new File('test/migrations/$s.sql').readAsString());
.execute(await File('test/migrations/$s.sql').readAsString());
}
return MySqlExecutor(connection, logger: logger);
}

View file

@ -5,7 +5,7 @@
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_postgres/LICENSE)
PostgreSQL support for Angel's ORM.
Postgresql support for Angel's ORM.
For documentation about the ORM, head to the main project repo:
https://github.com/angel-dart/orm
For documentation about the ORM with postgres, head to the main project repo:
[ORM Repo](https://github.com/dukefirehawk/angel/tree/angel3/packages/orm)

View file

@ -1,6 +1,6 @@
name: angel3_orm_postgres
version: 3.0.0-beta.1
description: PostgreSQL support for Angel's ORM. Includes functionality for querying and transactions.
description: PostgreSQL support for Angel3's ORM. Includes functionality for querying and transactions.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_postgres
environment:
sdk: '>=2.12.0 <3.0.0'
@ -13,7 +13,10 @@ dependencies:
dev_dependencies:
angel3_pretty_logging: ^3.0.0
test: ^1.17.4
test: ^1.17.0
pedantic: ^1.11.0
#angel3_orm_test:
# path: ../angel_orm_test
angel3_orm_test:
git:
url: https://github.com/dukefirehawk/angel.git
ref: angel3
path: packages/orm/angel_orm_test

View file

@ -1,17 +1,15 @@
//import 'package:angel3_orm_test/angel3_orm_test.dart';
import 'package:angel3_orm_test/angel3_orm_test.dart';
import 'package:logging/logging.dart';
import 'package:angel3_pretty_logging/angel3_pretty_logging.dart';
//import 'package:test/test.dart';
//import 'common.dart';
import 'package:test/test.dart';
import 'common.dart';
void main() {
Logger.root
..level = Level.ALL
..onRecord.listen(prettyLog);
/*
group('postgresql', () {
group('belongsTo',
() => belongsToTests(pg(['author', 'book']), close: closePg));
group('customExpr',
@ -31,5 +29,4 @@ void main() {
manyToManyTests(pg(['user', 'role', 'user_role']), close: closePg));
group('standalone', () => standaloneTests(pg(['car']), close: closePg));
});
*/
}

View file

@ -0,0 +1,12 @@
Primary Authors
===============
* __[Thomas Hii](dukefirehawk.apps@gmail.com)__
Thomas is the current maintainer of the code base. He has refactored and migrated the
code base to support NNBD.
* __[Tobe O](thosakwe@gmail.com)__
Tobe has written much of the original code prior to NNBD migration. He has moved on and
is no longer involved with the project.

View file

@ -1,2 +1,5 @@
# 2.0.0
* Migrated to support Dart SDK 2.12.x NNBD
# 1.0.0
* First version.

View file

@ -5,13 +5,13 @@ import 'package:logging/logging.dart';
import 'connect.dart';
import 'todo.dart';
main() async {
void main() async {
// Logging, connection setup.
hierarchicalLoggingEnabled = true;
var app = Angel(logger: Logger.detached('orm_service'));
var http = AngelHttp(app);
var executor = await connect();
app.logger.onRecord.listen((rec) {
app.logger!.onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);

View file

@ -3,7 +3,7 @@ import 'package:angel_migration_runner/postgres.dart';
import 'connect.dart';
import 'todo.dart';
main(List<String> args) {
Future main(List<String> args) {
var runner = PostgresMigrationRunner(conn, migrations: [
TodoMigration(),
]);

View file

@ -1,14 +1,15 @@
import 'package:angel_migration/angel_migration.dart';
import 'package:angel_serialize/angel_serialize.dart';
import 'package:angel_orm/angel_orm.dart';
import 'package:optional/optional.dart';
part 'todo.g.dart';
@serializable
@orm
abstract class _Todo extends Model {
@notNull
String get text;
String? get text;
@DefaultsTo(false)
bool isComplete;
bool? isComplete;
}

View file

@ -8,10 +8,10 @@ part of 'todo.dart';
class TodoMigration extends Migration {
@override
up(Schema schema) {
void up(Schema schema) {
schema.create('todos', (table) {
table.serial('id')..primaryKey();
table.boolean('is_complete')..defaultsTo(false);
table.serial('id').primaryKey();
table.boolean('is_complete').defaultsTo(false);
table.varChar('text');
table.timeStamp('created_at');
table.timeStamp('updated_at');
@ -19,7 +19,7 @@ class TodoMigration extends Migration {
}
@override
down(Schema schema) {
void down(Schema schema) {
schema.drop('todos');
}
}
@ -29,8 +29,8 @@ class TodoMigration extends Migration {
// **************************************************************************
class TodoQuery extends Query<Todo, TodoQueryWhere> {
TodoQuery({Set<String> trampoline}) {
trampoline ??= Set();
TodoQuery({Set<String>? trampoline}) {
trampoline ??= <String>{};
trampoline.add(tableName);
_where = TodoQueryWhere(this);
}
@ -38,25 +38,25 @@ class TodoQuery extends Query<Todo, TodoQueryWhere> {
@override
final TodoQueryValues values = TodoQueryValues();
TodoQueryWhere _where;
TodoQueryWhere? _where;
@override
get casts {
Map<String, String> get casts {
return {};
}
@override
get tableName {
String get tableName {
return 'todos';
}
@override
get fields {
List<String> get fields {
return const ['id', 'is_complete', 'text', 'created_at', 'updated_at'];
}
@override
TodoQueryWhere get where {
TodoQueryWhere? get where {
return _where;
}
@ -65,20 +65,20 @@ class TodoQuery extends Query<Todo, TodoQueryWhere> {
return TodoQueryWhere(this);
}
static Todo parseRow(List row) {
static Todo? parseRow(List row) {
if (row.every((x) => x == null)) return null;
var model = Todo(
id: row[0].toString(),
isComplete: (row[1] as bool),
text: (row[2] as String),
createdAt: (row[3] as DateTime),
updatedAt: (row[4] as DateTime));
isComplete: (row[1] as bool?),
text: (row[2] as String?),
createdAt: (row[3] as DateTime?),
updatedAt: (row[4] as DateTime?));
return model;
}
@override
deserialize(List row) {
return parseRow(row);
Optional<Todo> deserialize(List row) {
return Optional.ofNullable(parseRow(row));
}
}
@ -101,42 +101,42 @@ class TodoQueryWhere extends QueryWhere {
final DateTimeSqlExpressionBuilder updatedAt;
@override
get expressionBuilders {
List<SqlExpressionBuilder> get expressionBuilders {
return [id, isComplete, text, createdAt, updatedAt];
}
}
class TodoQueryValues extends MapQueryValues {
@override
get casts {
Map<String, String> get casts {
return {};
}
String get id {
return (values['id'] as String);
String? get id {
return (values['id'] as String?);
}
set id(String value) => values['id'] = value;
bool get isComplete {
return (values['is_complete'] as bool);
set id(String? value) => values['id'] = value;
bool? get isComplete {
return (values['is_complete'] as bool?);
}
set isComplete(bool value) => values['is_complete'] = value;
String get text {
return (values['text'] as String);
set isComplete(bool? value) => values['is_complete'] = value;
String? get text {
return (values['text'] as String?);
}
set text(String value) => values['text'] = value;
DateTime get createdAt {
return (values['created_at'] as DateTime);
set text(String? value) => values['text'] = value;
DateTime? get createdAt {
return (values['created_at'] as DateTime?);
}
set createdAt(DateTime value) => values['created_at'] = value;
DateTime get updatedAt {
return (values['updated_at'] as DateTime);
set createdAt(DateTime? value) => values['created_at'] = value;
DateTime? get updatedAt {
return (values['updated_at'] as DateTime?);
}
set updatedAt(DateTime value) => values['updated_at'] = value;
set updatedAt(DateTime? value) => values['updated_at'] = value;
void copyFrom(Todo model) {
isComplete = model.isComplete;
text = model.text;
@ -154,31 +154,31 @@ class Todo extends _Todo {
Todo(
{this.id,
this.isComplete = false,
@required this.text,
required this.text,
this.createdAt,
this.updatedAt});
@override
final String id;
final String? id;
@override
final bool isComplete;
final bool? isComplete;
@override
final String text;
final String? text;
@override
final DateTime createdAt;
final DateTime? createdAt;
@override
final DateTime updatedAt;
final DateTime? updatedAt;
Todo copyWith(
{String id,
bool isComplete,
String text,
DateTime createdAt,
DateTime updatedAt}) {
{String? id,
bool? isComplete,
String? text,
DateTime? createdAt,
DateTime? updatedAt}) {
return Todo(
id: id ?? this.id,
isComplete: isComplete ?? this.isComplete,
@ -187,6 +187,7 @@ class Todo extends _Todo {
updatedAt: updatedAt ?? this.updatedAt);
}
@override
bool operator ==(other) {
return other is _Todo &&
other.id == id &&
@ -203,7 +204,7 @@ class Todo extends _Todo {
@override
String toString() {
return "Todo(id=$id, isComplete=$isComplete, text=$text, createdAt=$createdAt, updatedAt=$updatedAt)";
return 'Todo(id=$id, isComplete=$isComplete, text=$text, createdAt=$createdAt, updatedAt=$updatedAt)';
}
Map<String, dynamic> toJson() {
@ -235,34 +236,31 @@ class TodoSerializer extends Codec<Todo, Map> {
const TodoSerializer();
@override
get encoder => const TodoEncoder();
TodoEncoder get encoder => const TodoEncoder();
@override
get decoder => const TodoDecoder();
TodoDecoder get decoder => const TodoDecoder();
static Todo fromMap(Map map) {
if (map['text'] == null) {
throw FormatException("Missing required field 'text' on Todo.");
}
return Todo(
id: map['id'] as String,
isComplete: map['is_complete'] as bool ?? false,
text: map['text'] as String,
id: map['id'] as String?,
isComplete: map['is_complete'] as bool? ?? false,
text: map['text'] 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);
}
static Map<String, dynamic> toMap(_Todo model) {
if (model == null) {
return null;
}
if (model.text == null) {
throw FormatException("Missing required field 'text' on Todo.");
}

View file

@ -33,14 +33,14 @@ class OrmService<Id, Data, TQuery extends Query<Data, QueryWhere>>
{this.idField = 'id',
this.allowRemoveAll = false,
this.allowQuery = true,
FutureOr<Data> Function(RequestContext, ResponseContext) readData})
FutureOr<Data> Function(RequestContext, ResponseContext)? readData})
: super(readData: readData);
SqlExpressionBuilder _findBuilder(TQuery query, String name) {
return query.where.expressionBuilders.firstWhere(
return query.where!.expressionBuilders.firstWhere(
(b) => b.columnName == name,
orElse: () => throw ArgumentError(
'${query.where.runtimeType} has no expression builder for a column named "$name".'));
orElse: (() => throw ArgumentError(
'${query.where.runtimeType} has no expression builder for a column named "$name".')) as SqlExpressionBuilder<dynamic> Function()?);
}
void _apply(TQuery query, String name, dynamic value) {
@ -53,7 +53,7 @@ class OrmService<Id, Data, TQuery extends Query<Data, QueryWhere>>
}
}
Future<void> _applyQuery(TQuery query, Map<String, dynamic> params) async {
Future<void> _applyQuery(TQuery query, Map<String, dynamic>? params) async {
if (params == null || params.isEmpty) return;
if (allowQuery || !params.containsKey('provider')) {
@ -87,7 +87,7 @@ class OrmService<Id, Data, TQuery extends Query<Data, QueryWhere>>
@override
Future<List<Data>> readMany(List<Id> ids,
[Map<String, dynamic> params]) async {
[Map<String, dynamic>? params]) async {
if (ids.isEmpty) {
throw ArgumentError.value(ids, 'ids', 'cannot be empty');
}
@ -107,36 +107,36 @@ class OrmService<Id, Data, TQuery extends Query<Data, QueryWhere>>
}
@override
Future<List<Data>> index([Map<String, dynamic> params]) async {
Future<List<Data>> index([Map<String, dynamic>? params]) async {
var query = await queryCreator();
await _applyQuery(query, params);
return await query.get(executor);
}
@override
Future<Data> read(Id id, [Map<String, dynamic> params]) async {
Future<Data> read(Id id, [Map<String, dynamic>? params]) async {
var query = await queryCreator();
_apply(query, idField, id);
await _applyQuery(query, params);
var result = await query.getOne(executor);
if (result != null) return result;
if (result != null && result.isPresent) return result.value;
throw AngelHttpException.notFound(message: 'No record found for ID $id');
}
@override
Future<Data> findOne(
[Map<String, dynamic> params,
[Map<String, dynamic>? params,
String errorMessage =
'No record was found matching the given query.']) async {
var query = await queryCreator();
await _applyQuery(query, params);
var result = await query.getOne(executor);
if (result != null) return result;
if (result != null && result.isPresent) return result.value;
throw AngelHttpException.notFound(message: errorMessage);
}
@override
Future<Data> create(Data data, [Map<String, dynamic> params]) async {
Future<Data> create(Data data, [Map<String, dynamic>? params]) async {
var query = await queryCreator();
try {
@ -146,16 +146,18 @@ class OrmService<Id, Data, TQuery extends Query<Data, QueryWhere>>
'${query.values.runtimeType} has no `copyFrom` method, but OrmService requires this for insertions.');
}
return await query.insert(executor);
var result = await query.insert(executor);
return result.value;
}
@override
Future<Data> modify(Id id, Data data, [Map<String, dynamic> params]) {
Future<Data> modify(Id id, Data data, [Map<String, dynamic>? params]) {
return update(id, data, params);
}
@override
Future<Data> update(Id id, Data data, [Map<String, dynamic> params]) async {
Future<Data> update(Id id, Data data, [Map<String, dynamic>? params]) async {
var query = await queryCreator();
_apply(query, idField, id);
await _applyQuery(query, params);
@ -168,12 +170,12 @@ class OrmService<Id, Data, TQuery extends Query<Data, QueryWhere>>
}
var result = await query.updateOne(executor);
if (result != null) return result;
if (result != null) return result.value;
throw AngelHttpException.notFound(message: 'No record found for ID $id');
}
@override
Future<Data> remove(Id id, [Map<String, dynamic> params]) async {
Future<Data> remove(Id id, [Map<String, dynamic>? params]) async {
var query = await queryCreator();
if (id == null || id == 'null') {
@ -189,7 +191,7 @@ class OrmService<Id, Data, TQuery extends Query<Data, QueryWhere>>
}
var result = await query.deleteOne(executor);
if (result != null) return result;
if (result != null && result.isPresent) return result.value;
throw AngelHttpException.notFound(message: 'No record found for ID $id');
}
}

View file

@ -1,35 +1,55 @@
name: angel_orm_service
version: 1.0.0
version: 2.0.0
description: Service implementation that wraps over Angel ORM Query classes.
homepage: https://github.com/angel-dart/orm
author: Tobe O <thosakwe@gmail.com>
publish_to: none
environment:
sdk: '>=2.10.0 <2.12.0'
sdk: '>=2.12.0 <3.0.0'
dependencies:
angel_framework: #^2.0.0-alpha
path: ../../framework
angel_orm: #^2.0.0
path: ../angel_orm
angel_framework:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/framework
angel_orm:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm
postgres: ^2.3.2
optional: ^6.0.0-nullsafety.2
dev_dependencies:
angel_migration: #^2.0.0-alpha
path: ../angel_migration
angel_migration:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_migration
angel_migration_runner:
path: ../angel_migration_runner
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_migration_runner
angel_orm_generator:
path: ../angel_orm_generator
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm_generator
angel_orm_postgres:
path: ../angel_orm_postgres
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm_postgres
angel_orm_test:
path: ../angel_orm_test
angel_serialize: #^2.0.0
path: ../../serialize/angel_serialize
build_runner: ^1.0.0
logging: ^0.11.0
pedantic: ^1.0.0
# postgres: ^2.2.0
test: ^1.15.7
#dependency_overrides:
# angel_migration:
# path: ../angel_migration
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm_test
angel_serialize:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/serialize/angel_serialize
build_runner: ^2.0.4
logging: ^1.0.1
pedantic: ^1.11.0
test: ^1.17.7

View file

@ -9,9 +9,9 @@ import 'package:test/test.dart';
import 'pokemon.dart';
void main() {
Logger logger;
PostgreSqlExecutor executor;
Service<int, Pokemon> pokemonService;
late Logger logger;
late PostgreSqlExecutor executor;
late Service<int?, Pokemon> pokemonService;
setUp(() async {
var conn = PostgreSQLConnection('localhost', 5432, 'angel_orm_service_test',
@ -60,7 +60,8 @@ void main() {
});
group('after create', () {
Pokemon giratina, pikachu;
late Pokemon giratina;
late Pokemon pikachu;
setUp(() async {
giratina = await pokemonService.create(Pokemon(
@ -85,7 +86,7 @@ void main() {
test('with callback', () async {
var result = await pokemonService.index({
'query': (PokemonQuery query) async {
query.where.level.equals(pikachu.level);
query.where!.level.equals(pikachu.level!);
},
});
@ -163,7 +164,7 @@ void main() {
pikachu);
expect(
() => pokemonService.findOne({
'query': {PokemonFields.level: pikachu.level * 3}
'query': {PokemonFields.level: pikachu.level! * 3}
}),
throwsA(TypeMatcher<AngelHttpException>()));
});

View file

@ -1,6 +1,7 @@
import 'package:angel_migration/angel_migration.dart';
import 'package:angel_serialize/angel_serialize.dart';
import 'package:angel_orm/angel_orm.dart';
import 'package:optional/optional.dart';
part 'pokemon.g.dart';
enum PokemonType {
@ -19,15 +20,15 @@ enum PokemonType {
@orm
abstract class _Pokemon extends Model {
@notNull
String get species;
String? get species;
String get name;
String? get name;
@notNull
int get level;
int? get level;
@notNull
PokemonType get type1;
PokemonType? get type1;
PokemonType get type2;
PokemonType? get type2;
}

View file

@ -8,9 +8,9 @@ part of 'pokemon.dart';
class PokemonMigration extends Migration {
@override
up(Schema schema) {
void up(Schema schema) {
schema.create('pokemons', (table) {
table.serial('id')..primaryKey();
table.serial('id').primaryKey();
table.varChar('species');
table.varChar('name');
table.integer('level');
@ -22,7 +22,7 @@ class PokemonMigration extends Migration {
}
@override
down(Schema schema) {
void down(Schema schema) {
schema.drop('pokemons');
}
}
@ -32,8 +32,8 @@ class PokemonMigration extends Migration {
// **************************************************************************
class PokemonQuery extends Query<Pokemon, PokemonQueryWhere> {
PokemonQuery({Set<String> trampoline}) {
trampoline ??= Set();
PokemonQuery({Set<String>? trampoline}) {
trampoline ??= <String>{};
trampoline.add(tableName);
_where = PokemonQueryWhere(this);
}
@ -41,20 +41,20 @@ class PokemonQuery extends Query<Pokemon, PokemonQueryWhere> {
@override
final PokemonQueryValues values = PokemonQueryValues();
PokemonQueryWhere _where;
PokemonQueryWhere? _where;
@override
get casts {
Map<String, String> get casts {
return {};
}
@override
get tableName {
String get tableName {
return 'pokemons';
}
@override
get fields {
List<String> get fields {
return const [
'id',
'species',
@ -68,7 +68,7 @@ class PokemonQuery extends Query<Pokemon, PokemonQueryWhere> {
}
@override
PokemonQueryWhere get where {
PokemonQueryWhere? get where {
return _where;
}
@ -77,23 +77,23 @@ class PokemonQuery extends Query<Pokemon, PokemonQueryWhere> {
return PokemonQueryWhere(this);
}
static Pokemon parseRow(List row) {
static Pokemon? parseRow(List row) {
if (row.every((x) => x == null)) return null;
var model = Pokemon(
id: row[0].toString(),
species: (row[1] as String),
name: (row[2] as String),
level: (row[3] as int),
species: (row[1] as String?),
name: (row[2] as String?),
level: (row[3] as int?),
type1: row[4] == null ? null : PokemonType.values[(row[4] as int)],
type2: row[5] == null ? null : PokemonType.values[(row[5] as int)],
createdAt: (row[6] as DateTime),
updatedAt: (row[7] as DateTime));
createdAt: (row[6] as DateTime?),
updatedAt: (row[7] as DateTime?));
return model;
}
@override
deserialize(List row) {
return parseRow(row);
Optional<Pokemon> deserialize(List row) {
return Optional.ofNullable(parseRow(row));
}
}
@ -127,57 +127,57 @@ class PokemonQueryWhere extends QueryWhere {
final DateTimeSqlExpressionBuilder updatedAt;
@override
get expressionBuilders {
List<SqlExpressionBuilder> get expressionBuilders {
return [id, species, name, level, type1, type2, createdAt, updatedAt];
}
}
class PokemonQueryValues extends MapQueryValues {
@override
get casts {
Map<String, String> get casts {
return {};
}
String get id {
return (values['id'] as String);
String? get id {
return (values['id'] as String?);
}
set id(String value) => values['id'] = value;
String get species {
return (values['species'] as String);
set id(String? value) => values['id'] = value;
String? get species {
return (values['species'] as String?);
}
set species(String value) => values['species'] = value;
String get name {
return (values['name'] as String);
set species(String? value) => values['species'] = value;
String? get name {
return (values['name'] as String?);
}
set name(String value) => values['name'] = value;
int get level {
return (values['level'] as int);
set name(String? value) => values['name'] = value;
int? get level {
return (values['level'] as int?);
}
set level(int value) => values['level'] = value;
set level(int? value) => values['level'] = value;
PokemonType get type1 {
return PokemonType.values[(values['type1'] as int)];
}
set type1(PokemonType value) => values['type1'] = value?.index;
set type1(PokemonType? value) => values['type1'] = value?.index;
PokemonType get type2 {
return PokemonType.values[(values['type2'] as int)];
}
set type2(PokemonType value) => values['type2'] = value?.index;
DateTime get createdAt {
return (values['created_at'] as DateTime);
set type2(PokemonType? value) => values['type2'] = value?.index;
DateTime? get createdAt {
return (values['created_at'] as DateTime?);
}
set createdAt(DateTime value) => values['created_at'] = value;
DateTime get updatedAt {
return (values['updated_at'] as DateTime);
set createdAt(DateTime? value) => values['created_at'] = value;
DateTime? get updatedAt {
return (values['updated_at'] as DateTime?);
}
set updatedAt(DateTime value) => values['updated_at'] = value;
set updatedAt(DateTime? value) => values['updated_at'] = value;
void copyFrom(Pokemon model) {
species = model.species;
name = model.name;
@ -197,47 +197,47 @@ class PokemonQueryValues extends MapQueryValues {
class Pokemon extends _Pokemon {
Pokemon(
{this.id,
@required this.species,
required this.species,
this.name,
@required this.level,
@required this.type1,
required this.level,
required this.type1,
this.type2,
this.createdAt,
this.updatedAt});
@override
final String id;
final String? id;
@override
final String species;
final String? species;
@override
final String name;
final String? name;
@override
final int level;
final int? level;
@override
final PokemonType type1;
final PokemonType? type1;
@override
final PokemonType type2;
final PokemonType? type2;
@override
final DateTime createdAt;
final DateTime? createdAt;
@override
final DateTime updatedAt;
final DateTime? updatedAt;
Pokemon copyWith(
{String id,
String species,
String name,
int level,
PokemonType type1,
PokemonType type2,
DateTime createdAt,
DateTime updatedAt}) {
{String? id,
String? species,
String? name,
int? level,
PokemonType? type1,
PokemonType? type2,
DateTime? createdAt,
DateTime? updatedAt}) {
return Pokemon(
id: id ?? this.id,
species: species ?? this.species,
@ -249,6 +249,7 @@ class Pokemon extends _Pokemon {
updatedAt: updatedAt ?? this.updatedAt);
}
@override
bool operator ==(other) {
return other is _Pokemon &&
other.id == id &&
@ -269,7 +270,7 @@ class Pokemon extends _Pokemon {
@override
String toString() {
return "Pokemon(id=$id, species=$species, name=$name, level=$level, type1=$type1, type2=$type2, createdAt=$createdAt, updatedAt=$updatedAt)";
return 'Pokemon(id=$id, species=$species, name=$name, level=$level, type1=$type1, type2=$type2, createdAt=$createdAt, updatedAt=$updatedAt)';
}
Map<String, dynamic> toJson() {
@ -301,9 +302,9 @@ class PokemonSerializer extends Codec<Pokemon, Map> {
const PokemonSerializer();
@override
get encoder => const PokemonEncoder();
PokemonEncoder get encoder => const PokemonEncoder();
@override
get decoder => const PokemonDecoder();
PokemonDecoder get decoder => const PokemonDecoder();
static Pokemon fromMap(Map map) {
if (map['species'] == null) {
throw FormatException("Missing required field 'species' on Pokemon.");
@ -318,36 +319,33 @@ class PokemonSerializer extends Codec<Pokemon, Map> {
}
return Pokemon(
id: map['id'] as String,
species: map['species'] as String,
name: map['name'] as String,
level: map['level'] as int,
id: map['id'] as String?,
species: map['species'] as String?,
name: map['name'] as String?,
level: map['level'] as int?,
type1: map['type1'] is PokemonType
? (map['type1'] as PokemonType)
? (map['type1'] as PokemonType?)
: (map['type1'] is int
? PokemonType.values[map['type1'] as int]
: null),
type2: map['type2'] is PokemonType
? (map['type2'] as PokemonType)
? (map['type2'] as PokemonType?)
: (map['type2'] is int
? PokemonType.values[map['type2'] as int]
: null),
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);
}
static Map<String, dynamic> toMap(_Pokemon model) {
if (model == null) {
return null;
}
if (model.species == null) {
throw FormatException("Missing required field 'species' on Pokemon.");
}
@ -366,9 +364,9 @@ class PokemonSerializer extends Codec<Pokemon, Map> {
'name': model.name,
'level': model.level,
'type1':
model.type1 == null ? null : PokemonType.values.indexOf(model.type1),
model.type1 == null ? null : PokemonType.values.indexOf(model.type1!),
'type2':
model.type2 == null ? null : PokemonType.values.indexOf(model.type2),
model.type2 == null ? null : PokemonType.values.indexOf(model.type2!),
'created_at': model.createdAt?.toIso8601String(),
'updated_at': model.updatedAt?.toIso8601String()
};