Giter VIP home page Giter VIP logo

actixweb-seaorm-example's Introduction

Read Me

サービス

サービスの配置

各サービスは、servicesモジュールにエントリポイントを作成する。 エントリポイントとなるmain関数を持つファイルは、services/src/bin/ディレクトリに配置する。

サービスの実行

各サービスは、以下の通り実行する。

cargo run --bin <main-fun-file>

# Web APIサービスを起動する場合
cargo run --bin web_api_service

モジュール構成

モジュール名 内容
services サービスのエントリポイントを配置
adapters サービスとユースケースを接続するアダプタを配置
usecases ユースケースで実行する処理を配置
infra データストアを配置
domains エンティティ、値オブジェクトを配置
common 上記モジュールから呼び出す一般的な機能を配置

モジュールの依存関係

  • services -> common
  • services -> adapters
  • adapters -> common
  • adapters -> usecases
  • adapters -> infra
  • infrastructures -> usecases
  • infrastructures -> domains
  • usecases -> common
  • usecases -> domains
  • domains -> common

sqlx

準備

sqlx-cliを以下の通りインストールする。

cargo install sqlx-cli --no-default-features --features rustls,postgres

.envファイルを以下の通り作成する。

DATABASE_URL=postgres://postgres:password@localhost:5432/axum-seaorm-ddd-example

Dockerサービスを起動した後、Dockerコンテナを以下の通り起動する。

docker-compose up -d    # d: バックグラウンドでコンテナを起動

Dockerコマンド

コマンド 説明
up コンテナの構築、作成、起動、アタッチ。
down コンテナを停止して、upで作成したコンテナ、ネットワーク、ボリューム及びイメージを削除。
start 既存のコンテナを起動。
stop 起動中のコンテナを削除。
restart 起動中のコンテナを再起動。
kill 起動中のコンテナにシグナルを送信。-sオプションでシグナルを指定。デフォルトのシグナルはSIGKILL

データベースの作成

データベースを以下の通り作成する。

sqlx database create

# データベースを削除する場合
# sqlx database drop

マイグレーションファイルの作成と実行

マイグレーションファイルを以下の通り作成する。

sqlx migrate add <name>

migrations/<timestamp>-<name>.sqlファイルが作成される。 このファイルにデータベーススキーマの変更を記述する。

マイグレーションを以下の通り実行する。

sqlx migrate run

実行しているデータベースのマイグレーション履歴と、migrationsディレクトリのマイグレーションファイルを比較して、実行されていないスクリプトを実行する。

マイグレーションを戻す

updownスクリプトに対応する戻すことが可能なマイグレションを作成したい場合は、マイグレーションを作成するときに、-rフラグを追加する。

sqlx migrate add -r <name>
# Creating migrations/<timestamp>_<name>.up.sql
# Creating migrations/<timestamp>_<name>.down.sql

その後、マイグレーションを以下の通り実行する。

sqlx migrate run
# Applied <timestamp>-<name>

マイグレーションを戻す場合は以下を実行する。

sqlx migrate revert
# Applied <timestamp>/revert <name>

戻すことが不可能なマイグレーションと、戻すことが可能なマイグレーションを混在させると、エラーが発生する。

# 戻すことが不可能なマイグレーションを作成
sqlx migrate add <name1>
Creating migrations/20211001154420_<name>.sql

# 戻すことが可能なマイグレーションを作成(エラーが発生)
sqlx migrate add -r <name2>
error: cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations

Actix Web

バージョン3の安定バージョンがSeaORMが依存しているtokioとバージョンが合わないため、バージョン4.0.0-rc.3を使用している。

SeaORM

準備

sea-orm-cliコマンドを以下の通りインストールする。

cargo install sea-orm-cli

エンティティファイルの作成

データベースに存在するテーブルを表現するエンティティファイルを、以下の通り作成する。

sea-orm-cli generate entity -o infrastructures/src/postgres/schema

Cargo.toml

注意: 以下で説明する方法で、開発環境に限定してmockを有効にできなかった。

SeaORMMockDatabaseConnectionCloneを実装しない。 よって、axumExtensionでデータベースコネクションを状態として記録できない。 このため、SeaORMmockフィーチャを、以下の通り開発環境のみで有効にする。

[dependencies.sea-orm]
version = "0.5"
features = ["sqlx-postgres", "runtime-tokio-rustls", "macros", "debug-print"]
default-features = false

[dev-dependencies.sea-orm]
version = "0.5"
features = ["sqlx-postgres", "runtime-tokio-rustls", "macros", "debug-print", "mock"]
default-features = false

JWT

トークンはデータベースに蓄積され続けるため、定期的にデータベースに記録しているJWTトークンを削除する必要がある。

actixweb-seaorm-example's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.