Skip to content

Commit

Permalink
Adds default settings properties.not_null = false (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
dresende committed Apr 10, 2013
1 parent c012c99 commit 84645c8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/Associations/Many.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports.prepare = function (Model, associations) {
props = {};
} else {
for (var k in props) {
props[k] = Property.check(props[k]);
props[k] = Property.check(props[k], Model.settings);
}
}

Expand Down
5 changes: 4 additions & 1 deletion lib/Drivers/DDL/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function buildColumnDefinition(driver, name, prop) {
}
break;
case "boolean":
def = driver.query.escapeId(name) + " BOOLEAN NOT NULL";
def = driver.query.escapeId(name) + " BOOLEAN";
break;
case "date":
if (prop.time === false) {
Expand All @@ -155,6 +155,9 @@ function buildColumnDefinition(driver, name, prop) {
default:
throw new Error("Unknown property type: '" + prop.type + "'");
}
if (prop.notnull === true) {
def += " NOT NULL";
}
if (prop.hasOwnProperty("defaultValue")) {
def += " DEFAULT " + driver.query.escapeVal(prop.defaultValue);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Drivers/DDL/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function buildColumnDefinition(driver, table, name, prop) {
}
break;
case "boolean":
def = driver.query.escapeId(name) + " BOOLEAN NOT NULL";
def = driver.query.escapeId(name) + " BOOLEAN";
break;
case "date":
if (prop.time === false) {
Expand All @@ -192,6 +192,9 @@ function buildColumnDefinition(driver, table, name, prop) {
default:
throw new Error("Unknown property type: '" + prop.type + "'");
}
if (prop.notnull === true) {
def += " NOT NULL";
}
if (prop.hasOwnProperty("defaultValue")) {
def += " DEFAULT " + driver.query.escapeVal(prop.defaultValue);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Drivers/DDL/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function buildColumnDefinition(driver, name, prop) {
}
break;
case "boolean":
def = driver.query.escapeId(name) + " INTEGER UNSIGNED NOT NULL";
def = driver.query.escapeId(name) + " INTEGER UNSIGNED";
break;
case "date":
def = driver.query.escapeId(name) + " DATETIME";
Expand All @@ -140,6 +140,9 @@ function buildColumnDefinition(driver, name, prop) {
default:
throw new Error("Unknown property type: '" + prop.type + "'");
}
if (prop.notnull === true) {
def += " NOT NULL";
}
if (prop.hasOwnProperty("defaultValue")) {
def += " DEFAULT " + driver.query.escapeVal(prop.defaultValue);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ORM.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ ORM.prototype.define = function (name, properties, opts) {
opts = opts || {};

for (var k in properties) {
properties[k] = Property.check(properties[k]);
properties[k] = Property.check(properties[k], this.settings);
}

this.models[name] = new Model({
Expand Down
6 changes: 5 additions & 1 deletion lib/Property.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.check = function (prop) {
exports.check = function (prop, Settings) {
if (typeof prop == "function") {
switch (prop.name) {
case "String":
Expand Down Expand Up @@ -32,6 +32,10 @@ exports.check = function (prop) {
throw new Error("Unknown property type: " + prop.type);
}

if (!prop.hasOwnProperty("notnull") && Settings.get("properties.not_null")) {
prop.notnull = true;
}

return prop;
};

Expand Down
3 changes: 2 additions & 1 deletion lib/Settings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var default_settings = {
properties : {
primary_key : "id",
association_key : "{name}_id"
association_key : "{name}_id",
not_null : false
},
instance : {
cache : true,
Expand Down
21 changes: 11 additions & 10 deletions test/integration/test-property-types.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
var common = require('../common');
var assert = require('assert');
var Property = require('../../lib/Property');
var Settings = common.ORM.settings;

assert.equal(Property.check(String).type, "text");
assert.equal(Property.check(Number).type, "number");
assert.equal(Property.check(Boolean).type, "boolean");
assert.equal(Property.check(Date).type, "date");
assert.equal(Property.check(Object).type, "object");
assert.equal(Property.check(Buffer).type, "binary");
assert.equal(Property.check([ 'a', 'b' ]).type, "enum");
assert.deepEqual(Property.check([ 'a', 'b' ]).values, [ 'a', 'b' ]);
assert.equal(Property.check(String, Settings).type, "text");
assert.equal(Property.check(Number, Settings).type, "number");
assert.equal(Property.check(Boolean, Settings).type, "boolean");
assert.equal(Property.check(Date, Settings).type, "date");
assert.equal(Property.check(Object, Settings).type, "object");
assert.equal(Property.check(Buffer, Settings).type, "binary");
assert.equal(Property.check([ 'a', 'b' ], Settings).type, "enum");
assert.deepEqual(Property.check([ 'a', 'b' ], Settings).values, [ 'a', 'b' ]);

assert.equal({ type: "text" }.type, "text");
assert.equal({ type: "number" }.type, "number");
Expand All @@ -19,5 +20,5 @@ assert.equal({ type: "enum" }.type, "enum");
assert.equal({ type: "object" }.type, "object");
assert.equal({ type: "binary" }.type, "binary");

assert.throws(function () { Property.check({ type: "buffer" }); });
assert.throws(function () { Property.check({ type: "unknown" }); });
assert.throws(function () { Property.check({ type: "buffer" }, Settings); });
assert.throws(function () { Property.check({ type: "unknown" }, Settings); });

0 comments on commit 84645c8

Please sign in to comment.