platform/packages/orm/angel_orm_mysql/README.md

109 lines
3.9 KiB
Markdown
Raw Normal View History

2021-07-25 13:55:58 +00:00
# Angel3 ORM for MySQL
2021-12-20 04:25:43 +00:00
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/angel3_orm_mysql?include_prereleases)
2021-06-18 10:15:23 +00:00
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
2024-07-07 15:02:49 +00:00
[![Discord](https://img.shields.io/discord/1060322353214660698)](https://discord.gg/3X6bxTUdCM)
2023-12-25 03:45:10 +00:00
[![License](https://img.shields.io/github/license/dart-backend/angel)](https://github.com/dart-backend/angel/tree/master/packages/orm/angel_orm_mysql/LICENSE)
2021-06-18 10:15:23 +00:00
2023-12-24 01:52:57 +00:00
This package contains the SQL executor required by Angel3 ORM to work with MySQL or MariaDB. In order to better support both MySQL and MariaDB, two different flavors of drives have been included; `mysql_client` and `mysql1`. They are implmented as `MySqlExecutor` and `MariaDbExecutor` respectively.
2021-06-18 10:15:23 +00:00
2022-07-24 04:00:10 +00:00
## Supported databases
2022-01-02 07:21:17 +00:00
2022-07-24 04:00:10 +00:00
* MariaDD 10.2.x or greater
* MySQL 8.x or greater
2022-01-02 07:21:17 +00:00
2022-07-24 04:00:10 +00:00
**Note** MySQL below version 8.0 and MariaDB below version 10.2.0 are not supported as Angel3 ORM requires common table expressions (CTE) to work.
2022-01-02 07:21:17 +00:00
2022-07-24 04:00:10 +00:00
## MySqlExecutor
2022-04-23 04:21:39 +00:00
2022-07-24 04:00:10 +00:00
This SQL executor is implemented using [`mysql_client`](https://pub.dev/packages?q=mysql_client) driver. It works with both `MySQL` 8.0+ and `MariaDB` 10.2+ database.
2022-04-23 04:21:39 +00:00
2022-07-24 04:00:10 +00:00
### Connecting to MySQL or MariaDB
2022-04-23 04:21:39 +00:00
```dart
2022-04-24 02:59:03 +00:00
import 'package:mysql_client/mysql_client.dart';
2022-04-23 04:21:39 +00:00
var connection = await MySQLConnection.createConnection(
host: "localhost",
port: 3306,
databaseName: "orm_test",
userName: "test",
2022-07-16 02:44:49 +00:00
password: "test123",
2022-07-24 04:00:10 +00:00
secure: true);
2022-04-23 04:21:39 +00:00
var logger = Logger('orm_mysql');
await connection.connect(timeoutMs: 10000);
var executor = MySqlExecutor(connection, logger: logger);
```
2022-05-16 05:04:21 +00:00
2022-07-24 04:00:10 +00:00
### Known Limitation for MySqlExecutor
2022-07-16 02:44:49 +00:00
2022-07-24 04:00:10 +00:00
* `Blob` data type mapping is not support.
* `timestamp` data type mapping is not supported. Use `datetime` instead.
* UTC datetime is not supported.
2022-07-16 02:44:49 +00:00
2022-07-24 04:00:10 +00:00
## MariaDBExecutor
This SQL executor is implemented using [`mysql1`](https://pub.dev/packages?q=mysql1) driver. It only works with `MariaDB` 10.2+ database. Do not use this for `MySQL` 8.0+ database.
### Connecting to MariaDB
```dart
import 'package:mysql1/mysql1.dart';
var settings = ConnectionSettings(
host: 'localhost',
port: 3306,
db: 'orm_test',
user: 'test',
password: 'test123');
var connection = await MySqlConnection.connect(settings);
var logger = Logger('orm_mariadb');
var executor = MariaDbExecutor(connection, logger: logger);
```
### Known Limitation for MariaDBExecutor
* `Blob` type mapping is not supported.
* `timestamp` mapping is not supported. Use `datetime` instead.
* Only UTC datetime is supported. None UTC datetime will be automatically converted into UTC datetime.
## Creating a new database in MariaDB or MySQL
2022-07-16 02:44:49 +00:00
1. Login to MariaDB/MySQL database console with the following command.
2023-12-24 01:52:57 +00:00
```bash
mysql -u root -p
```
2022-07-16 02:44:49 +00:00
2023-12-24 01:52:57 +00:00
2. Run the following commands to create a new database, `orm_test`, and grant both local and remote access to user, `test`. Replace `orm_test`, `test` and `test123` with your own database name, username and password.
2022-07-16 02:44:49 +00:00
2023-12-24 01:52:57 +00:00
```mysql
create database orm_test;
-- Granting localhost access only
create user 'test'@'localhost' identified by 'test123';
grant all privileges on orm_test.* to 'test'@'localhost';
2022-07-16 02:44:49 +00:00
2023-12-24 01:52:57 +00:00
-- Granting localhost and remote access
create user 'test'@'%' identified by 'test123';
grant all privileges on orm_test.* to 'test'@'%';
```
2022-07-16 02:44:49 +00:00
2022-07-24 04:00:10 +00:00
## Compatibility Matrix
2022-07-16 02:44:49 +00:00
2022-07-24 04:00:10 +00:00
### MariaDB 10.2+
2022-07-16 02:44:49 +00:00
2022-07-24 04:00:10 +00:00
| | Create | Read | Update | Delete |
|-----------------|--------|--------|--------|--------|
| MySqlExecutor | Y | Y | Y | Y |
| MariaDBExecutor | Y | Y | Y | Y |
2022-07-16 02:44:49 +00:00
2022-07-24 04:00:10 +00:00
### MySQL 8.0+
2022-07-16 02:44:49 +00:00
2022-07-24 04:00:10 +00:00
| | Create | Read | Update | Delete |
|-----------------|--------|--------|--------|--------|
| MySqlExecutor | Y | Y | Y | Y |
| MariaDBExecutor | N | N | N | N |