diff --git a/.gitignore b/.gitignore index e43b0f9..9daa824 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +node_modules diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..114c21a --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +test: + @./node_modules/mocha/bin/mocha --colors -t 10000 --reporter dot test/*.test.js + +.PHONY: test diff --git a/index.js b/index.js new file mode 100644 index 0000000..c57bde1 --- /dev/null +++ b/index.js @@ -0,0 +1,5 @@ +function Sequenice(sequelize, options) { + +}; + +module.exports = Sequenice; diff --git a/package.json b/package.json new file mode 100644 index 0000000..1aae2c2 --- /dev/null +++ b/package.json @@ -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 ", + "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" + } +} diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..0c0dad6 --- /dev/null +++ b/test/index.test.js @@ -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(); + }); +}); diff --git a/test/models/project.js b/test/models/project.js new file mode 100644 index 0000000..1e781c6 --- /dev/null +++ b/test/models/project.js @@ -0,0 +1,11 @@ +function Project(s) { + /** + * Field declarations + */ + this.field("name", s.STRING); + + /** + * Associations + */ + this.belongsTo("User"); +} diff --git a/test/models/user.js b/test/models/user.js new file mode 100644 index 0000000..8b5d6a9 --- /dev/null +++ b/test/models/user.js @@ -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(); +}