-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a80ab8
commit 1de5db2
Showing
7 changed files
with
200 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.DS_Store | ||
node_modules |
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,4 @@ | ||
test: | ||
@./node_modules/mocha/bin/mocha --colors -t 10000 --reporter dot test/*.test.js | ||
|
||
.PHONY: test |
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,5 @@ | ||
function Sequenice(sequelize, options) { | ||
|
||
}; | ||
|
||
module.exports = Sequenice; |
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,34 @@ | ||
{ | ||
"name": "sequenice", | ||
"version": "0.0.1", | ||
"description": "A nice model wrapper for `sequelize`", | ||
"main": "index.js", | ||
"directories": { | ||
"example": "example" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/saschagehlich/sequenice.git" | ||
}, | ||
"keywords": [ | ||
"sequelize", | ||
"model", | ||
"mysql", | ||
"postgres", | ||
"database", | ||
"sql" | ||
], | ||
"author": "Sascha Gehlich <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/saschagehlich/sequenice/issues" | ||
}, | ||
"devDependencies": { | ||
"sequelize": "~2.0.0-beta.1", | ||
"should": "~2.0.2", | ||
"mocha": "~1.14.0" | ||
} | ||
} |
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,91 @@ | ||
var should = require("should") | ||
, Sequelize = require("sequelize") | ||
, Sequenice = require("..") | ||
, sequelize, sequenice | ||
, models = {}; | ||
|
||
before(function (done) { | ||
sequelize = new Sequelize("sequenice_test", "root", ""); | ||
sequenice = new Sequenice(sequelize, { | ||
modelDirectory: __dirname + "/models", | ||
modelAttacher: models, | ||
getterPrefix: "get", | ||
setterPrefix: "set" | ||
}); | ||
sequelize.sync({ force: true }).success(function () { | ||
done() | ||
}); | ||
}); | ||
|
||
describe("sequenice example", function () { | ||
/** | ||
* Field definitions | ||
*/ | ||
describe("field definitions", function () { | ||
it("creates `User` and `Project` models and attaches them to `models`", function () { | ||
should.exist(models.User); | ||
should.exist(models.Project); | ||
}); | ||
|
||
it("creates a `name` field", function (done) { | ||
models.User.create({ name: "A Name" }).success(function (user) { | ||
should.exist(user.name); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("creates a `isAdmin` field with a default value", function (done) { | ||
models.User.create({ name: "A Name" }).success(function (user) { | ||
user.isAdmin.should.equal(false); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* Associations | ||
*/ | ||
describe("associations definitions", function () { | ||
it("creates a hasMany relation from `User` to `Project`", function (done) { | ||
models.User.create({ name: "A Name" }).success(function (user) { | ||
models.Project.create({ name: "Project name" }).success(function (project) { | ||
user.setProjects([project]).success(function () { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it("creates a belongsTo relation from `Project` to `User`", function (done) { | ||
models.Project.create({ name: "Project name" }).success(function (project) { | ||
models.User.create({ name: "A Name" }).success(function (user) { | ||
project.setUser(user).success(function () { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* Hooks | ||
*/ | ||
describe("hooks", function () { | ||
it("defines a `beforeCreate` hook", function (done) { | ||
models.User.create().success(function (user) { | ||
user.getDataValue("beforeCreateCalled").should.be.true; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* Getters / setters | ||
*/ | ||
it("defines getters and setters", function (done) { | ||
models.User.build({ price: 20 }).priceInCents.should.equal(20 * 100); | ||
models.User.build({ priceInCents: 30 * 100 }).price.should.equal("$" + 30); | ||
|
||
done(); | ||
}); | ||
}); |
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,11 @@ | ||
function Project(s) { | ||
/** | ||
* Field declarations | ||
*/ | ||
this.field("name", s.STRING); | ||
|
||
/** | ||
* Associations | ||
*/ | ||
this.belongsTo("User"); | ||
} |
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,54 @@ | ||
function User(s) { | ||
/** | ||
* Field declarations | ||
*/ | ||
this.field("name", s.STRING); | ||
this.field("isAdmin", s.BOOLEAN, { defaultValue: false }); | ||
|
||
/** | ||
* Associations | ||
*/ | ||
this.hasMany("Project"); | ||
|
||
/** | ||
* Getters | ||
*/ | ||
this.get("price"); | ||
this.get("priceInCents"); | ||
|
||
/** | ||
* Setters | ||
*/ | ||
this.set("price"); | ||
|
||
/** | ||
* Hooks | ||
*/ | ||
this.beforeCreate("myBeforeCreateMethod"); | ||
|
||
/** | ||
* Validations | ||
*/ | ||
this.validates("myValidationMethod") | ||
} | ||
|
||
User.prototype.getPrice = function() { | ||
return "$" + (this.getDataValue('priceInCents') / 100); | ||
}; | ||
|
||
User.prototype.getPriceInCents = function() { | ||
return this.dataValues.priceInCents; | ||
}; | ||
|
||
User.prototype.setPrice = function(value) { | ||
this.dataValues.priceInCents = value * 100; | ||
}; | ||
|
||
User.prototype.myBeforeCreateMethod = function(callback) { | ||
this.dataValues.beforeCreateCalled = true; | ||
callback(); | ||
}; | ||
|
||
User.prototype.myValidationMethod = function (callback) { | ||
callback(); | ||
} |