Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added minimal support for typescript #24

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
5 changes: 5 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 => {
Expand Down
30 changes: 24 additions & 6 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -184,6 +197,12 @@ export default class Migrator {
require('babel-polyfill');
}

if (this.typescript) {
require("ts-node").register({
disableWarnings: true
});
}

let migrationFunctions;

try {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -375,4 +394,3 @@ function fileRequired(error) {


module.exports = Migrator;