npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@marianmeres/data-to-sql-params

v1.1.1

Published

A single helper function to convert data objects to a numbered list of parameters. Useful for automatically creating SQL statements.

Downloads

34

Readme

@marianmeres/data-to-sql-params

A single helper function to convert data objects to various helper lists. Useful for programmatically creating SQL statements.

Installation

npm install @marianmeres/data-to-sql-params

Signature

function dataToSqlParams(
	data: Record<string, any>,
	extractor?: string[] | Record<string, CallableFunction | true>
): {
	keys: string[];
	placeholders: string[];
	values: any[];
	pairs: string[];
	map: Record<string, any>;
	_next: number;
	_extractor: Record<string, CallableFunction>;
} {
	/* ... */
}

Example usage

import { dataToSqlParams } from '@marianmeres/data-to-sql-params';
// no extractor example - all defined keys are extracted
const result = dataToSqlParams({ a: 1, x: undefined, b: 2, c: 3 });
/* {
    keys: [ '"a"', '"b"', '"c"' ],
    placeholders: [ '$1', '$2', '$3' ],
    pairs: [ '"a"=$1', '"b"=$2', '"c"=$3' ],
    values: [ 1, 2, 3 ],
    map: { $a: 1, $b: 2, $c: 3 },
    _next: 4,
    _extractor: Record<string, CallableFunction> (actual value omitted)
} */
// extractor as a plain key whitelist (note that "x" will not be part of output)
const result = dataToSqlParams({ a: 1, x: undefined, b: 2, c: 3 }, ['b', 'c', 'x']);
/* {
    keys: [ '"b"', '"c"' ],
    placeholders: [ '$1', '$2' ],
    values: [ 2, 3 ],
    pairs: [ '"b"=$1', '"c"=$2' ],
    map: { $b: 2, $c: 3 },
    _next: 3,
    _extractor: Record<string, CallableFunction> (actual value omitted)
} */
// custom extractor example - transforming data upon extraction
// eg. cast to string, JSON encode, etc...
// (explicit true below just whitelists the given key)
const result = dataToSqlParams(
	{ id: 1, x: undefined, b: 2, c: 3 },
	{ id: true, c: (v) => `${v + v}` }
);
/* {
    keys: [ '"id"', '"c"' ],
    placeholders: [ '$1', '$2' ],
    values: [ 1, '6' ],
    pairs: [ '"id"=$1', '"c"=$2' ],
    map: { $id: 1, $c: '6' },
    _next: 3,
    _extractor: Record<string, CallableFunction> (actual value omitted)
} */

What is it good for?

For helping to programmatically build SQL statements. For example:

// assuming result from the last example above
let { keys, values, placeholders, pairs, map, _next } = result;

if (fooRecordExists) {
	const pk = 'id'; // example
	sql = `update foo set ${pairs} where "${pk}" = $${_next++}`;
	// update foo set "id" = $1,"c" = $2 where "id" = $3
	values.push(_extractor[pk](data[pk]));
} else {
	sql = `insert into foo (${keys}) values (${placeholders})`;
	// insert into foo ("id", "c") values ($1, $2)
}

// and now execute the statement
await db.query(sql, values);

// or we can use the named map (if the db supports it), eg:
await db.run('update foo set c = $c where id = $id', map);