首頁  >  文章  >  web前端  >  Mongogrator - 用於 TS 和 JS 的 MongoDB 遷移工具

Mongogrator - 用於 TS 和 JS 的 MongoDB 遷移工具

Barbara Streisand
Barbara Streisand原創
2024-10-02 06:40:30877瀏覽

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

這將列印所有遷移的列表,每個遷移的狀態為「未遷移」或「已遷移」

┌───┬───────────────────────────────┬──────────────┐
│   │ migration                     │ status       │
├───┼───────────────────────────────┼──────────────┤
│ 0 │ 20240923150201806_insert_user │ NOT MIGRATED │
└───┴───────────────────────────────┴──────────────┘

當然,狀態將是 NOT MIGRATED,因為我們還沒有運行遷移

運行遷移

只需呼叫即可運行遷移

mongogrator migrate

這將運行所有遷移並將它們記錄到配置logsCollectionName中指定集合名稱下的資料庫

出於生產目的,如果在同一路徑下無法訪問,您可以直接將配置路徑傳遞給 migrate 命令

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn