# sql-buddy

> Tiny sql query builder

Latest version **1.0.8** (published 2024-10-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install sql-buddy
pnpm add sql-buddy
yarn add sql-buddy
bun add sql-buddy
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2024-10-20 |
| First published | 2015-10-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Daniel Pihlstrom |
| Maintainers | sciolist |

## Links

- npm: https://www.npmjs.com/package/sql-buddy
- Repository: https://github.com/sciolist/node-sql-buddy
- Homepage: https://github.com/sciolist/node-sql-buddy#readme
- Issues: https://github.com/sciolist/node-sql-buddy/issues
- npm.io page: https://npm.io/package/sql-buddy

## Recent versions

- 1.0.8 (latest) — 2024-10-20
- 1.0.7 — 2024-10-09
- 1.0.6 — 2024-09-02
- 1.0.5 — 2024-09-02
- 1.0.4 — 2024-09-02
- 1.0.3 — 2024-09-01
- 1.0.2 — 2024-09-01
- 1.0.1 — 2017-04-27
- 1.0.0 — 2017-04-27
- 0.0.3 — 2015-10-08
- 0.0.2 — 2015-10-07
- 0.0.1 — 2015-10-06

## README

# sql-buddy

Just tracks parameter orders for sql queries.

## Options

    import SqlBuddy, { pgsql } from 'sql-buddy';
    
    const sql = SqlBuddy({
        // change the '$' prefix to another character, such as '@' for mssql
        parameterNamePrefix: '$',
        
        // change how the parameter name is built
        parameterNameFormat(index, opts) { return '$' + index }

        // change the parameter value before sending
        parameterValueFormat(value, opts) {
            if (value instanceof Date) return +value;
            return value;
        }
    });

    sql('select 1');

    // there is also a postgres default as 'pgsql';

    pgsql('select 1');


## Usage

You can piece together your sql by appending query bits:

    import { pgsql } from 'sql-buddy';

    const query = pgsql`select ${'one'}` // using template strings
      .append('select $1;', ['two']);    // or parameter arrays

    query.text;       // 'select $1; select $2;'
    query.names;      // ['$1', '$2'];
    query.values;     // ['one', 'two'];

You can also append an array of values, which will automatically place commas between each:

    import { pgsql } from 'sql-buddy';
    const values = [{ id: 'one' }, { id: 'two' }]

    const query = pgsql()
      .append`insert into mytable values`
      .multiAppend(values, (q, it) => q`(${it.id})`)

    query.text;      // 'insert into mytable values ($1) , ($2)'
    query.names;     // ['$1', '$2'];
    query.values;    // ['one', 'two'];

## Postgres example

```
import pg from 'pg';
import { pgsql } from 'sql-buddy'; // use default settings for postgresql

const conn = new pg.Client();
await conn.connect();

const id = 5;

const result = await conn.query(pgsql`select * from mytable where id = ${id}`);

console.log(result.rows);
```

---
_Source: https://npm.io/package/sql-buddy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
