{"id":21277653,"url":"https://github.com/mycodeself/mongo-migrate-ts","last_synced_at":"2025-04-08T15:14:28.353Z","repository":{"id":35420809,"uuid":"216399958","full_name":"mycodeself/mongo-migrate-ts","owner":"mycodeself","description":"Run mongodb migrations easy from TypeScript","archived":false,"fork":false,"pushed_at":"2024-11-18T22:45:54.000Z","size":1725,"stargazers_count":91,"open_issues_count":11,"forks_count":28,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T14:14:15.229Z","etag":null,"topics":["database","migrations","mongo-migrate","mongodb","npm","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mycodeself.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-20T17:27:43.000Z","updated_at":"2024-12-24T16:20:25.000Z","dependencies_parsed_at":"2023-12-16T19:44:13.567Z","dependency_job_id":"7cf00f9b-5c72-43ec-97a1-540d87fccccf","html_url":"https://github.com/mycodeself/mongo-migrate-ts","commit_stats":{"total_commits":72,"total_committers":9,"mean_commits":8.0,"dds":0.5416666666666667,"last_synced_commit":"c7ecb1cf094ae277bd279da091b48076d44137e5"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mycodeself%2Fmongo-migrate-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mycodeself%2Fmongo-migrate-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mycodeself%2Fmongo-migrate-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mycodeself%2Fmongo-migrate-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mycodeself","download_url":"https://codeload.github.com/mycodeself/mongo-migrate-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247867362,"owners_count":21009240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","migrations","mongo-migrate","mongodb","npm","typescript"],"created_at":"2024-11-21T10:06:38.441Z","updated_at":"2025-04-08T15:14:28.331Z","avatar_url":"https://github.com/mycodeself.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongo-migrate-ts\n\n[![CircleCI](https://circleci.com/gh/mycodeself/mongo-migrate-ts.svg?style=svg)](https://circleci.com/gh/mycodeself/mongo-migrate-ts)\n\nA library for easy run migrations on mongodb with TypeScript.\n\nBased on migrate-mongo (https://github.com/seppevs/migrate-mongo/), but with TypeScript support.\n\n## Installation\n\nInstall using your favourite package manager, example using npm\n\n```\nnpm install mongo-migrate-ts\n```\n\nYou can install it globally for the CLI usage\n\n```\nnpm install -g mongo-migrate-ts\n```\n\n## Usage\n\n### CLI options\n\n```\nUsage: mongo-migrate [options] [command]\n\nOptions:\n  -h, --help      output usage information\n\nCommands:\n  init            Creates the migrations directory and configuration file\n  new [options]   Create a new migration file under migrations directory\n  up              Run all pending migrations\n  down [options]  Undo migrations\n  status          Show the status of the migrations\n```\n\nCreate a directory for your migrations.\n```bash\nmongo-migrate init\n```\n\nInstantiate a CLI within the newly created migrations directory\n```typescript\n// index.ts in this example\nimport { mongoMigrateCli } from 'mongo-migrate-ts';\n\nmongoMigrateCli({\n  uri: 'mongodb://username:password@0.0.0.0:27017',\n  database: 'db',\n  migrationsDir: __dirname,\n  migrationsCollection: 'migrations_collection',\n});\n```\n\nCreate a migration file in the configured migrations folder...\n```bash\nmongo-migrate new\n```\n\n```typescript\nimport { MigrationInterface } from 'mongo-migrate-ts';\nimport { Db, MongoClient } from 'mongodb';\n\nexport class MyMigration implements MigrationInterface {\n  async up(db: Db, client: MongoClient): Promise\u003cvoid | never\u003e {\n    await db.createCollection('my_collection');\n  }\n\n  async down(db: Db, client: MongoClient): Promise\u003cvoid | never\u003e {\n    await db.dropCollection('my_collection');\n  }\n}\n```\n\nCompile and up all migrations\n\n```\ntsc migrations/index.js \u0026\u0026 node build/migrations/index.js up\n```\n\nor run directly with ts-node\n\n```\nts-node migrations/index.ts up\n```\n\n## Configuration\n\n```typescript\n{\n  // The path where the migrations are stored\n  migrationsDir: string;\n  // The name of the collection to store the applied migrations\n  // (Default: \"migrations_changelog\")\n  migrationsCollection?: string;\n  // The glob pattern for migration scripts\n  // (Default: isTsNode() ? '**/*.ts' : '**/*.js'\n  globPattern?: string;\n  // The glob options for pattern matching\n  // (see https://github.com/isaacs/node-glob#options)\n  // (Default: { cwd: migrationsDir })\n  globOptions?: string;  \n  // The connection uri, it can be empty if useEnv is true\n  // (Example: mongodb://user:password@127.0.0.1:27017/db?authSource=admin)\n  uri?: string;\n  // The database where run the migrations\n  // it can be empty if the database is on the uri or useEnv is true\n  database?: string;\n  // If true, will load the configuration from environment variables.\n  useEnv?: boolean;\n  // Options related to environment configuration\n  environment?: {\n    // The name of the environment variable with the uri connection\n    // (Default: MONGO_MIGRATE_URI)\n    uriVar?: string;\n    // The name of the environment variable with the db name\n    // (Default: MONGO_MIGRATE_DB)\n    databaseVar?: string;\n  };\n  // The format pattern for timestamp in the migration file name. By default: 'T'\n  // (see https://date-fns.org/v2.30.0/docs/format)\n  migrationNameTimestampFormat?: string;\n  // Specific configuration of mongodb client\n  // (see https://mongodb.github.io/node-mongodb-native/4.3/interfaces/MongoClientOptions.html)\n  options?: MongoClientOptions;\n}\n```\n\nExample configuration in json\n\n```json\n{\n  \"uri\": \"mongodb://admin:admin@127.0.0.1:27017/mydb?authSource=admin\",\n  \"migrationsDir\": \"migrations\",\n  \"migrationNameTimestampFormat\": \"yyyyMMddHHmmss\"\n}\n```\n\n## Transactions\n\nThe `up` and `down` methods in migrations have the mongo client available to create a session and use transactions. See example\n\n```typescript\nimport { Db, MongoClient } from 'mongodb';\nimport { MigrationInterface } from '../../lib';\n\nexport class Transaction1691171075957 implements MigrationInterface {\n  public async up(db: Db, client: MongoClient): Promise\u003cvoid | never\u003e {\n    const session = client.startSession();\n    try {\n      await session.withTransaction(async () =\u003e {\n        await db.collection('mycol').insertOne({ foo: 'one' });\n        await db.collection('mycol').insertOne({ foo: 'two' });\n        await db.collection('mycol').insertOne({ foo: 'three' });\n      });\n    } finally {\n      await session.endSession();\n    }\n  }\n\n  public async down(db: Db, client: MongoClient): Promise\u003cvoid | never\u003e {\n    const session = client.startSession();\n    try {\n      await session.withTransaction(async () =\u003e {\n        await db.collection('mycol').deleteOne({ foo: 'one' });\n        await db.collection('mycol').deleteOne({ foo: 'two' });\n        await db.collection('mycol').deleteOne({ foo: 'three' });\n      });\n    } finally {\n      await session.endSession();\n    }\n  }\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmycodeself%2Fmongo-migrate-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmycodeself%2Fmongo-migrate-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmycodeself%2Fmongo-migrate-ts/lists"}