Migrated to NNBD

This commit is contained in:
thomashii 2021-05-16 16:25:29 +08:00
parent a32bb29af6
commit bf9bf67fa8
6 changed files with 84 additions and 84 deletions

View file

@ -13,22 +13,22 @@ Future<void> configureServer(Angel app) async {
var executor = PostgreSqlExecutor(connection, logger: logger); var executor = PostgreSqlExecutor(connection, logger: logger);
app app
..container.registerSingleton<QueryExecutor>(executor) ..container!.registerSingleton<QueryExecutor>(executor)
..shutdownHooks.add((_) => connection.close()); ..shutdownHooks.add((_) => connection.close());
} }
Future<PostgreSQLConnection> connectToPostgres(Map configuration) async { Future<PostgreSQLConnection> connectToPostgres(Map configuration) async {
var postgresConfig = configuration['postgres'] as Map ?? {}; var postgresConfig = configuration['postgres'] as Map? ?? {};
var connection = PostgreSQLConnection( var connection = PostgreSQLConnection(
postgresConfig['host'] as String ?? 'localhost', postgresConfig['host'] as String? ?? 'localhost',
postgresConfig['port'] as int ?? 5432, postgresConfig['port'] as int? ?? 5432,
postgresConfig['database_name'] as String ?? postgresConfig['database_name'] as String? ??
Platform.environment['USER'] ?? Platform.environment['USER'] ??
Platform.environment['USERNAME'], Platform.environment['USERNAME']!,
username: postgresConfig['username'] as String, username: postgresConfig['username'] as String?,
password: postgresConfig['password'] as String, password: postgresConfig['password'] as String?,
timeZone: postgresConfig['time_zone'] as String ?? 'UTC', timeZone: postgresConfig['time_zone'] as String? ?? 'UTC',
timeoutInSeconds: postgresConfig['timeout_in_seconds'] as int ?? 30, timeoutInSeconds: postgresConfig['timeout_in_seconds'] as int? ?? 30,
useSSL: postgresConfig['use_ssl'] as bool ?? false); useSSL: postgresConfig['use_ssl'] as bool? ?? false);
return connection; return connection;
} }

View file

@ -2,12 +2,12 @@ import 'package:angel_migration/angel_migration.dart';
import 'package:angel_model/angel_model.dart'; import 'package:angel_model/angel_model.dart';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel_serialize/angel_serialize.dart';
import 'package:angel_orm/angel_orm.dart'; import 'package:angel_orm/angel_orm.dart';
import 'package:meta/meta.dart'; import 'package:optional/optional.dart';
part 'greeting.g.dart'; part 'greeting.g.dart';
@serializable @serializable
@orm @orm
abstract class _Greeting extends Model { abstract class _Greeting extends Model {
@SerializableField(isNullable: false) @SerializableField(isNullable: false)
String get message; String? get message;
} }

View file

