npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

flat2hier-graphql

v0.0.2

Published

Transform flat sql query result array into a hierarchical graphql like response

Downloads

9

Readme

flat2hier-graphql

Folds your JOINNED sql rows into a hierarchy of objects suitable for graphql response.

Get started

npm install --save flat2hier-graphql

How it works

flat2hier exposes a single function flat2hier(rows: Array<Object>, removeDollor: boolean): Array<Object> | Object

It takes an array of object and groups the objects by the follwing rules/convention and returns an array

Conventions

  • For each row it splits each keys by _ say level_keys
  • For each level key it adds an object or array for its value if it is not the last level_keys
  • If a level_key starts with $ it adds an array, otherwise an object
  • If level_key starts with $ and it has a child level key id it uses that id for subsequest row to insert in that same object in the array

Usage

Pass your row result renamed using _ for indention and pass it to flat2hier

P.S When using sql as to rename column u may quote them as some database like postgres otherwise doesn't preserve case.

const flat2hier = require('flat2hier')
const sqlRow = [ {
    $query_id: '1',
    $query_firstName: 'Ramu',
    $query_lastName: 'Pal',
    $query_$assignedCars_id: '1',
    $query_$assignedCars_name: 'Toyota XI',
    $query_$assignedCars_licenseNo: 'WB XX XXXX'
}, {
    $query_id: '1',
    $query_firstName: 'Ramu',
    $query_lastName: 'Pal',
    $query_$assignedCars_id: '2',
    $query_$assignedCars_name: 'Toyota XII',
    $query_$assignedCars_licenseNo: 'HR XX XXXX'
}, {
    $query_id: '2',
    $query_firstName: 'Sanatan',
    $query_lastName: 'Sarkar',
    $query_$assignedCars_id: '4',
    $query_$assignedCars_name: 'Seadan V5',
    $query_$assignedCars_licenseNo: 'WB XXX XXXX'
}]

flat2hier(sqlRow)
/* Returns: (note pass true as 2nd param to flat2hier if u want to remove the leading $ sign)
{ '$query':
   [ { id: '1',
       firstName: 'Ramu',
       lastName: 'Pal',
       '$assignedCars':
        [ { id: '1', name: 'Toyota XI', licenseNo: 'WB XX XXXX' },
          { id: '2', name: 'Toyota XII', licenseNo: 'HR XX XXXX' } ] },
     { id: '2',
       firstName: 'Sanatan',
       lastName: 'Sarkar',
       '$assignedCars': [ { id: '4', name: 'Seadan V5', licenseNo: 'WB XXX XXXX' } ] } ] }
*/

Examples

Suppose we have two tables

TABLE 1 cars

TABLE 2 drivers

And every driver is assigned to zero or more cars using another table

TABLE 3 assigned_cars

Now, Suppose we have a graphql schema like:

type Query {
    allCars(limit: Int): [Car]
    allDrivers(limit: Int): [Driver]
    driver(id: Int!): Driver
}

type Car {
    id: Int
    name: String
    licenseNo: String
}

type Driver {
    id: Int
    firstName: String
    lastName: String
    assignedCars: [Car]
}

For resolving allDrivers we can retrive all rows using sql join. But we need to group by driver and make an array of Car for same driver. flat2hier does this using some simple convention. e.g.

return database.queryAsync(`
    SELECT 
        d.id as \`$query_id\`,
        d.first_name as \`$query_firstName\`,
        d.last_name as \`$query_lastName\`,
        c.id as \`$query_$assignedCars_id\`,
        c.name as \`$query_$assignedCars_name\`,
        c.license_no as \`$query_$assignedCars_licenseNo\`,
    FROM drivers d
    JOIN assigned_cars asg_c ON asg_c.driver_id == d.id
    JOIN cars c ON asg_c.car_id == c.id
`).then(rows =>  flat2Hier(rows, true).query)

Convention used in flat2hier

flat2hier folds your array of row by

  • Breaking each key/col name by _, and add the partial key into a object hierarchy(new object if at top level otherwise at current level). e.g. user_mailbox_certs_ssl128 => { user: { mailbox: { certs: { ssl128: <value> } } } }
  • If a partial key(after splitting by _) starts with $ instead of adding a object for that key, it adds an array of object. e.g. $query_queryMetadata_sender_id => {$query: [ { queryMetadata: { sender: {id: <value>}} } ]}
  • If it finds another partial key with starting with a $ and has a partial key child with name id it tries to find a object in the same key with matching id, if it finds one it creates object at current level and append in the array it just found.

For flat2hier 's usage see example/graphql.js file

Running included examples

  • Clone this repository
  • Install dev dependencies
  • run example/server.js with nodejs
git clone https://github.com/Eyezon/flat2hier-graphql
yarn install # or npm install
node example/server.js