# all files / keystone/lib/list/ selectColumns.js

13.33% Statements 2/15
0% Branches 0/6
0% Functions 0/2
13.33% Lines 2/15

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    1×

    1×
     /**
     * Specified select and populate options for a query based the provided columns.
     *
     * @param {Query} query
     * @param {Array} columns
     */
    function selectColumns (q, cols) {
    	// Populate relationship columns
    	var select = [];
    	var populate = {};
    	var path;
    	cols.forEach(function (col) {
    		select.push(col.path);
    		if (col.populate) {
    			if (!populate[col.populate.path]) {
    				populate[col.populate.path] = [];
    			}
    			populate[col.populate.path].push(col.populate.subpath);
    		}
    	});
    	q.select(select.join(' '));
    	for (path in populate) {
    		if (populate.hasOwnProperty(path)) {
    			q.populate(path, populate[path].join(' '));
    		}
    	}
    }

    module.exports = selectColumns;