GithubHelp home page GithubHelp logo

Documentation about json-sql-builder2 HOT 1 CLOSED

planetarydev avatar planetarydev commented on May 30, 2024
Documentation

from json-sql-builder2.

Comments (1)

planetarydev avatar planetarydev commented on May 30, 2024 1

Hi,
json-sql-builder2 is a transpiler so that you can write your SQL-Queries using the JSON instead of writing your queries as plain text and manage the parameters by your own.

Here I will show you a really basic example using a PostgreSQL database and the node-pg driver.

// require the real database driver
// in this example I am using PostgreSQL
// npm install --save pg
const { Client } = require('pg')

const SQLBuilder = require('json-sql-builder2');
// create a new instance of the SQLBuilder and load the language extension for PostgreSQL
var sql = new SQLBuilder('PostgreSQL');

// initialize you connection to your database
const client = new Client({
  host: 'localhost',
  port: 5433,
  database: 'pureworkx',
  user: 'postgres',
  password: 'testtest',
});

// connect to your pg database
client.connect((err) => {
  if (err) {
    console.error('connection error', err.stack)
  } else {
    // okay, now we are connected to the database
    // let's and we can query the database
    console.log('connected');

    // write your SQL-query using json-sql-builder2
    var peoples = sql.$select({
      $from: 'people',
      $where: {
        first_name: 'Koo'
      }
    });
    // now query the database with the transpiled results from json-sql-builder2
    client.query(peoples.sql, peoples.values, (err, result) => {
      if (err) {
        console.log(err);
        return;
      }
      result.rows.forEach(people => {
        console.log(people.id, people.first_name, people.last_name);
      })
    });

    // without json-sql-builder2 you will write your
    // query using plaintext and support your values
    // manually like this:
    var sqlQuery = 'SELECT * FROM people WHERE first_name = $1';
    var queryValues = ['Koo'];
    // query the database using your plain defined SQL and values
    client.query(sqlQuery, queryValues, (err, result) => {
      // output error and results
      if (err) {
        console.log(err);
        return;
      }
      result.rows.forEach(people => {
        console.log(people.id, people.first_name, people.last_name);
      });
    });
  }
});

So you can write your own small database-wrapper using json-sql-builder2 with the database-driver of your choice or use it like the examle above.

I have written a wrapper for PostgreSQL with the package pure-live-pg that uses the json-sql-builder2 and supports reactive queries. Have a look at this repository - but it's not documented at this time. https://github.com/planetarydev/pure-live-pg

I hope this helps you to get started with json-sql-builder2. Feel free to open further issues if you've got more questions.

from json-sql-builder2.

Related Issues (20)

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.