@ -10,7 +10,7 @@ class GreetingMigration extends Migration {
@override @override
void up(Schema schema) { void up(Schema schema) {
schema.create('greetings', (table) { schema.create('greetings', (table) {
table.serial('id')..primaryKey(); table.serial('id').primaryKey();
table.varChar('message'); table.varChar('message');
table.timeStamp('created_at'); table.timeStamp('created_at');
table.timeStamp('updated_at'); table.timeStamp('updated_at');
@ -28,7 +28,7 @@ class GreetingMigration extends Migration {
// ************************************************************************** // **************************************************************************
class GreetingQuery extends Query<Greeting, GreetingQueryWhere> { class GreetingQuery extends Query<Greeting, GreetingQueryWhere> {
GreetingQuery({Set<String> trampoline}) { GreetingQuery({Set<String>? trampoline}) {
trampoline ??= Set(); trampoline ??= Set();
trampoline.add(tableName); trampoline.add(tableName);
_where = GreetingQueryWhere(this); _where = GreetingQueryWhere(this);
@ -37,25 +37,25 @@ class GreetingQuery extends Query<Greeting, GreetingQueryWhere> {
@override @override
final GreetingQueryValues values = GreetingQueryValues(); final GreetingQueryValues values = GreetingQueryValues();
GreetingQueryWhere _where; GreetingQueryWhere? _where;
@override @override
get casts { Map<String, String> get casts {
return {}; return {};
} }
@override @override
get tableName { String get tableName {
return 'greetings'; return 'greetings';
} }
@override @override
get fields { List<String> get fields {
return const ['id', 'message', 'created_at', 'updated_at']; return const ['id', 'message', 'created_at', 'updated_at'];
} }
@override @override
GreetingQueryWhere get where { GreetingQueryWhere? get where {
return _where; return _where;
} }
@ -64,19 +64,19 @@ class GreetingQuery extends Query<Greeting, GreetingQueryWhere> {
return GreetingQueryWhere(this); return GreetingQueryWhere(this);
} }
static Greeting parseRow(List row) { static Greeting? parseRow(List row) {
if (row.every((x) => x == null)) return null; if (row.every((x) => x == null)) return null;
var model = Greeting( var model = Greeting(
id: row[0].toString(), id: row[0].toString(),
message: (row[1] as String), message: (row[1] as String?),
createdAt: (row[2] as DateTime), createdAt: (row[2] as DateTime?),
updatedAt: (row[3] as DateTime)); updatedAt: (row[3] as DateTime?));
return model; return model;
} }
@override @override
deserialize(List row) { Optional<Greeting> deserialize(List row) {
return parseRow(row); return Optional.ofNullable(parseRow(row));
} }
} }
@ -96,37 +96,37 @@ class GreetingQueryWhere extends QueryWhere {
final DateTimeSqlExpressionBuilder updatedAt; final DateTimeSqlExpressionBuilder updatedAt;
@override @override
get expressionBuilders { List<SqlExpressionBuilder> get expressionBuilders {
return [id, message, createdAt, updatedAt]; return [id, message, createdAt, updatedAt];
} }
} }
class GreetingQueryValues extends MapQueryValues { class GreetingQueryValues extends MapQueryValues {
@override @override
get casts { Map<String, String> get casts {
return {}; return {};
} }
String get id { String? get id {
return (values['id'] as String); return (values['id'] as String?);
} }
set id(String value) => values['id'] = value; set id(String? value) => values['id'] = value;
String get message { String? get message {
return (values['message'] as String); return (values['message'] as String?);
} }
set message(String value) => values['message'] = value; set message(String? value) => values['message'] = value;
DateTime get createdAt { DateTime? get createdAt {
return (values['created_at'] as DateTime); return (values['created_at'] as DateTime?);
} }
set createdAt(DateTime value) => values['created_at'] = value; set createdAt(DateTime? value) => values['created_at'] = value;
DateTime get updatedAt { DateTime? get updatedAt {
return (values['updated_at'] as DateTime); return (values['updated_at'] as DateTime?);
} }
set updatedAt(DateTime value) => values['updated_at'] = value; set updatedAt(DateTime? value) => values['updated_at'] = value;
void copyFrom(Greeting model) { void copyFrom(Greeting model) {
message = model.message; message = model.message;
createdAt = model.createdAt; createdAt = model.createdAt;
@ -140,22 +140,22 @@ class GreetingQueryValues extends MapQueryValues {
@generatedSerializable @generatedSerializable
class Greeting extends _Greeting { class Greeting extends _Greeting {
Greeting({this.id, @required this.message, this.createdAt, this.updatedAt}); Greeting({this.id, required this.message, this.createdAt, this.updatedAt});
@override @override
final String id; final String? id;
@override @override
final String message; final String? message;
@override @override
final DateTime createdAt; final DateTime? createdAt;
@override @override
final DateTime updatedAt; final DateTime? updatedAt;
Greeting copyWith( Greeting copyWith(
{String id, String message, DateTime createdAt, DateTime updatedAt}) { {String? id, String? message, DateTime? createdAt, DateTime? updatedAt}) {
return Greeting( return Greeting(
id: id ?? this.id, id: id ?? this.id,
message: message ?? this.message, message: message ?? this.message,
@ -163,6 +163,7 @@ class Greeting extends _Greeting {
updatedAt: updatedAt ?? this.updatedAt); updatedAt: updatedAt ?? this.updatedAt);
} }
@override
bool operator ==(other) { bool operator ==(other) {
return other is _Greeting && return other is _Greeting &&
other.id == id && other.id == id &&
@ -178,7 +179,7 @@ class Greeting extends _Greeting {
@override @override
String toString() { String toString() {
return "Greeting(id=$id, message=$message, createdAt=$createdAt, updatedAt=$updatedAt)"; return 'Greeting(id=$id, message=$message, createdAt=$createdAt, updatedAt=$updatedAt)';
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
@ -210,33 +211,31 @@ class GreetingSerializer extends Codec<Greeting, Map> {
const GreetingSerializer(); const GreetingSerializer();
@override @override
get encoder => const GreetingEncoder(); GreetingEncoder get encoder => const GreetingEncoder();
@override @override
get decoder => const GreetingDecoder(); GreetingDecoder get decoder => const GreetingDecoder();
static Greeting fromMap(Map map) { static Greeting fromMap(Map map) {
if (map['message'] == null) { if (map['message'] == null) {
throw FormatException("Missing required field 'message' on Greeting."); throw FormatException("Missing required field 'message' on Greeting.");
} }
return Greeting( return Greeting(
id: map['id'] as String, id: map['id'] as String?,
message: map['message'] as String, message: map['message'] 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);
} }
static Map<String, dynamic> toMap(_Greeting model) { static Map<String, dynamic> toMap(_Greeting model) {
if (model == null) {
return null;
}
if (model.message == null) { if (model.message == null) {
throw FormatException("Missing required field 'message' on Greeting."); throw FormatException("Missing required field 'message' on Greeting.");
} }

View file

@ -22,7 +22,7 @@ AngelConfigurer configureServer(FileSystem fileSystem) {
app.get('/', (req, res) => res.render('hello')); app.get('/', (req, res) => res.render('hello'));
app.get('/greetings', (req, res) { app.get('/greetings', (req, res) {
var executor = req.container.make<QueryExecutor>(); var executor = req.container!.make<QueryExecutor>()!;
var query = GreetingQuery(); var query = GreetingQuery();
return query.get(executor); return query.get(executor);
}); });
@ -33,7 +33,7 @@ AngelConfigurer configureServer(FileSystem fileSystem) {
if (!req.bodyAsMap.containsKey('message')) { if (!req.bodyAsMap.containsKey('message')) {
throw AngelHttpException.badRequest(message: 'Missing "message".'); throw AngelHttpException.badRequest(message: 'Missing "message".');
} else { } else {
var executor = req.container.make<QueryExecutor>(); var executor = req.container!.make<QueryExecutor>()!;
var message = req.bodyAsMap['message'].toString(); var message = req.bodyAsMap['message'].toString();
var query = GreetingQuery()..values.message = message; var query = GreetingQuery()..values.message = message;
return await query.insert(executor); return await query.insert(executor);
@ -42,8 +42,8 @@ AngelConfigurer configureServer(FileSystem fileSystem) {
app.get('/greetings/:message', (req, res) { app.get('/greetings/:message', (req, res) {
var message = req.params['message'] as String; var message = req.params['message'] as String;
var executor = req.container.make<QueryExecutor>(); var executor = req.container!.make<QueryExecutor>()!;
var query = GreetingQuery()..where.message.equals(message); var query = GreetingQuery()..where!.message.equals(message);
return query.get(executor); return query.get(executor);
}); });

View file

@ -2,94 +2,95 @@ name: angel
description: An app that's going to be amazing pretty soon. description: An app that's going to be amazing pretty soon.
publish_to: none # Ensure we don't accidentally publish our private code! ;) publish_to: none # Ensure we don't accidentally publish our private code! ;)
environment: environment:
sdk: '>=2.10.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
homepage: https://github.com/angel-dart/angel homepage: https://github.com/dukefirehawk/angel
dependencies: dependencies:
angel_auth: angel_auth:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/auth path: packages/auth
angel_configuration: angel_configuration:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/configuration path: packages/configuration
angel_framework: angel_framework:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/framework path: packages/framework
angel_jael: angel_jael:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/jael/angel_jael path: packages/jael/angel_jael
angel_migration: angel_migration:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/orm/angel_migration path: packages/orm/angel_migration
angel_orm: angel_orm:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm path: packages/orm/angel_orm
angel_orm_postgres: angel_orm_postgres:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm_postgres path: packages/orm/angel_orm_postgres
angel_serialize: angel_serialize:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/serialize/angel_serialize path: packages/serialize/angel_serialize
angel_production: angel_production:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/production path: packages/production
angel_static: angel_static:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/static path: packages/static
angel_validate: angel_validate:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/validate path: packages/validate
optional: ^6.0.0-nullsafety.2
dev_dependencies: dev_dependencies:
angel_hot: angel_hot:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/hot path: packages/hot
angel_migration_runner: angel_migration_runner:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/orm/angel_migration_runner path: packages/orm/angel_migration_runner
angel_orm_generator: angel_orm_generator:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm_generator path: packages/orm/angel_orm_generator
angel_serialize_generator: angel_serialize_generator:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/serialize/angel_serialize_generator path: packages/serialize/angel_serialize_generator
angel_test: angel_test:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/test path: packages/test
build_runner: ^1.11.5 # Runs builds build_runner: ^2.0.3 # Runs builds
io: ^0.3.0 # For pretty printing. io: ^1.0.0 # For pretty printing.
pedantic: ^1.0.0 # Enforces Dart style conventions. pedantic: ^1.11.0 # Enforces Dart style conventions.
test: ^1.0.0 # For unit testing. test: ^1.17.4 # For unit testing.

View file

@ -20,7 +20,7 @@ import 'package:test/test.dart';
// https://github.com/dart-lang/test // https://github.com/dart-lang/test
void main() async { void main() async {
TestClient client; late TestClient client;
setUp(() async { setUp(() async {
var app = Angel(); var app = Angel();
@ -35,7 +35,7 @@ void main() async {
test('index returns 200', () async { test('index returns 200', () async {
// Request a resource at the given path. // Request a resource at the given path.
var response = await client.get('/'); var response = await client.get(Uri.parse('/'));
// Expect a 200 response. // Expect a 200 response.
expect(response, hasStatus(200)); expect(response, hasStatus(200));