From 0e85f5975c032b9b646109435f9bcdb61432461c Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 4 Dec 2014 13:35:08 -0800 Subject: [PATCH] Map required/id properties to NOT NULL See https://github.com/strongloop/strong-arc/issues/600 --- lib/mssql.js | 7 ++++++- test/autoupdate.test.js | 9 +++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/mssql.js b/lib/mssql.js index 11bb49a..04e0139 100644 --- a/lib/mssql.js +++ b/lib/mssql.js @@ -1183,10 +1183,15 @@ MsSQL.prototype.indexSettingsSQL = function (model, prop) { return cmd; }; +function isNullable(p) { + return !(p.required || p.id || p.nullable === false || + p.allowNull === false || p['null'] === false); +} + MsSQL.prototype.propertySettingsSQL = function (model, prop) { var p = this._models[model].properties[prop]; return this.columnDataType(model, prop) + ' ' + - (p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL'); + (isNullable(p) ? 'NULL' : 'NOT NULL'); }; MsSQL.prototype.automigrate = function(models, cb) { diff --git a/test/autoupdate.test.js b/test/autoupdate.test.js index 44d9980..f327b7d 100644 --- a/test/autoupdate.test.js +++ b/test/autoupdate.test.js @@ -32,7 +32,7 @@ describe('MS SQL server connector', function () { }, "email": { "type": "String", - "required": false, + "required": true, "length": 40 }, "age": { @@ -66,7 +66,7 @@ describe('MS SQL server connector', function () { "columnName": "EMAIL", "dataType": "nvarchar", "dataLength": 60, - "nullable": "Y" + "nullable": "YES" } }, "firstName": { @@ -92,6 +92,11 @@ describe('MS SQL server connector', function () { var names = props.map(function (p) { return p.columnName; }); + assert.equal(props[0].nullable, 'NO'); + assert.equal(props[1].nullable, 'YES'); + assert.equal(props[2].nullable, 'NO'); + assert.equal(props[3].nullable, 'YES'); + assert.equal(names[0], 'id'); assert.equal(names[1], 'name'); assert.equal(names[2], 'email');