@ frabbit/ mongo- migrate- ts (法语)
又一个叉子...
一个使用 TypeScript 在mongodb 上简单运行迁移的库.
根据移徙-蒙戈(https://github.com/seppevs/migrate-mongo/ 互联网档案馆的存檔,存档日期2013-12-21),但得到了TypeScript的支持.
安装
使用您最喜欢的软件包管理器安装, 例如使用 npm
npm install @frabbit/mongo-migrate-ts
您可以在全球安装用于 CLI
npm install -g @frabbit/mongo-migrate-ts
使用量
CLI 选项
Usage: mongo-migrate [options] [command]
Options:
-h, --help output usage information
Commands:
init Creates the migrations directory and configuration file
new [options] Create a new migration file under migrations directory
up Run all pending migrations
down [options] Undo migrations
status Show the status of the migrations
为您的迁移创建目录, 并即时创建 CLI
import { mongoMigrateCli } from '@frabbit/mongo-migrate-ts';
mongoMigrateCli({
uri: 'mongodb://username:[email protected]:27017',
migrationsDir: __dirname,
migrationsCollection: 'migrations_collection',
});
在配置的迁移文件夹中创建迁移文件...
import { MigrationInterface } from '@frabbit/mongo-migrate-ts';
import { Db } from 'mongodb';
export class MyMigration implements MigrationInterface {
async up(db: Db): Promise<any> {
await db.createCollection('my_collection');
}
async down(db: Db): Promise<any> {
await db.dropCollection('my_collection');
}
}
汇编和增加所有迁移
tsc migrations/index.js && node build/migrations/index.js up
或用 ts- 节点直接运行
ts-node migrations/index.ts up
配置
{
// The path where the migrations are stored
migrationsDir: string;
// The name of the collection to store the applied migrations
// (Default: "migrations_changelog")
migrationsCollection?: string;
// The connection uri, it can be empty if useEnv is true
// (Example: mongodb://user:[email protected]:27017/db?authSource=admin)
uri?: string;
// The database where run the migrations
// it can be empty if the database is on the uri or useEnv is true
database?: string;
// If true, will load the configuration from environment variables.
useEnv?: boolean;
// Options related to environment configuration
environment?: {
// The name of the environment variable with the uri connection
// (Default: MONGO_MIGRATE_URI)
uriVar?: string;
// The name of the environment variable with the db name
// (Default: MONGO_MIGRATE_DB)
databaseVar?: string;
};
// Specific configuration of mongodb client
// (see https://mongodb.github.io/node-mongodb-native/4.3/interfaces/MongoClientOptions.html)
options?: MongoClientOptions;
}
Json 中的示例配置
{
"uri": "mongodb://admin:[email protected]:27017/mydb?authSource=admin",
"migrationsDir": "migrations"
}