Giter Site home page Giter Site logo

argile's Introduction

argile

Convert SQL rows to js object(s).

Installation

npm install --save argile

How does it work ?

Database

capture

The figure above pesents a simple database. We want to retrieve all the posts.

SQL request

The following request returns all the data in the database.

  SELECT
    post.id,
    post.title,
    author.username,
    tag.id AS "idTag",
    tag.label
  FROM post
  INNER JOIN author ON author.id = post.author_id
  LEFT JOIN post_tag ON post_tag.post_id = post.id
  LEFT JOIN tag ON tag.id = post_tag.tag_id
  ORDER BY post.id, tag.label

Construct the JS result

Now, to convert the SQL result to JS objects, we design an object that will be the returned result.
Here, we want to construct by post, an array of tags and an user object.

A string represents the SQL key that will match the correponding return js object property.
The * character tags the SQL string as a primary key. It is imperative to define a primary key for sub arrays.

  {
    id    : 'id',
    title : 'title',
    author : {
      username : 'username'
    },
    tags : [{
      id    : '*idTag',
      label : 'label'
    }]
  }

Conversion

  var argile = require('argile');

  var res = argile.convert(SQLrows, {
    id    : 'id',
    title : 'title',
    author : {
      username : 'username'
    },
    tags : [{
      id    : 'idTag',
      label : 'label'
    }]
  });

We assume SQLrows is an array of JS objects constructed from the SQL request.

The conversion keeps the SQL order defined in the SQL request.

API

The module has only one function argile.convert(SQLrows, constructedResult).

  • {Array} SQLrows
  • {Object} constructedResult

It returns an Array

argile's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.