Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ lib-cov
*.out
*.pid
*.gz

.lvimrc
pids
logs
results
Expand Down
23 changes: 23 additions & 0 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ PostgreSQL.prototype._categorizeProperties = function(model, data) {
*/
PostgreSQL.prototype.create = function (model, data, callback) {
var self = this;
data = self.filterUndefined(data);
var props = self._categorizeProperties(model, data);

var sql = [];
Expand Down Expand Up @@ -294,6 +295,7 @@ PostgreSQL.prototype.create = function (model, data, callback) {
/*
PostgreSQL.prototype.updateOrCreate = function (model, data, callback) {
var self = this;
data = self.filterUndefined(data);
var props = self._categorizeProperties(model, data);
var idColumns = props.ids.map(function(key) {
return self.columnEscaped(model, key); }
Expand Down Expand Up @@ -342,6 +344,7 @@ PostgreSQL.prototype.updateOrCreate = function (model, data, callback) {
*/
PostgreSQL.prototype.save = function (model, data, callback) {
var self = this;
data = self.filterUndefined(data);
var props = self._categorizeProperties(model, data);

var sql = [];
Expand All @@ -360,6 +363,7 @@ PostgreSQL.prototype.save = function (model, data, callback) {
PostgreSQL.prototype.update =
PostgreSQL.prototype.updateAll = function (model, where, data, callback) {
var whereClause = this.buildWhere(model, where);
data = this.filterUndefined(data);

var sql = ['UPDATE ', this.tableEscaped(model), ' SET ',
this.toFields(model, data), ' ', whereClause].join('');
Expand All @@ -373,6 +377,24 @@ PostgreSQL.prototype.update =
});
};

/*!
* Filters undefined values from model instance data
*
* @param {Object} The model instance data
*/

PostgreSQL.prototype.filterUndefined = function (data) {
// There is no undefined in PostgreSQL and sending NULL for every
// undefined field prevents the database from handling default values.
//
// If you actually want to send NULL values, use NULL values.
Object.keys(data).forEach(function (k) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use plain for-loop to avoid performance penalties. The closure is heavy.

if (data[k] === undefined) delete data[k];
});

return data;
}

/*!
* Build a list of column name/value pairs
*
Expand All @@ -382,6 +404,7 @@ PostgreSQL.prototype.update =
*/
PostgreSQL.prototype.toFields = function (model, data, forCreate) {
var self = this;

var props = self._categorizeProperties(model, data);
var dataIdNames = props.idsInData;
var nonIdsInData = props.nonIdsInData;
Expand Down