Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return lefts.length && option.rels.length && go(option.rels, C.map(async function(me) {
const query = me.query();
if (query && query.text) query.text = query.text.replace(/^\s*WHERE/i, 'AND');
var fold_key = me.rel_type == 'x' ?
`_#_${me.where_key.split('.')[1]}_#_` : me.where_key;
const colums = uniq(add_column(me).originals.concat(
me.rel_type != 'x' ? me.as + '.' + me.where_key : me.where_key + ' AS ' + fold_key));
const in_vals = filter(a => a != null, pluck(me.left_key, lefts));
const is_row_num = me.row_number.length == 2;
const rights = (!in_vals.length ? [] : await (is_row_num ?
QUERY `
SELECT *
FROM (
SELECT
${COLUMN(...colums)},
ROW_NUMBER() OVER (PARTITION BY ${CL(me.where_key)} ORDER BY ${me.row_number[1]}) as "--row_number--"
FROM ${TB(me.table)} AS ${TB(me.as)}
${me.xjoin}
WHERE ${IN(me.as +'.'+me.where_key, in_vals)} ${me.poly_type} ${tag(query)}
) AS "--row_number_table--"
WHERE "--row_number_table--"."--row_number--"<=${me.row_number[0]}`
:
QUERY `
SELECT ${COLUMN(...colums)}
cmap(async function(me) {
const f_key_ids = uniq(filter((r) => !!r, pluck(me.left_key, results)));
if (me.rel_type == '-' || !f_key_ids.length) return recur([me, cat(map(r => r._ ? r._[me.as] : null, results))]);
return go(
(!me.left_join_over && me.left_joins.length ?
left_join_query : where_in_query)(me, SQL `WHERE ${IN(me.as + '.' + me.key, f_key_ids)}`, QUERY),
group_by((v) => v[me.key]),
function(groups) {
each(function(result) {
result._ = result._ || {};
result._[me.as] = (groups[result[me.left_key]] || []);
}, results);
return recur([me, cat(map(r => r._ ? r._[me.as] : null, results))]);
}
);
}),
() => results
return async function(strs, ...tails) {
return go(
ready_sqls(strs, tails),
deep_flat,
filter(t => t.as),
each(option => {
option.column = option.column || '*';
option.query = option.query || tag();
option.table = option.table || (option.rel_type == '-' ? plural(option.as) : option.as);
option.rels = [];
option.row_number = option.row_number || [];
}),
function setting([left, ...rest]) {
const cur = [left];
each(me => {
while (!(last(cur).depth < me.depth)) cur.pop();
const left = last(cur);
left.rels.push(me);
if (me.rel_type == '-') {
me.left_key = me.left_key || (me.is_poly ? 'id' : singular(me.table) + '_id');
me.where_key = me.key || (me.is_poly ? 'attached_id' : 'id');
return async function ljoin(strs, ...tails) {
return go(
ready_sqls(strs, tails),
cat,
filter(t => t.as),
each(option => {
option.query = option.query || tag();
option.table = option.table || (option.rel_type == '-' ? option.as + 's' : option.as);
option.column = option.column || CL(...table_columns[option.table]);
option.left_joins = [];
option.rels = [];
option.row_number = option.row_number || [];
}),
([left, ...rest]) => {
const cur = [left];
each(me => {
while (!(last(cur).depth < me.depth)) cur.pop();
const left = last(cur);
if (me.rel_type == '-') {
me.left_key = me.left_key || (me.is_poly ? 'id' : singular(me.table) + '_id');
me.key = me.key || (me.is_poly ? 'attached_id' : 'id');