|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const File = require('./file.js'); |
| 4 | +const dbmUtil = require('db-migrate-shared').util; |
| 5 | + |
| 6 | +const Template = function () { |
| 7 | + this.templateType = arguments[3]; |
| 8 | + // this is a tweak for node 4 this can be translated |
| 9 | + // to ...arguments instead in upcoming node versions |
| 10 | + const Comp = File.bind.apply(File, [null].concat(dbmUtil.toArray(arguments))); |
| 11 | + this.file = new Comp(); |
| 12 | +}; |
| 13 | + |
| 14 | +Template.prototype.defaultCoffeeTemplate = function () { |
| 15 | + return [ |
| 16 | + "'use strict';", |
| 17 | + '', |
| 18 | + 'dbm = undefined', |
| 19 | + 'type = undefined', |
| 20 | + 'seed = undefined', |
| 21 | + '', |
| 22 | + 'exports.setup = (options, seedLink) ->', |
| 23 | + ' dbm = options.dbmigrate', |
| 24 | + ' type = dbm.dataType', |
| 25 | + ' seed = seedLink', |
| 26 | + '', |
| 27 | + 'exports.up = (db) ->', |
| 28 | + ' null', |
| 29 | + '', |
| 30 | + 'exports.down = (db) ->', |
| 31 | + ' null', |
| 32 | + '', |
| 33 | + '' |
| 34 | + ].join('\n'); |
| 35 | +}; |
| 36 | + |
| 37 | +Template.prototype.defaultJsTemplate = function () { |
| 38 | + return [ |
| 39 | + "'use strict';", |
| 40 | + '', |
| 41 | + 'var dbm;', |
| 42 | + 'var type;', |
| 43 | + 'var seed;', |
| 44 | + '', |
| 45 | + '/**', |
| 46 | + ' * We receive the dbmigrate dependency from dbmigrate initially.', |
| 47 | + ' * This enables us to not have to rely on NODE_PATH.', |
| 48 | + ' */', |
| 49 | + 'exports.setup = function(options, seedLink) {', |
| 50 | + ' dbm = options.dbmigrate;', |
| 51 | + ' type = dbm.dataType;', |
| 52 | + ' seed = seedLink;', |
| 53 | + '};', |
| 54 | + '', |
| 55 | + 'exports.up = function(db) {', |
| 56 | + ' return null;', |
| 57 | + '};', |
| 58 | + '', |
| 59 | + 'exports.down = function(db) {', |
| 60 | + ' return null;', |
| 61 | + '};', |
| 62 | + '', |
| 63 | + 'exports._meta = {', |
| 64 | + ' "version": 1', |
| 65 | + '};', |
| 66 | + '' |
| 67 | + ].join('\n'); |
| 68 | +}; |
| 69 | + |
| 70 | +Template.prototype.defaultSqlTemplate = function () { |
| 71 | + return '/* Replace with your SQL commands */'; |
| 72 | +}; |
| 73 | + |
| 74 | +Template.prototype.sqlFileLoaderTemplate = function () { |
| 75 | + return [ |
| 76 | + "'use strict';", |
| 77 | + '', |
| 78 | + 'var dbm;', |
| 79 | + 'var type;', |
| 80 | + 'var seed;', |
| 81 | + "var fs = require('fs');", |
| 82 | + "var path = require('path');", |
| 83 | + 'var Promise;', |
| 84 | + '', |
| 85 | + '/**', |
| 86 | + ' * We receive the dbmigrate dependency from dbmigrate initially.', |
| 87 | + ' * This enables us to not have to rely on NODE_PATH.', |
| 88 | + ' */', |
| 89 | + 'exports.setup = function(options, seedLink) {', |
| 90 | + ' dbm = options.dbmigrate;', |
| 91 | + ' type = dbm.dataType;', |
| 92 | + ' seed = seedLink;', |
| 93 | + ' Promise = options.Promise;', |
| 94 | + '};', |
| 95 | + '', |
| 96 | + 'exports.up = function(db) {', |
| 97 | + " var filePath = path.join(__dirname, 'sqls', '" + |
| 98 | + this.file.name.replace('.js', '') + |
| 99 | + "-up.sql');", |
| 100 | + ' return new Promise( function( resolve, reject ) {', |
| 101 | + " fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){", |
| 102 | + ' if (err) return reject(err);', |
| 103 | + " console.log('received data: ' + data);", |
| 104 | + '', |
| 105 | + ' resolve(data);', |
| 106 | + ' });', |
| 107 | + ' })', |
| 108 | + ' .then(function(data) {', |
| 109 | + ' return db.runSql(data);', |
| 110 | + ' });', |
| 111 | + '};', |
| 112 | + '', |
| 113 | + 'exports.down = function(db) {', |
| 114 | + " var filePath = path.join(__dirname, 'sqls', '" + |
| 115 | + this.file.name.replace('.js', '') + |
| 116 | + "-down.sql');", |
| 117 | + ' return new Promise( function( resolve, reject ) {', |
| 118 | + " fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){", |
| 119 | + ' if (err) return reject(err);', |
| 120 | + " console.log('received data: ' + data);", |
| 121 | + '', |
| 122 | + ' resolve(data);', |
| 123 | + ' });', |
| 124 | + ' })', |
| 125 | + ' .then(function(data) {', |
| 126 | + ' return db.runSql(data);', |
| 127 | + ' });', |
| 128 | + '};', |
| 129 | + '', |
| 130 | + 'exports._meta = {', |
| 131 | + ' "version": 1', |
| 132 | + '};', |
| 133 | + '' |
| 134 | + ].join('\n'); |
| 135 | +}; |
| 136 | + |
| 137 | +Template.prototype.sqlFileLoaderIgnoreOnInitTemplate = function () { |
| 138 | + return [ |
| 139 | + "'use strict';", |
| 140 | + '', |
| 141 | + 'var dbm;', |
| 142 | + 'var type;', |
| 143 | + 'var seed;', |
| 144 | + "var fs = require('fs');", |
| 145 | + "var path = require('path');", |
| 146 | + 'var ignoreOnInit = false;', |
| 147 | + 'var Promise;', |
| 148 | + '', |
| 149 | + '/**', |
| 150 | + ' * We receive the dbmigrate dependency from dbmigrate initially.', |
| 151 | + ' * This enables us to not have to rely on NODE_PATH.', |
| 152 | + ' */', |
| 153 | + 'exports.setup = function(options, seedLink) {', |
| 154 | + ' dbm = options.dbmigrate;', |
| 155 | + ' ignoreOnInit = options.ignoreOnInit;', |
| 156 | + ' type = dbm.dataType;', |
| 157 | + ' seed = seedLink;', |
| 158 | + ' Promise = options.Promise;', |
| 159 | + '};', |
| 160 | + '', |
| 161 | + 'exports.up = function(db, callback) {', |
| 162 | + " var filePath = path.join(__dirname + '/sqls/" + |
| 163 | + this.file.name.replace('.js', '') + |
| 164 | + "-up.sql');", |
| 165 | + ' if (!ignoreOnInit) {', |
| 166 | + ' return new Promise( function( resolve, reject ) {', |
| 167 | + " fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){", |
| 168 | + ' if (err) return reject(err);', |
| 169 | + " console.log('received data: ' + data);", |
| 170 | + '', |
| 171 | + ' resolve(data);', |
| 172 | + ' });', |
| 173 | + ' })', |
| 174 | + ' .then(function(data) {', |
| 175 | + ' return db.runSql(data);', |
| 176 | + ' });', |
| 177 | + ' }', |
| 178 | + ' else {', |
| 179 | + " console.log('ignoring on init: ' + filePath)", |
| 180 | + ' return null;', |
| 181 | + ' }', |
| 182 | + '};', |
| 183 | + '', |
| 184 | + 'exports.down = function(db, callback) {', |
| 185 | + " var filePath = path.join(__dirname + '/sqls/" + |
| 186 | + this.file.name.replace('.js', '') + |
| 187 | + "-down.sql');", |
| 188 | + ' return new Promise( function( resolve, reject ) {', |
| 189 | + " fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){", |
| 190 | + ' if (err) return reject(err);', |
| 191 | + " console.log('received data: ' + data);", |
| 192 | + '', |
| 193 | + ' resolve(data);', |
| 194 | + ' });', |
| 195 | + ' })', |
| 196 | + ' .then(function(data) {', |
| 197 | + ' return db.runSql(data);', |
| 198 | + ' });', |
| 199 | + '};', |
| 200 | + '', |
| 201 | + 'exports._meta = {', |
| 202 | + ' "version": 1', |
| 203 | + '};', |
| 204 | + '' |
| 205 | + ].join('\n'); |
| 206 | +}; |
| 207 | + |
| 208 | +Template.prototype.coffeeSqlFileLoaderTemplate = function () { |
| 209 | + return [ |
| 210 | + "'use strict';", |
| 211 | + '', |
| 212 | + 'dbm = undefined', |
| 213 | + 'type = undefined', |
| 214 | + 'seed = undefined', |
| 215 | + "fs = require 'fs'", |
| 216 | + "path = require 'path'", |
| 217 | + '', |
| 218 | + '#', |
| 219 | + '# We receive the dbmigrate dependency from dbmigrate initially.', |
| 220 | + '# This enables us to not have to rely on NODE_PATH.', |
| 221 | + '#', |
| 222 | + 'exports.setup = (options, seedLink) ->', |
| 223 | + ' dbm = options.dbmigrate', |
| 224 | + ' type = dbm.dataType', |
| 225 | + ' seed = seedLink', |
| 226 | + '', |
| 227 | + '', |
| 228 | + 'exports.up = (db, callback) ->', |
| 229 | + ' filePath = path.join "#{__dirname}/sqls/' + |
| 230 | + this.file.name.replace('.coffee', '') + |
| 231 | + '-up.sql"', |
| 232 | + " fs.readFile filePath, {encoding: 'utf-8'}, (err,data) ->", |
| 233 | + ' return callback err if err', |
| 234 | + '', |
| 235 | + ' db.runSql data, callback', |
| 236 | + '', |
| 237 | + 'exports.down = (db, callback) ->', |
| 238 | + ' filePath = path.join "#{__dirname}/sqls/' + |
| 239 | + this.file.name.replace('.coffee', '') + |
| 240 | + '-down.sql"', |
| 241 | + |
| 242 | + " fs.readFile filePath, {encoding: 'utf-8'}, (err,data) ->", |
| 243 | + ' return callback err if err', |
| 244 | + '', |
| 245 | + ' db.runSql data, callback', |
| 246 | + '' |
| 247 | + ].join('\n'); |
| 248 | +}; |
| 249 | + |
| 250 | +Template.TemplateType = { |
| 251 | + DEFAULT_JS: 0, |
| 252 | + DEFAULT_SQL: 1, |
| 253 | + SQL_FILE_LOADER: 2, |
| 254 | + DEFAULT_COFFEE: 3, |
| 255 | + COFFEE_SQL_FILE_LOADER: 4, |
| 256 | + SQL_FILE_LOADER_IGNORE_ON_INIT: 5 |
| 257 | +}; |
| 258 | + |
| 259 | +Template.prototype.getTemplate = function () { |
| 260 | + switch (this.templateType) { |
| 261 | + case Template.TemplateType.DEFAULT_SQL: |
| 262 | + return this.defaultSqlTemplate(); |
| 263 | + case Template.TemplateType.SQL_FILE_LOADER: |
| 264 | + return this.sqlFileLoaderTemplate(); |
| 265 | + case Template.TemplateType.DEFAULT_COFFEE: |
| 266 | + return this.defaultCoffeeTemplate(); |
| 267 | + case Template.TemplateType.COFFEE_SQL_FILE_LOADER: |
| 268 | + return this.coffeeSqlFileLoaderTemplate(); |
| 269 | + case Template.TemplateType.SQL_FILE_LOADER_IGNORE_ON_INIT: |
| 270 | + return this.sqlFileLoaderIgnoreOnInitTemplate(); |
| 271 | + case Template.TemplateType.DEFAULT_JS: |
| 272 | + /* falls through */ |
| 273 | + default: |
| 274 | + return this.defaultJsTemplate(); |
| 275 | + } |
| 276 | +}; |
| 277 | + |
| 278 | +Template.prototype.write = function (callback) { |
| 279 | + return this.file.write(this.getTemplate()).nodeify(callback); |
| 280 | +}; |
| 281 | + |
| 282 | +module.exports = Template; |
0 commit comments