Mongogrator - TS および JS 用の MongoDB 移行ツール

Barbara Streisand
リリース: 2024-10-02 06:40:30
オリジナル
883 人が閲覧しました

Mongogrator - a MongoDB migration tool for TS & JS

リポジトリはこちら

Mongogrator は、MongoDB 用の非常に高速なデータベース移行 CLI です。その目的は、開発段階と実稼働段階の移行を簡単に作成して実行することです

インストール中

次のコマンドを使用すると、Mongogrator が自動的にダウンロード、インストールされ、PATH に追加されます

MacOS/Linux

curl -fsSL git.new/mongogrator-installer.sh | bash
ログイン後にコピー

powershell -c "irm git.new/mongogrator-installer.ps1 | iex"
ログイン後にコピー

コマンド一覧

Mongogrator CLI
Usage: mongogrator <command> [options]

Commands:
   init [--js]               Initialize a new configuration file
   add                       Creates a new migration file with the provided name
   list                      List all migrations and their status
   migrate [config_path]     Run all migrations that have not been applied yet
   version, -v, --version    Prints the current version of Mongogrator

Flags:
   --help, -h                Prints the detailed description of the command
ログイン後にコピー

ご利用ガイド

CLI の使用方法に関する基本ガイド

新しい移行の追加

まず設定ファイルを初期化します

mongogrator init
ログイン後にコピー

これにより、コマンドの場所にある mongogrator.config.ts ファイルが初期化されます。オプションで、コマンドの最後に --js フラグを渡して js ファイルで初期化できます

目的の mongo クラスターへの URL を設定し、それが実行されていることを確認します

mongogrator add insert_user
ログイン後にコピー

これにより、構成 migrationsPath で割り当てられたディレクトリ キーの下に移行ファイルが作成されます

以下は新しく作成された ts 移行ファイルの例です

import type { Db } from 'mongodb'

/**
 * This function is called when the migration is run.
 * @param _db The mongodb database object that's passed to the migration
 */
export const migrate = async (_db: Db): Promise<void> => {
  // Migration code here
}
ログイン後にコピー

移行はネイティブ MongoDB Node.js ドライバーを通じて実行されます

移行クエリの例

import type { Db } from 'mongodb'

/**
 * This function is called when the migration is run.
 * @param _db The mongodb database object that's passed to the migration
 */
export const migrate = async (_db: Db): Promise<void> => {
  // Migration code here
  _db.collection('users').insertOne({ name: 'Alex' })
}
ログイン後にコピー

移行リスト

必要なだけ移行を追加し、list コマンドを呼び出して各移行のステータスを表示できます

mongogrator list
ログイン後にコピー

これにより、すべての移行のリストが出力されます。それぞれのステータスは NOT MIGRATED または MIGRATED です

┌───┬───────────────────────────────┬──────────────┐
│   │ migration                     │ status       │
├───┼───────────────────────────────┼──────────────┤
│ 0 │ 20240923150201806_insert_user │ NOT MIGRATED │
└───┴───────────────────────────────┴──────────────┘
ログイン後にコピー

まだ移行を実行していないため、当然のことながら、ステータスは NOT MIGRATED になります

移行の実行

呼び出すだけで移行を実行できます

mongogrator migrate
ログイン後にコピー

これにより、すべての移行が実行され、構成 logsCollectionName で指定されたコレクション名でデータベースにログが記録されます

本番環境の場合、同じパスでアクセスできない場合は、構成パスを直接移行コマンドに渡すことができます

mongogrator migrate /dist
ログイン後にコピー

ここで list コマンドを再度実行すると、移行ファイルが正常に実行されたことがわかります

┌───┬───────────────────────────────┬──────────────┐
│   │ migration                     │ status       │
├───┼───────────────────────────────┼──────────────┤
│ 0 │ 20240923150201806_insert_user │ MIGRATED     │
└───┴───────────────────────────────┴──────────────┘
ログイン後にコピー

ログ収集スキーマ

{
  _id: objectId(),
  name: string,
  createdAt: Date(),
}
ログイン後にコピー

構成

{
  url: 'mongodb://localhost:27017', // Cluster url
  database: 'test', // Database name for which the migrations will be executed
  migrationsPath: './migrations', // Migrations directory relative to the location of the commands
  logsCollectionName: 'migrations', // Name of the logs collection that will be stored in the database
  format: 'ts', // Format type of the migration files ['ts', 'js']
}
ログイン後にコピー

パス値を持つすべての設定キーは、設定ファイル自体の場所に相対的です

以上がMongogrator - TS および JS 用の MongoDB 移行ツールの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!