Skip to content

Commit

Permalink
Merge pull request #724 from CartoDB/es6
Browse files Browse the repository at this point in the history
 Apply ES2018 syntax uniformly
  • Loading branch information
dgaubert authored May 1, 2020
2 parents 47e9e10 + 7b6d2e8 commit 1aac151
Show file tree
Hide file tree
Showing 55 changed files with 1,959 additions and 2,387 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Version 6.0.1
2020-mm-dd

Announcements:
- Apply ES2018 syntax uniformly (#724)
- Better error messages for TorqueRenderer.

# Version 6.0.0
2020-04-05
Expand Down
7 changes: 0 additions & 7 deletions lib/backends/README.md

This file was deleted.

145 changes: 70 additions & 75 deletions lib/backends/attributes.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,89 @@
'use strict';

const PSQL = require('cartodb-psql');

const RendererParams = require('../renderers/renderer-params');
const parseDbParams = require('../renderers/renderer-params');
const Timer = require('../stats/timer');
const SubstitutionTokens = require('cartodb-query-tables').utils.substitutionTokens;

function AttributesBackend () {}

module.exports = AttributesBackend;

/// Gets attributes for a given layer feature
//
/// Calls req2params, then expects parameters:
///
/// * token - MapConfig identifier
/// * layer - Layer number
/// * fid - Feature identifier
///
/// The referenced layer must have been configured
/// to allow for attributes fetching.
/// See https://github.com/CartoDB/Windshaft/wiki/MapConfig-1.1.0
///
/// @param testMode if true generates a call returning requested
/// columns plus the fid column of the first record
/// it is only meant to check validity of configuration
///
AttributesBackend.prototype.getFeatureAttributes = function (mapConfigProvider, params, testMode, callback) {
const timer = new Timer();

mapConfigProvider.getMapConfig((err, mapConfig) => {
if (err) {
return callback(err);
}

const layer = mapConfig.getLayer(params.layer);
if (!layer) {
const error = new Error(`Map ${params.token} has no layer number ${params.layer}`);
return callback(error);
}
module.exports = class AttributesBackend {
// Gets attributes for a given layer feature
//
// * token - MapConfig identifier
// * layer - Layer number
// * fid - Feature identifier
//
// The referenced layer must have been configured
// to allow for attributes fetching.
// See https://github.com/CartoDB/Windshaft/wiki/MapConfig-1.1.0
//
// @param testMode if true generates a call returning requested
// columns plus the fid column of the first record
// it is only meant to check validity of configuration
//
getFeatureAttributes (mapConfigProvider, params, testMode, callback) {
const timer = new Timer();

mapConfigProvider.getMapConfig((err, mapConfig) => {
if (err) {
return callback(err);
}

timer.start('getAttributes');
const attributes = layer.options.attributes;
if (!attributes) {
const error = new Error(`Layer ${params.layer} has no exposed attributes`);
return callback(error);
}
const layer = mapConfig.getLayer(params.layer);
if (!layer) {
const error = new Error(`Map ${params.token} has no layer number ${params.layer}`);
return callback(error);
}

const dbParams = Object.assign(
{},
RendererParams.dbParamsFromReqParams(params),
mapConfig.getLayerDatasource(params.layer)
);

let pg;
try {
pg = new PSQL(dbParams);
} catch (error) {
return callback(error);
}
timer.start('getAttributes');
const attributes = layer.options.attributes;
if (!attributes) {
const error = new Error(`Layer ${params.layer} has no exposed attributes`);
return callback(error);
}

const sql = getSQL(attributes, pg, layer, params, testMode);
const dbParams = Object.assign(
{},
parseDbParams(params),
mapConfig.getLayerDatasource(params.layer)
);

let pg;
try {
pg = new PSQL(dbParams);
} catch (error) {
return callback(error);
}

pg.query(sql, (err, data) => {
timer.end('getAttributes');
const sql = getSQL(attributes, pg, layer, params, testMode);

if (err) {
return callback(err);
}
pg.query(sql, (err, data) => {
timer.end('getAttributes');

if (testMode) {
return callback(null, null, timer.getTimes());
}
if (err) {
return callback(err);
}

const featureAttributes = extractFeatureAttributes(data.rows);
if (testMode) {
return callback(null, null, timer.getTimes());
}

if (!featureAttributes) {
const rowsLengthError = new Error(
`Multiple features (${data.rows.length}) identified by ` +
`'${attributes.id}' = ${params.fid} in layer ${params.layer}`
);
if (!data.rows.length) {
rowsLengthError.http_status = 404;
const featureAttributes = extractFeatureAttributes(data.rows);

if (!featureAttributes) {
const rowsLengthError = new Error(
`Multiple features (${data.rows.length}) identified by ` +
`'${attributes.id}' = ${params.fid} in layer ${params.layer}`
);
if (!data.rows.length) {
rowsLengthError.http_status = 404;
}
return callback(rowsLengthError);
}
return callback(rowsLengthError);
}

return callback(null, featureAttributes, timer.getTimes());
}, true); // use read-only transaction
});
return callback(null, featureAttributes, timer.getTimes());
}, true); // use read-only transaction
});
}
};

function getSQL (attributes, pg, layer, params, testMode) {
Expand Down
9 changes: 0 additions & 9 deletions lib/backends/index.js

This file was deleted.

Loading

0 comments on commit 1aac151

Please sign in to comment.