Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested tables on join #144

Closed
matbalazsi opened this issue Feb 14, 2018 · 5 comments
Closed

nested tables on join #144

matbalazsi opened this issue Feb 14, 2018 · 5 comments

Comments

@matbalazsi
Copy link

Hi,
Using feathers-knex (postgresql), I am trying to nest my tables on a join. Here is an example on how to achieve this with node-postgres.

brianc/node-postgres#733

SELECT to_json(a.*) AS table1, to_json(b.*) AS table2 FROM some_table a LEFT JOIN another_table b ON a.id = b.id;
With this all rows will be structured as such:

{ table1: { ... },
table2: { ... } }

However, using:
const query = this.createQuery({ query: hook.params.query });
query.select(to_json(a.*) AS table1)

is giving me an error because it assumes that the table name is “to_json(a”. Is there a way to generate raw queries with feathers-knex?

@daffl
Copy link
Member

daffl commented Feb 14, 2018

You can create raw queries using this.Model which is the db instance you initially passed.

@matbalazsi
Copy link
Author

thanks! I wanted to create the join query in a hook, but setting
hook.params.knex = hook.app.service('a').Model.raw('SELECT * FROM etc....');
doesn't work. Would you suggest creating a new custom service for this query, or is it possible through hooks?

@matbalazsi
Copy link
Author

I'm closing the issue, thanks for the quick response! If possible, does anyone have any suggestion regarding the best practice for these types of joins (as mentioned in my previous comment)?

@roelvan
Copy link

roelvan commented Mar 2, 2018

This is what works for me (full before hook example):

const { mapKeys } = require('lodash');

module.exports = (options = {}) => { // eslint-disable-line no-unused-vars
  return async context => {
    // PREVENT: "column reference "[column name]" is ambiguous"
    const sort = mapKeys(context.params.query.$sort, (order, field) => {
      return `plannings.${field}`;
    });
    context.params.query.$sort = sort;
    
    const query = context.service.createQuery({ query: context.params.query });
    query
      .count('plannings.id as planninRuleCount')
      .leftJoin('planningRules as plr', 'plr.planningId', 'plannings.id')
      .groupBy('plannings.id');
    
    if (context.id) // Scope the query if it's a single GET call
      query.where('plannings.id', context.id);
    
    context.params.knex = query;
    return context;
  };
};

@xiaguoli
Copy link

xiaguoli commented Mar 27, 2018

you can try like this:

find: [
  function (hook) {
    

    const query = this.db().column('t_wip_type.product_type','t_wip_type.product_type_code','t_wip_type.code_rules','t_wip_code.*').select();

    query.leftOuterJoin('t_wip_type', 't_wip_code.wip_type_id', 't_wip_type.id');

    hook.params.knex = query;

    return hook;

    //console.log(query.toSQL().toNative())
  }],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants