不要怂,就是干,撸起袖子干!

Commit e7c8f265 by Andre Cerqueira

support for relative paths on sequelize.import

1 parent a8ce9570
...@@ -215,6 +215,13 @@ module.exports = (function() { ...@@ -215,6 +215,13 @@ module.exports = (function() {
} }
Sequelize.prototype.import = function(path) { Sequelize.prototype.import = function(path) {
// is it a relative path?
if (url.parse(path).pathname.indexOf('/') !== 0) {
// make path relative to the caller
var callerFilename = Utils.stack()[1].getFileName();
path = url.resolve(callerFilename, path);
}
if (!this.importCache[path]) { if (!this.importCache[path]) {
var defineCall = require(path) var defineCall = require(path)
this.importCache[path] = defineCall(this, DataTypes) this.importCache[path] = defineCall(this, DataTypes)
......
...@@ -451,6 +451,18 @@ var Utils = module.exports = { ...@@ -451,6 +451,18 @@ var Utils = module.exports = {
return subClass; return subClass;
}, },
stack: function() {
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack){ return stack; };
var err = new Error();
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
},
now: function(dialect) { now: function(dialect) {
var now = new Date() var now = new Date()
if(dialect != "postgres") now.setMilliseconds(0) if(dialect != "postgres") now.setMilliseconds(0)
......
...@@ -375,11 +375,17 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -375,11 +375,17 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
}) })
describe('import', function() { describe('import', function() {
it("imports a dao definition from a file", function(done) { it("imports a dao definition from a file absolute path", function(done) {
var Project = this.sequelize.import(__dirname + "/assets/project") var Project = this.sequelize.import(__dirname + "/assets/project")
expect(Project).to.exist expect(Project).to.exist
done() done()
}) })
it("imports a dao definition from a file relative path", function(done) {
var Project = this.sequelize.import("assets/project")
expect(Project).to.exist
done()
})
}) })
describe('define', function() { describe('define', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!