const { setSurvey, pool } = require('../connection');

async function get (projectId, path, opts = {}) {

	const text = `SELECT meta data FROM public.projects WHERE pid = $1;`;
	const res = await pool.query(text, [projectId]);

	const config = res.rows.length == 1
		? res.rows[0].data
		: res.rows.map(r => r.data);

	if (path) {
		return path.split('/').filter(i => i !== "").reduce( (obj, idx) =>
			(typeof obj !== 'undefined' && obj !== null) ? obj[idx] : undefined, config);
	} else {
		return config;
	}
}

module.exports = get;
