diff --git a/package.json b/package.json index 20f91cc..1dc6afe 100644 --- a/package.json +++ b/package.json @@ -26,17 +26,18 @@ "url": "https://github.com/balmasi/migrate-mongoose.git" }, "dependencies": { - "babel-cli": "^6.18.0", - "babel-core": "^6.20.0", - "babel-plugin-transform-runtime": "^6.15.0", - "babel-polyfill": "^6.20.0", - "babel-preset-latest": "^6.16.0", - "babel-register": "^6.18.0", - "bluebird": "^3.3.3", + "babel-cli": "^6.24.1", + "babel-core": "^6.24.1", + "babel-plugin-transform-runtime": "^6.23.0", + "babel-polyfill": "^6.23.0", + "babel-preset-latest": "^6.24.1", + "babel-register": "^6.24.1", + "bluebird": "^3.5.0", "colors": "^1.1.2", "inquirer": "^0.12.0", "mkdirp": "^0.5.1", - "mongoose": "^4.4.6", + "mongoose": "^4.10.2", + "ts-node": "^3.0.4", "yargs": "^4.8.1" } } diff --git a/src/cli.js b/src/cli.js index 5c1ffb3..74ff8ff 100755 --- a/src/cli.js +++ b/src/cli.js @@ -65,6 +65,10 @@ let { argv: args } = yargs type: 'boolean', description: 'use es6 migration template?' }) + .option('typescript', { + type: 'boolean', + description: 'use typescript migration template?' + }) .option('md', { alias: 'migrations-dir', description: 'The path to the migration files', @@ -115,6 +119,7 @@ let migrator = new Migrator({ templatePath: args['template-file'], dbConnectionUri: args.dbConnectionUri, es6Templates: args.es6, + typescript: args.typescript, collectionName: args.collection, autosync: args.autosync, cli: true diff --git a/src/db.js b/src/db.js index 7fc1829..be7ca99 100644 --- a/src/db.js +++ b/src/db.js @@ -3,7 +3,7 @@ import Promise from 'bluebird'; // Factory function for a mongoose model mongoose.Promise = Promise; -export default function ( collection = 'migrations', dbConnection ) { +export default function ( collection = 'migrations', dbConnection, {typescript} ) { const MigrationSchema = new Schema({ name: String, @@ -27,7 +27,9 @@ export default function ( collection = 'migrations', dbConnection ) { }); MigrationSchema.virtual('filename').get(function() { - return `${this.createdAt.getTime()}-${this.name}.js`; + let basename = `${this.createdAt.getTime()}-${this.name}`; + if(typescript) return `${basename}.ts`; + return `${basename}.js`; }); dbConnection.on('error', err => { diff --git a/src/lib.js b/src/lib.js index 5ad3489..0586321 100644 --- a/src/lib.js +++ b/src/lib.js @@ -56,20 +56,22 @@ export default class Migrator { migrationsPath = './migrations', dbConnectionUri, es6Templates = false, + typescript = false, collectionName = 'migrations', autosync = false, cli = false, connection }) { - const defaultTemplate = es6Templates ? es6Template : es5Template; + const defaultTemplate = typescript || es6Templates ? es6Template : es5Template; this.template = templatePath ? fs.readFileSync(templatePath, 'utf-8') : defaultTemplate; this.migrationPath = path.resolve(migrationsPath); this.connection = connection || mongoose.createConnection(dbConnectionUri); this.es6 = es6Templates; + this.typescript = typescript; this.collection = collectionName; this.autosync = autosync; this.cli = cli; - MigrationModel = MigrationModelFactory(collectionName, this.connection); + MigrationModel = MigrationModelFactory(collectionName, this.connection, {typescript}); } log (logString, force = false) { @@ -94,6 +96,17 @@ export default class Migrator { return this.connection ? this.connection.close() : Promise.resolve(); } + /** + * Generate name for migration file with right extension + * @param basename + * @return {string} + */ + getMigrationFileName(basename) { + if (this.typescript) return `${basename}.ts`; + return `${basename}.js`; + } + + /** * Create a new migration * @param {string} migrationName @@ -108,7 +121,7 @@ export default class Migrator { await this.sync(); const now = Date.now(); - const newMigrationFile = `${now}-${migrationName}.js`; + const newMigrationFile = this.getMigrationFileName(`${now}-${migrationName}`); mkdirp.sync(this.migrationPath); fs.writeFileSync(path.join(this.migrationPath, newMigrationFile), this.template); // create instance in db @@ -184,6 +197,12 @@ export default class Migrator { require('babel-polyfill'); } + if (this.typescript) { + require("ts-node").register({ + disableWarnings: true + }); + } + let migrationFunctions; try { @@ -241,7 +260,7 @@ export default class Migrator { const filesInMigrationFolder = fs.readdirSync(this.migrationPath); const migrationsInDatabase = await MigrationModel.find({}); // Go over migrations in folder and delete any files not in DB - const migrationsInFolder = _.filter(filesInMigrationFolder, file => /\d{13,}\-.+.js$/.test(file)) + const migrationsInFolder = _.filter(filesInMigrationFolder, file => /\d{13,}\-.+.(js|ts)$/.test(file)) .map(filename => { const fileCreatedAt = parseInt(filename.split('-')[0]); const existsInDatabase = migrationsInDatabase.some(m => filename == m.filename); @@ -294,7 +313,7 @@ export default class Migrator { const filesInMigrationFolder = fs.readdirSync(this.migrationPath); const migrationsInDatabase = await MigrationModel.find({}); // Go over migrations in folder and delete any files not in DB - const migrationsInFolder = _.filter(filesInMigrationFolder, file => /\d{13,}\-.+.js/.test(file) ) + const migrationsInFolder = _.filter(filesInMigrationFolder, file => /\d{13,}\-.+.(js|ts)/.test(file) ) .map(filename => { const fileCreatedAt = parseInt(filename.split('-')[0]); const existsInDatabase = migrationsInDatabase.some(m => filename == m.filename); @@ -375,4 +394,3 @@ function fileRequired(error) { module.exports = Migrator; -