Template for MariaDB

This commit is contained in:
thomashii 2022-05-01 17:46:14 +08:00
parent 985100c271
commit 0933a7f727
3 changed files with 50 additions and 3 deletions

View file

@ -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<String> args) async {
GreetingMigration(),
]);
// MySQL database
/*
var connection = await connectToMysql(configuration);
var migrationRunner = MySqlMigrationRunner(connection, migrations: [
GreetingMigration(),
]);
*/
await runMigrations(migrationRunner, args);
}

View file

@ -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

View file

@ -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<void> configureServer(Angel app) async {
@ -19,11 +20,12 @@ Future<void> configureServer(Angel app) async {
}
}
// MariaDB connection
Future<MySqlConnection> 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<MySqlConnection> connectToMariaDb(Map configuration) async {
var connection = await MySqlConnection.connect(settings);
return connection;
}
// For Mysql
/*
Future<void> configureServer(Angel app) async {
try {
var connection = await connectToMysql(app.configuration);
var executor = MySqlExecutor(connection, logger: app.logger);
app
..container.registerSingleton<QueryExecutor>(executor)
..shutdownHooks.add((_) => connection.close());
} catch (e) {
app.logger.severe("Failed to connect to MySQL. ORM disabled.", e);
}
}
// Mysql Connection
Future<MySQLConnection> 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;
}
*/