首页 > web前端 > js教程 > 正文

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
登录后复制

这将打印出所有迁移的列表,每个迁移的状态为“未迁移”或“已迁移”

┌───┬───────────────────────────────┬──────────────┐
│   │ 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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!