Finch uses mysql_client package for MySQL database operations. You can use the mysql_client package to perform database operations in your Finch application.

Connect to MySQL Database

To connect to MySQL database, you need to add the following code to your app.dart file.

import 'package:finch/finch.dart';

void main() async {
  final configs = FinchConfigs(
    mysqlConfig: FinchMysqlConfig(
      enable: true,
      host: 'localhost',
      port: 3306,
      user: 'database_username',
      pass: 'database_password',
      databaseName: 'database_name',
    ),
  );

  final server = FinchApp(configs: configs);

  server.start().then((value) {
    Console.p("Server started: http://localhost:${value.port}");
  });
}

In the above example, we have enabled the MySQL database and specified the host, port, username, password, and database name. You can change these values to match your database configuration.

Use MySQL Database

To use the MySQL database, you can use the app.mysqlDb property. This property returns the DatabaseDriver object that you can use to perform database operations.

var db = app.mysqlDb;

Database Models (Sql based tables)

Finch provides a simple way to create database models for MySQL database. You can use the MTable class to define the structure of your database table. The MTable class provides a simple way to define the fields of your table. You can use the MField classes to define the fields of your table.

import 'package:finch/finch.dart';

final table = MTable(
  name: 'users',
  fields: [
    MFieldInt(name: 'id', isPrimaryKey: true, isAutoIncrement: true),
    MFieldVarchar(name: 'name', length: 255),
    MFieldVarchar(name: 'email', length: 255),
  ],
);

In the above example, we have defined a table named users with three fields: id, name, and email. The id field is defined as an integer and is set as the primary key and auto-incremented. The name and email fields are defined as varchar with a maximum length of 255 characters.

You can use the MTable class to create tables for your application. You can also use the MTable class to create relationships between tables. For example, you can define a foreign key relationship between two tables.

Sqler

Finch uses sqler package to build SQL queries. You can use the sqler package to build SQL queries for your MySQL database. You can use the sqler package to build complex queries with ease.

Example

For more information, you can check the example project in the Finch repository. The example project uses MySQL database and Finch's database features.