# @msafe/sui3-model

Database and model for MSafe V3 SUI.

## Installation

```shell
yarn add @msafe/sui3-model
```

## Usage

There are 3 models, each can be accessed by different services.

* sync: Pull latest data from blockchain and write raw data to database.
* core: MSafe multi-sig business logic.
* stream: MPay service, which is stream payment.

```shell
import { CoreModel } from '@msafe/sui3-model/core';
import { StreamModel } from '@msafe/sui3-model/stream';
import { SyncModel } from '@msafe/sui3-model/sync';

const LOCAL_DB_CONFIG: ModelConfig = {
  host: '127.0.0.1',
  port: 3306,
  username: 'msafe',
  password: 'msafe',
  database: 'msafe_sui_local',
  logging: false,
};

const core = await CoreModel.New(LOCAL_DB_CONFIG);
// Handle core model queries

const sync = await SyncModel.New(LOCAL_DB_CONFIG);
// Handle core model queries

const stream = await StreamModel.New(LOCAL_DB_CONFIG);
// Handle core model queries
```

For each module, the `Repository` and `DataSource` from typeOrm is exported
in each instance. Please refer to
[TypeOrm documentation](https://typeorm.io/) for more database operations.

## Appendix

### Install and initialize mysql on local

**MacOS**

```shell
brew install mysql
mysql -u root

> create database msafe_sui_local;
```

Create a new user and grant permission to database

```shell
> CREATE USER 'msafe'@'localhost' IDENTIFIED BY 'msafe';
> use msafe_sui_local;
> GRANT ALL PRIVILEGES ON msafe_sui_local.* TO 'msafe'@'localhost'
> exit
```

Test our new user connection:

```shell
mysql -u msafe -p
// Input password `msafe`
use msafe_sui_local
```

Now you have user and database set.

### Scripts

**Database initialization**

After the database has been created, use `scripts/initialize.ts` to initialize the
database schema.

```shell
yarn initialize config/config.local.yaml
```

> To access database for other environments, please request the config file
> from admin.
