Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/guidesmiths/sql-params-format

Formatter for named sql params
https://github.com/guidesmiths/sql-params-format

Last synced: 22 days ago
JSON representation

Formatter for named sql params

Awesome Lists containing this project

README

        

# sql-params-format

a wrapper around pg-format to allow named parameters to sql queries.

```js
// default usage, no named params. operates just like pg-format
const sql = 'insert into %I (select * from foo where %s and bar > %L)';
const formatted = format(sql, 'a_table', 'something=another', 123);
formatted => insert into a_table (select * from foo where something=another and bar > '123')

// using named parameters

const sql = 'insert into %I:one (select * from foo where %s:two and bar > %L:three)';
const formatted = format(sql, {one: 'a_table', two: 'something=another', three: 123});
formatted => insert into a_table (select * from foo where something=another and bar > '123')

```

It will ignore any unused named parameters.
Parameters used more than once in the query need only be passed once.

Special note, this will convert moment objects to date objects if passed as parameters.