Add types for mongration (#29843)

* Add types for mongration

* Make MigrationResponse status type more explicit
This commit is contained in:
Anton Lobashev
2018-10-18 10:23:44 -07:00
committed by Sheetal Nandi
parent 5348b35343
commit 24a30d0f2e
4 changed files with 87 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
// Type definitions for mongration 1.0
// Project: https://github.com/awapps/mongration#readme
// Definitions by: Anton Lobashev <https://github.com/soulthreads>
// 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;
}
+28
View File
@@ -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);
}
};
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }