<commit_before>/*!
	GET requests handler
*/


var squel = require('squel');
var Promise = require('bluebird');

var {filterBy, selectBy} = require('./queryBuilder.js');


var handleGet = function (request, body, context) {
	squel.useFlavour('mysql');

	var primary_query = selectBy(request.primary, context);
	var linked_queries = request.linked.map((linked) => selectBy(linked, context));

	var promises = [].concat.apply([primary_query], [linked_queries]).map((query) => context.callQuery(query));

	var buildResponse = function (result) {
		return result.length > 1 ? result : result[0];
	}

	return Promise.all(promises).map((res) => res[0]).then((results) => {
		var response = {};

		if (0 === results[0].length) return {response, status: 404};

		var linked_index = {};
		var foreigns = context.resources[request.primary.resource].keys.foreigns.map((foreign) => foreign.field);
		foreigns.forEach((foreign) => {
			linked_index[foreign] = {};
		});
		foreigns.forEach((foreign) => {
			results[0].forEach((result) => {
				linked_index[foreign][result[foreign]] = linked_index[foreign][result[foreign]] || [];
				linked_index[foreign][result[foreign]].push(result);
			});
		});

		response[request.primary.resource] = buildResponse(results[0]);

		if (request.linked.length) {
			response.linked = {};
			results.slice(1).filter((r) => 0 !== r.length).forEach((result, i) => {
				//response.linked[request.linked[i].resource] = buildResponse(result);

				var primary_key = context.resources[request.linked[i].resource].keys.primary;
				var foreign_key = request.linked[i].superset.foreign.field;

				result.forEach((object) => {
					object.type = request.linked[i].resource;
					linked_index[foreign_key][object[primary_key]].forEach((primary) => {
						primary.links = primary.links || {};
						delete primary[foreign_key];
						primary.links[foreign_key] = object;
					});
				});
			});
		}

		return {response, status: 200};
	});
};

module.exports = handleGet;<commit_msg>works with the updated core, still to improve linked resources handling<commit_after>/*!
	GET requests handler
*/


var squel = require('squel');
var Promise = require('bluebird');

var {filterBy, selectBy} = require('./queryBuilder.js');


var handleGet = function (request, body, context) {
	squel.useFlavour('mysql');

	let primary_query = selectBy(request.primary, context);
	let linked_queries = request.linked.map((linked) => selectBy(linked, context));

	var promises = [].concat(primary_query, linked_queries).map((query) => context.callQuery(query));

	return Promise.all(promises).map((res) => res[0]).then((results) => {

		let response = {};

		// empty response
		if (0 === results[0].length) {
			return {response, status: 404};
		}

		// primary resource
		response.data = results[0];
		// add type member
		response.data.forEach((result) => result.type = request.primary.type);
		// if its a single object, unwrap it from the array
		if (response.data.length === 1) {
			response.data = response.data[0];
		}

		// are there any additional resources?
		if (request.linked.length) {
			response.linked = {};
			
			results.slice(1).forEach((result, i) => {
				response.linked[request.linked[i].type] = result;
			});
		}

		return {response, status: 200};
	});
};

module.exports = handleGet;
{'fraction_non_alphanumeric': 0.17342615012106538, 'fraction_numerical': 0.00847457627118644, 'mean_word_length': 4.296474358974359, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1958412', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1008, 'n_words': 203}