项目作者: mycodeself

项目描述 :
Run mongodb migrations easy from TypeScript
高级语言: TypeScript
项目地址: git://github.com/mycodeself/mongo-migrate-ts.git
创建时间: 2019-10-20T17:27:43Z
项目社区:https://github.com/mycodeself/mongo-migrate-ts

开源协议:MIT License

下载


mongo-migrate-ts

CircleCI

A library for easy run migrations on mongodb with TypeScript.

Based on migrate-mongo (https://github.com/seppevs/migrate-mongo/), but with TypeScript support.

Installation

Install using your favourite package manager, example using npm

  1. npm install mongo-migrate-ts

You can install it globally for the CLI usage

  1. npm install -g mongo-migrate-ts

Usage

CLI options

  1. Usage: mongo-migrate [options] [command]
  2. Options:
  3. -h, --help output usage information
  4. Commands:
  5. init Creates the migrations directory and configuration file
  6. new [options] Create a new migration file under migrations directory
  7. up Run all pending migrations
  8. down [options] Undo migrations
  9. status Show the status of the migrations

Create a directory for your migrations.

  1. mongo-migrate init

Instantiate a CLI within the newly created migrations directory

  1. // index.ts in this example
  2. import { mongoMigrateCli } from 'mongo-migrate-ts';
  3. mongoMigrateCli({
  4. uri: 'mongodb://username:password@0.0.0.0:27017',
  5. database: 'db',
  6. migrationsDir: __dirname,
  7. migrationsCollection: 'migrations_collection',
  8. });

Create a migration file in the configured migrations folder…

  1. mongo-migrate new
  1. import { MigrationInterface } from 'mongo-migrate-ts';
  2. import { Db, MongoClient } from 'mongodb';
  3. export class MyMigration implements MigrationInterface {
  4. async up(db: Db, client: MongoClient): Promise<void | never> {
  5. await db.createCollection('my_collection');
  6. }
  7. async down(db: Db, client: MongoClient): Promise<void | never> {
  8. await db.dropCollection('my_collection');
  9. }
  10. }

Compile and up all migrations

  1. tsc migrations/index.js && node build/migrations/index.js up

or run directly with ts-node

  1. ts-node migrations/index.ts up

Configuration

  1. {
  2. // The path where the migrations are stored
  3. migrationsDir: string;
  4. // The name of the collection to store the applied migrations
  5. // (Default: "migrations_changelog")
  6. migrationsCollection?: string;
  7. // The glob pattern for migration scripts
  8. // (Default: isTsNode() ? '**/*.ts' : '**/*.js'
  9. globPattern?: string;
  10. // The glob options for pattern matching
  11. // (see https://github.com/isaacs/node-glob#options)
  12. // (Default: { cwd: migrationsDir })
  13. globOptions?: string;
  14. // The connection uri, it can be empty if useEnv is true
  15. // (Example: mongodb://user:password@127.0.0.1:27017/db?authSource=admin)
  16. uri?: string;
  17. // The database where run the migrations
  18. // it can be empty if the database is on the uri or useEnv is true
  19. database?: string;
  20. // If true, will load the configuration from environment variables.
  21. useEnv?: boolean;
  22. // Options related to environment configuration
  23. environment?: {
  24. // The name of the environment variable with the uri connection
  25. // (Default: MONGO_MIGRATE_URI)
  26. uriVar?: string;
  27. // The name of the environment variable with the db name
  28. // (Default: MONGO_MIGRATE_DB)
  29. databaseVar?: string;
  30. };
  31. // The format pattern for timestamp in the migration file name. By default: 'T'
  32. // (see https://date-fns.org/v2.30.0/docs/format)
  33. migrationNameTimestampFormat?: string;
  34. // Specific configuration of mongodb client
  35. // (see https://mongodb.github.io/node-mongodb-native/4.3/interfaces/MongoClientOptions.html)
  36. options?: MongoClientOptions;
  37. }

Example configuration in json

  1. {
  2. "uri": "mongodb://admin:admin@127.0.0.1:27017/mydb?authSource=admin",
  3. "migrationsDir": "migrations",
  4. "migrationNameTimestampFormat": "yyyyMMddHHmmss"
  5. }

Transactions

The up and down methods in migrations have the mongo client available to create a session and use transactions. See example

  1. import { Db, MongoClient } from 'mongodb';
  2. import { MigrationInterface } from '../../lib';
  3. export class Transaction1691171075957 implements MigrationInterface {
  4. public async up(db: Db, client: MongoClient): Promise<void | never> {
  5. const session = client.startSession();
  6. try {
  7. await session.withTransaction(async () => {
  8. await db.collection('mycol').insertOne({ foo: 'one' });
  9. await db.collection('mycol').insertOne({ foo: 'two' });
  10. await db.collection('mycol').insertOne({ foo: 'three' });
  11. });
  12. } finally {
  13. await session.endSession();
  14. }
  15. }
  16. public async down(db: Db, client: MongoClient): Promise<void | never> {
  17. const session = client.startSession();
  18. try {
  19. await session.withTransaction(async () => {
  20. await db.collection('mycol').deleteOne({ foo: 'one' });
  21. await db.collection('mycol').deleteOne({ foo: 'two' });
  22. await db.collection('mycol').deleteOne({ foo: 'three' });
  23. });
  24. } finally {
  25. await session.endSession();
  26. }
  27. }
  28. }