From 24a30d0f2e6db719f3fcdc72f89157ea0cdd7ed9 Mon Sep 17 00:00:00 2001 From: Anton Lobashev Date: Thu, 18 Oct 2018 21:23:44 +0400 Subject: [PATCH] Add types for mongration (#29843) * Add types for mongration * Make MigrationResponse status type more explicit --- types/mongration/index.d.ts | 35 ++++++++++++++++++++++++++++ types/mongration/mongration-tests.ts | 28 ++++++++++++++++++++++ types/mongration/tsconfig.json | 23 ++++++++++++++++++ types/mongration/tslint.json | 1 + 4 files changed, 87 insertions(+) create mode 100644 types/mongration/index.d.ts create mode 100644 types/mongration/mongration-tests.ts create mode 100644 types/mongration/tsconfig.json create mode 100644 types/mongration/tslint.json diff --git a/types/mongration/index.d.ts b/types/mongration/index.d.ts new file mode 100644 index 0000000000..7f46f3edc3 --- /dev/null +++ b/types/mongration/index.d.ts @@ -0,0 +1,35 @@ +// Type definitions for mongration 1.0 +// Project: https://github.com/awapps/mongration#readme +// Definitions by: Anton Lobashev +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import { Db } from 'mongodb'; + +export interface DbConfig { + hosts?: string; + db?: string; + user?: string; + password?: string; + mongoUri?: string; + migrationCollection: string; + replicaSet?: string; +} + +export interface MigrationResponse { + id: string; + status: 'not-run' | 'skipped' | 'pending' | 'ok' | 'error' | 'rollback' | 'rollback-error'; +} + +export class Migration { + constructor(dbConfig: DbConfig); + add: (paths: string | string[]) => void; + addAllFromPath: (path: string) => void; + migrate: (doneCb?: (err: Error | null, response: MigrationResponse[]) => void) => void; +} + +export interface MigrationStep { + id: string; + up: (db: Db, cb: (err?: Error) => void) => void; + down?: (db: Db, cb: (err?: Error) => void) => void; +} diff --git a/types/mongration/mongration-tests.ts b/types/mongration/mongration-tests.ts new file mode 100644 index 0000000000..735aa6436a --- /dev/null +++ b/types/mongration/mongration-tests.ts @@ -0,0 +1,28 @@ +import { Migration, DbConfig, MigrationStep } from 'mongration'; + +const dbConfig: DbConfig = { + hosts: 'localhost:27017', + db: 'testdb', + migrationCollection: 'migrationversions' +}; + +const migration = new Migration(dbConfig); +migration.add('1-add-foo.js'); +migration.add(['2-add-bar.js', '3-add-foobar.js']); +migration.addAllFromPath('migrations'); +migration.migrate((err, response) => { + if (!err) { + return response; + } +}); + +// from https://github.com/awapps/mongration#creating-migration-steps +const step: MigrationStep = { + id: '1-step', + up: (db, cb) => { + db.collection('testcolleciton').insert({ name: 'initial-setup' }, cb); + }, + down: (db, cb) => { + db.collection('testcollection').remove({ name: 'initial-setup' }, cb); + } +}; diff --git a/types/mongration/tsconfig.json b/types/mongration/tsconfig.json new file mode 100644 index 0000000000..c47e0d78b6 --- /dev/null +++ b/types/mongration/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mongration-tests.ts" + ] +} diff --git a/types/mongration/tslint.json b/types/mongration/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/mongration/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }