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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,112 @@ PostgreSQL.prototype.applyPagination = function(model, stmt, filter) {
return stmt.merge(limitClause);
};

/**
* Assign a value to a nested dictionary, with a list of keys.
* @param {object} obj The target of the assignment
* @param {array} keys A list of keys to use for assignment
* @param {any} value The value to assign the last key to.
* @returns {object} Model data object
*/
function deepAssign(obj, keys, value) {
if(keys.length > 0) {
obj[keys[0]] = deepAssign(obj[keys[0]] || {}, keys.slice(1), value)
return obj
} else {
return value
}
}

/**
* Transform the row data into a model data object
* @param {string} model Model name
* @param {object} rowData An object representing the row data from DB
* @returns {object} Model data object
*/
PostgreSQL.prototype.fromRow = PostgreSQL.prototype.fromDatabase =
function(model, rowData) {
if (rowData == null) {
return rowData;
}
var props = this.getModelDefinition(model).properties;
var data = {};
for (var p in props) {
var columnName = this.column(model, p);
// Load properties from the row
var columnValue = this.fromColumnValue(props[p], rowData[columnName]);
if (columnValue !== undefined) {
data[p] = columnValue;
} else {
var keys = Object.keys(rowData).filter(function(row) {
return row.startsWith(columnName + '__');
})
for(var k in keys) {
data = deepAssign(data, keys[k].split('__'), rowData[keys[k]])
}
}
}
return data;
};

/**
* Build a list of escaped column names for the given model and fields filter
* @param {string} model Model name
* @param {object} filter The filter object
* @returns {string} Comma separated string of escaped column names
*/
PostgreSQL.prototype.buildColumnNames = function(model, filter) {
var fieldsFilter = filter && filter.fields;
var cols = this.getModelDefinition(model).properties;
if (!cols) {
return '*';
}
var self = this;
var keys = Object.keys(cols);
if (Array.isArray(fieldsFilter) && fieldsFilter.length > 0) {
// Not empty array, including all the fields that are valid properties
keys = fieldsFilter.filter(function(f) {
let p = cols[f];
// See if we are querying nested json
if (p == null && isNested(f) && cols[f.split('.')[0]] != null) {
p = f;
}
return p;
});
} else if ('object' === typeof fieldsFilter &&
Object.keys(fieldsFilter).length > 0) {
// { field1: boolean, field2: boolean ... }
var included = [];
var excluded = [];
keys.forEach(function(k) {
Object.keys(fieldsFilter).map(function(f) {
if(f.split('.')[0] === k) {
if(fieldsFilter[f]) {
included.push(f);
} else if (!fieldsFilter[f]) {
excluded.push(k);
}
}
})
});
if (included.length > 0) {
keys = included;
} else if (excluded.length > 0) {
excluded.forEach(function(e) {
var index = keys.indexOf(e);
keys.splice(index, 1);
});
}
}
var names = keys.map(function(c) {
if(isNested(c)) {
return self.columnEscaped(model, c) + ' as ' + c.replace(/\./g, '__'); // TODO: Escape c
} else {
return self.columnEscaped(model, c);
}
});
return names.join(',');
};

PostgreSQL.prototype.buildExpression = function(columnName, operator,
operatorValue, propertyDefinition) {
switch (operator) {
Expand Down
Loading