-
-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from sequelize/feature/models
Model loader
- Loading branch information
Showing
13 changed files
with
304 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var fs = require('fs') | ||
, path = require('path') | ||
, Sequelize = require('sequelize') | ||
, lodash = require('lodash') | ||
, env = process.env.NODE_ENV || 'development' | ||
, config = require(__CONFIG_FILE__)[env] | ||
, sequelize = new Sequelize(config.database, config.username, config.password, config) | ||
, db = {} | ||
|
||
fs | ||
.readdirSync(__dirname) | ||
.filter(function(file) { | ||
return (file.indexOf('.') !== 0) && (file !== 'index.js') | ||
}) | ||
.forEach(function(file) { | ||
var model = sequelize.import(path.join(__dirname, file)) | ||
db[model.name] = model | ||
}) | ||
|
||
Object.keys(db).forEach(function(modelName) { | ||
if ('associate' in db[modelName]) { | ||
db[modelName].associate(db) | ||
} | ||
}) | ||
|
||
module.exports = lodash.extend({ | ||
sequelize: sequelize, | ||
Sequelize: Sequelize | ||
}, db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var fs = require('fs-extra') | ||
, path = require('path') | ||
|
||
module.exports = { | ||
copy: function (from, to) { | ||
fs.copySync(path.resolve(__dirname, '..', 'assets', from), to) | ||
}, | ||
|
||
injectConfigFilePath: function(filePath, configPath) { | ||
var fileContent = fs.readFileSync(filePath).toString() | ||
fs.writeFileSync(filePath, fileContent.replace('__CONFIG_FILE__', configPath) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var helpers = require(__dirname) | ||
, args = require('yargs').argv | ||
, path = require('path') | ||
, fs = require('fs') | ||
, clc = require('cli-color') | ||
|
||
module.exports = { | ||
notifyAboutExistingFile: function(file) { | ||
helpers.view.log('The file ' + clc.blueBright(file) + ' already exists. Run "sequelize init --force" to overwrite it.') | ||
}, | ||
|
||
createFolder: function(folderName, folder, force) { | ||
if (force) { | ||
console.log('Deleting the ' + folderName + 'folder. (--force)') | ||
|
||
try { | ||
fs.readdirSync(folder).forEach(function(filename) { | ||
fs.unlinkSync(folder + '/' + filename) | ||
}) | ||
} catch(e) { | ||
console.log(e) | ||
} | ||
|
||
try { | ||
fs.rmdirSync(folder) | ||
console.log('Successfully deleted the ' + folderName + ' folder.') | ||
} catch(e) { | ||
console.log(e) | ||
} | ||
} | ||
|
||
try { | ||
helpers.generic.mkdirp(folder) | ||
console.log('Successfully created ' + folderName + ' folder at "' + folder + '".') | ||
} catch(e) { | ||
console.log(e) | ||
} | ||
}, | ||
|
||
createMigrationsFolder: function(force) { | ||
this.createFolder('migrations', helpers.path.getMigrationsPath(), force) | ||
}, | ||
|
||
createModelsFolder: function(force) { | ||
this.createFolder('models', helpers.path.getModelsPath(), force) | ||
}, | ||
|
||
createModelsIndexFile: function(force) { | ||
var modelsPath = helpers.path.getModelsPath() | ||
, indexPath = path.resolve(modelsPath, 'index.js') | ||
|
||
if (!fs.existsSync(modelsPath)) { | ||
helpers.view.log('Models folder not available.') | ||
} else if (fs.existsSync(indexPath) && !force) { | ||
this.notifyAboutExistingFile(indexPath) | ||
} else { | ||
var relativeConfigPath = path.relative( | ||
helpers.path.getModelsPath(), | ||
helpers.config.getConfigFile() | ||
) | ||
|
||
helpers.asset.copy('models/index.js', indexPath) | ||
helpers.asset.injectConfigFilePath(indexPath, "__dirname + '/" + relativeConfigPath + "'") | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var helpers = require(__dirname) | ||
, args = require('yargs').argv | ||
, path = require('path') | ||
, fs = require('fs') | ||
|
||
module.exports = { | ||
getMigrationsPath: function() { | ||
return args.migrationsPath || path.resolve(process.cwd(), 'migrations') | ||
}, | ||
|
||
getModelsPath: function() { | ||
return args.modelsPath || path.resolve(process.cwd(), 'models') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.