2.0.3 • Published 2 years ago

neat-sql-template v2.0.3

Weekly downloads
56
License
MIT
Repository
github
Last release
2 years ago

neat-mysql

A library to generate SQL templates in a neat way. Works well with either of: g

Installation

npm i neat-sql-template

neat-sql-template has typescript typings out of the box.

Usage

Template using tag

import { SQL } from 'neat-sql-template';

const statement = SQL`
  UPDATE people SET name = ${'John'} WHERE id = ${1}
`;
// { sql: 'UPDATE people SET name = ? WHERE id = ?', values: ['John', 1] }

const concatenatedStatement = SQL`
  UPDATE people SET name = (${SQL`SELECT name from people WHERE id = ${2}`}) WHERE id = ${1}
`;
/*
  {
    sql: 'UPDATE people SET name = (SELECT name from people WHERE id = ?) WHERE id = ?;',
    values: [2, 1]
  }
*/

{
      statement: '\n' +
        'UPDATE people SET name = SELECT name from people WHERE id = ? WHERE id = ?\n',
      arguments: [ '2', '1' ]
    }

Template using javascript objects

import { sqlTemplate } from 'neat-sql-template';

const people = [
  { name: 'John', email: 'john@mail.com' },
  { name: 'Tim', email: 'tim@mail.com' },
  { name: 'Benjamin', email: 'ben@mail.com' },
]

const insertStatement = sqlTemplate.insert(people[0], 'people');
/*
  {
    sql: 'INSERT INTO people (name, email) VALUES (?, ?);',
    values: ['John', 'john@mail.com']
  }
*/

const insertMultipleStatement = sqlTemplate.insertMultiple(people, 'people');
/*
  {
    sql: 'INSERT INTO people (name, email) VALUES (?, ?), (?, ?), (?, ?);',
    values: ['John', 'john@mail.com', 'Tim', 'tim@mail.com', 'Benjamin', 'ben@mail.com']
  }
*/

const updateStatement = sqlTemplate.update({ ...people[0], id: 1 }, 'people');
/*
  {
    sql: 'UPDATE people SET name = ?, email = ? WHERE id = ?',
    values: ['John', 'john@mail.com', 1]
  }
*/
2.0.3

2 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago