From 0933a7f72726f2339e682ead768e8947744321ae Mon Sep 17 00:00:00 2001 From: thomashii Date: Sun, 1 May 2022 17:46:14 +0800 Subject: [PATCH] Template for MariaDB --- bin/migrate.dart | 9 ++++++++ config/default.yaml | 3 ++- lib/src/config/plugins/orm.dart | 41 +++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/bin/migrate.dart b/bin/migrate.dart index 05b3ae0..aa6a261 100644 --- a/bin/migrate.dart +++ b/bin/migrate.dart @@ -3,6 +3,7 @@ import 'package:angel/models.dart'; import 'package:angel3_configuration/angel3_configuration.dart'; import 'package:angel3_migration_runner/angel3_migration_runner.dart'; import 'package:angel3_migration_runner/mariadb.dart'; +//import 'package:angel3_migration_runner/mysql.dart'; import 'package:file/local.dart'; import 'package:logging/logging.dart'; @@ -27,5 +28,13 @@ void main(List args) async { GreetingMigration(), ]); + // MySQL database + /* + var connection = await connectToMysql(configuration); + var migrationRunner = MySqlMigrationRunner(connection, migrations: [ + GreetingMigration(), + ]); + */ + await runMigrations(migrationRunner, args); } diff --git a/config/default.yaml b/config/default.yaml index c9ac4d5..90c4412 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -2,12 +2,13 @@ jwt_secret: INSECURE_DEFAULT_SECRET host: 127.0.0.1 port: 3000 -mariadb: +mysql: host: localhost port: 3306 database_name: appdb username: appuser password: App1970# + use_ssl: false diff --git a/lib/src/config/plugins/orm.dart b/lib/src/config/plugins/orm.dart index 72ec12f..43e39a1 100644 --- a/lib/src/config/plugins/orm.dart +++ b/lib/src/config/plugins/orm.dart @@ -4,6 +4,7 @@ import 'package:angel3_framework/angel3_framework.dart'; import 'package:angel3_orm/angel3_orm.dart'; import 'package:angel3_orm_mysql/angel3_orm_mysql.dart'; import 'package:mysql1/mysql1.dart'; +//import 'package:mysql_client/mysql_client.dart'; // For MariaDb Future configureServer(Angel app) async { @@ -19,11 +20,12 @@ Future configureServer(Angel app) async { } } +// MariaDB connection Future connectToMariaDb(Map configuration) async { - var mariaDbConfig = configuration['mariadb'] as Map? ?? {}; + var mariaDbConfig = configuration['mysql'] as Map? ?? {}; var settings = ConnectionSettings( host: mariaDbConfig['host'] as String? ?? 'localhost', - port: mariaDbConfig['port'] as int? ?? 5432, + port: mariaDbConfig['port'] as int? ?? 3306, db: mariaDbConfig['database_name'] as String? ?? Platform.environment['USER'] ?? Platform.environment['USERNAME'] ?? @@ -37,3 +39,38 @@ Future connectToMariaDb(Map configuration) async { var connection = await MySqlConnection.connect(settings); return connection; } + +// For Mysql +/* +Future configureServer(Angel app) async { + try { + var connection = await connectToMysql(app.configuration); + var executor = MySqlExecutor(connection, logger: app.logger); + + app + ..container.registerSingleton(executor) + ..shutdownHooks.add((_) => connection.close()); + } catch (e) { + app.logger.severe("Failed to connect to MySQL. ORM disabled.", e); + } +} + +// Mysql Connection +Future connectToMysql(Map configuration) async { + var mysqlConfig = configuration['mysql'] as Map? ?? {}; + + var connection = await MySQLConnection.createConnection( + host: mysqlConfig['host'] as String? ?? 'localhost', + port: mysqlConfig['port'] as int? ?? 3306, + databaseName: mysqlConfig['database_name'] as String? ?? + Platform.environment['USER'] ?? + Platform.environment['USERNAME'] ?? + '', + userName: mysqlConfig['username'] as String? ?? '', + password: mysqlConfig['password'] as String? ?? '', + secure: mysqlConfig['use_ssl'] as bool? ?? false); + + await connection.connect(timeoutMs: 10000); + return connection; +} +*/