From c435cda4fba0972bb9d2e16090851e8b6a5b76f2 Mon Sep 17 00:00:00 2001 From: Andrey Loukhnov Date: Wed, 21 Jan 2015 10:09:33 +0300 Subject: [PATCH 1/3] provide database column default values via Loopback model description --- lib/postgresql.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/postgresql.js b/lib/postgresql.js index 5da604d4..8e94a976 100644 --- a/lib/postgresql.js +++ b/lib/postgresql.js @@ -1273,6 +1273,8 @@ PostgreSQL.prototype.propertySettingsSQL = function (model, propName) { } var result = self.columnDataType(model, propName); if (!propertyCanBeNull.call(self, model, propName)) result = result + ' NOT NULL'; + + result += self.columnDbDefault(model, propName); return result; }; @@ -1384,6 +1386,20 @@ function escape(val) { return "'" + val + "'"; } +/*! + * Get the database-default value for column from given model property + * + * @param {String} model The model name + * @param {String} property The property name + * @returns {String} The column default value + */ +PostgreSQL.prototype.columnDbDefault = function(model, property) { + var columnMetadata = this.columnMetadata(model, property); + var colDefault = columnMetadata && columnMetadata.dbDefault; + + return colDefault ? (' DEFAULT ' + columnMetadata.dbDefault): ''; +} + /*! * Find the column type for a given model property * From 9534b1301e7224f3b3aa0832be28e59da6acb47c Mon Sep 17 00:00:00 2001 From: Andrey Loukhnov Date: Thu, 22 Jan 2015 00:41:34 +0300 Subject: [PATCH 2/3] basic tests for PR #54 --- test/postgresql.dbdefaults.test.js | 83 ++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 test/postgresql.dbdefaults.test.js diff --git a/test/postgresql.dbdefaults.test.js b/test/postgresql.dbdefaults.test.js new file mode 100644 index 00000000..59af4c98 --- /dev/null +++ b/test/postgresql.dbdefaults.test.js @@ -0,0 +1,83 @@ +var should = require('should'), + assert = require('assert'); +var InvalidDefault, Post, db; + +describe('database default field values', function () { + before(function () { + db = getDataSource(); + + Post = db.define('PostWithDbDefaultValue', { + created: { + type: 'Date', + postgresql: { + dbDefault: "now()" + } + }, + defaultInt: { + type: 'Number', + postgresql: { + dbDefault: "5" + } + }, + oneMore: { + type: 'Number' + } + }); + + InvalidDefault = db.define('PostWithInvalidDbDefaultValue', { + created: { + type: 'Date', + postgresql: { + dbDefault: "'5'" + } + } + }); + }); + + it('should run migration', function (done) { + db.automigrate('PostWithDbDefaultValue', function () { + done(); + }); + }); + + it('should report inconsistent default values used', function (done) { + db.automigrate('PostWithInvalidDbDefaultValue', function (err) { + should.exists(err); + done(); + }); + }); + + it('should have \'now()\' default value in SQL column definition', function (done) { + var query = "select column_name, data_type, character_maximum_length, column_default " + + "from information_schema.columns where table_name = 'postwithdbdefaultvalue' and column_name='created'"; + + db.connector.query(query, function (err, results) { + assert.equal(results[0].column_default, "now()"); + done(err); + }); + }); + + it('should create a record with default value', function (done) { + Post.create({oneMore: 3}, function (err, p) { + should.not.exists(err); + Post.findOne({where: {defaultInt: 5}}, function (err, p) { + should.not.exists(err); + should.exists(p); + p.should.have.property('defaultInt', 5); + done(); + }); + }); + }); + + it('should create a record with custom value', function (done) { + Post.create({oneMore: 2, defaultInt: 6}, function (err, p) { + should.not.exists(err); + Post.findOne({where: {defaultInt: 6}}, function (err, p) { + should.not.exists(err); + should.exists(p); + p.should.have.property('defaultInt', 6); + done(); + }); + }); + }); +}) From a7cd59a763ef591db145a979cbf9c5bab006a73f Mon Sep 17 00:00:00 2001 From: Andrey Loukhnov Date: Fri, 23 Jan 2015 12:52:54 +0300 Subject: [PATCH 3/3] one-line fix for #51 --- lib/postgresql.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/postgresql.js b/lib/postgresql.js index 8e94a976..b2616af3 100644 --- a/lib/postgresql.js +++ b/lib/postgresql.js @@ -236,7 +236,7 @@ PostgreSQL.prototype._categorizeProperties = function(model, data) { }); var props = Object.keys(this._models[model].properties); var nonIdsInData = Object.keys(data).filter(function(key) { - return props.indexOf(key) !== -1 && ids.indexOf(key) === -1; + return props.indexOf(key) !== -1 && ids.indexOf(key) === -1 && data[key] !== undefined; }); return { ids: ids, @@ -387,6 +387,9 @@ PostgreSQL.prototype.toFields = function (model, data, forCreate) { var nonIdsInData = props.nonIdsInData; var query = []; if (forCreate) { + if(nonIdsInData.length == 0 && dataIdNames.length == 0) { + return 'default values'; + } query.push('('); query.push(nonIdsInData.map(function (key) { return self.columnEscaped(model, key);