emirotin / mongodb-migrations

A Node.js migration framework for MongoDB
MIT License
258 stars 70 forks source link

It would be really cool if we could write migrations in typescript #41

Open vadim82 opened 7 years ago

vadim82 commented 7 years ago

A nice advantage of this is, we would then be able to get the @typings for mongodb and get intellisense for the entire mongo api.

emirotin commented 7 years ago

I like TS myself but there are dozens of compile-to-JS languages and it would be impractical to have special handling for each of them. CoffeeScript is an exception historically but I consider dropping its support in v1 or v2.

Instead I'm interested in discussing the possibilities of writing your migrations in any language and making the module recognize them. Right now I see three options:

a) programmatic usage of the module with a bit of extra config. If TS has a register mode similar to require('coffee-script/register') you would be able to configure the extensions to recognize and Node's require will just be able to pick the modules.

b) add a simple wrapper script that builds the migrations in place (.ts -> .js) and then use the binary as is

c) support --compiler option as mocha does.

igabesz commented 7 years ago

There's another option, which is the absolute minimum:

Now I use this:

import * as mongoose from 'mongoose';

interface Migrator {
    db: mongoose.Connection;
    log: Function;
}

interface DoneCallback {
    (): void;
    (error: any): void;
}

export const id = 'ProposalPrice';

export function up (this: Migrator, done: DoneCallback) {
    // use this.db for MongoDB communication, and this.log() for logging
};

export function down (this: Migrator, done: DoneCallback) {
    // use this.db for MongoDB communication, and this.log() for logging
    done();
};

Of course the types can be generated to migrations/types.d.ts, then the migration files should only contain import { Migrator, DoneCallback } from './types'.

igabesz commented 7 years ago

Right now I have the following project structure:

And I have the following npm scripts:

Just an example for using typed migration scripts + my own build system.