Git Product home page Git Product logo

sql-methods-in-node.js's Introduction

no# PostgreSQL methods for node.js PostgreSQL scripts as object methods in node.js for database-intensive applications

SQL file syntax

Example - four trivial SQL queries with method specifiers.

-- A query that yields a single record
--! {"name": "the_first_method", "returns": "record"}
SELECT n num, format('As text: %s', n) txt
  from generate_series(1, 100, 1) s(n)
 where n = $1;
  
-- A query that yields a single value
--! {"name": "the_second_method", "returns": "value"}
SELECT to_char(n, 'FMRN') rn
  from generate_series(1, $1, 1) s(n)
 where n = $2;

-- A query that yields a set of records
--! {"name": "the_third_method", "returns": "recordset"}
SELECT n, to_char(n, 'FMRN') rn
  from generate_series(1, $1, 1) s(n);

-- A query with no parameters
--! {"name": "alternative_method", "returns": "record"}
SELECT 1 as num, 'one' as eng, 'edno' as bul, 'bir' as tur, 'ANY' as amount;

Method specifiers are lines that start with --! folowed by JSON with exactly these mandatory attributes:

  1. "name" - specifies the method name as a valid identifier, up to 63 characters;
  2. "returns" - specifies the method return type. Can be one of:
    • "value" - a scalar;
    • "record" - a JSON object representing a single record;
    • "recordset" - a JSON array of objects;
    • "none"

Queries can be of any length and complexity. Comments, empty lines and leading/trailing whitespaces are ignored.

Note

PgMethods uses prepared statements.

Important

SQL files must be UTF-8 encoded.

Building the database gateway object

import PgMethods from './../pg_methods.mjs';
let db_gw = new PgMethods(client);

Importing SQL files

  • A single SQL file can be imported by the constructor
let db_gw = new PgMethods(client,filename);
  • SQL files can be imported using sql_import method
let db_gw = new PgMethods(client);
db_gw.sql_import(filename);
db_gw.sql_import(another_filename);
  • Chained
let db_gw = (new PgMethods(client))
            .sql_import(filename)
            .sql_import(another_filename);

Invoking SQL methods

let res = await db_gw.the_first_method.run(10);

Demo script and result

import PgMethods from './../pg_methods.mjs';
import pg from 'pg';

const pg_client = new pg.Client();  // uses environment variables
await pg_client.connect();
try
{
  let db_gw = new PgMethods(pg_client); 
  db_gw.sql_import('proba.sql')
       .sql_import('append.sql');
  let res_a = await db_gw.the_first_method.run(10),
      res_b = await db_gw.the_second_method.run(100, 26),
      res_c = await db_gw.the_third_method.run(5),
      res_d = await db_gw.alternative_method.run('Количество');
  await db_gw.anonymous_block.run();
  console.log(res_a);
  console.log(res_b);
  console.log(res_c);
  console.log(res_d);
}
finally { await pg_client.end(); } // database connection leak protection

image

sql-methods-in-node.js's People

Contributors

stefanov-sm avatar

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.