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

sql-fn

v0.0.1

Published

generate code from SQL files

Downloads

180

Readme

sql-fn

SQL in, functions out. No ORM. No learning curve.

Loosely inspired by clojure's hugsql library.

Basic Usage

Given a directory of SQL files, each file containing one statement, something like this:

./sql/
    findOnePersonById.sql
    findOnePersonByName.sql
    findAllPersons.sql
    createOnePerson.sql
    updateOnePersonById.sql
    deleteOnePersonById.sql

Generate code like this:

const config = {
    /* postgres config: db, username, password, etc. 
    This is the same config you'd pass to pg.Pool
    */
};

/* generate code from SQL files */
const {generate} = require('sql-fn')(config);

/* generated SQL functions have same names as filenames */
const {
    fns: {
        findOnePersonById,
        findOnePersonByName,
        findAllPersons,
        createOnePerson,
        updateOnePersonById,
        deleteOnePersonById
    }
} = generate('./sql');

(async () => {
    let rows = await createOnePerson(1, 'John', 'Smith', 32, '555-1212');
    const person = await findOnePersonById(1);
    console.log(person);
});

Options

By default, any query (SQL filename) beginning with findOne, updateOne, createOne, or deleteOne is assumed to return a single row, and the generated code will return first row only.

Similarly, any SQL command which has UPDATE, DELETE, or INSERT results in the operation being wrapped in a transaction.

These rules can be changed, or overridden, for individual queries, via an options parameter.

Override Global setting

In the example below, we use the options keys tx and single to turn transaction wrapping off entirely, and return single result for any sql containing the text "SELECT".

const {generate, withOptions} = require('sql-fn')(config);
const {
    fns: {
        findOnePersonById,
        findOnePersonByName,
        findAllPersons,
        createOnePerson
    }
} = generate('./sql', withOptions({ tx: (name, sql) => false, single: (name, sql) => sql.includes('SELECT')} );

Override Individual Queries

In the example below, the createOnePerson query has been overridden to not be a transaction, and returning single row.

const {generate, withOptions} = require('sql-fn')(config);
const {
    fns: {
        findOnePersonById,
        findOnePersonByName,
        findAllPersons,
        createOnePerson
    }
} = generate('./sql', withOptions({ createOnePerson: {single: true, tx: false} );

Cursors

Individual queries can be configured to return cursors. This setting overrides both tx and single, which will not be applied for a cursor-returning query.

const {generate, withOptions} = require('sql-fn')(config);
const {
    fns: {
        findAllPersons
    }
} = generate('./sq', withOptions({ findAllPersons: {cursor: 100}}));

(async () => {
    for await (const rows of findAllPersons()) {
        // write to stream, probably
    }
});

Advanced Use Cases

Three advanced use cases are supported with regard to transactions, txSeries, txParallel, and txWaterfall. These loosely map to the corresponding operations in the async node package. To use these, you must drop below the level of generated code and use underlying functions that power this library.

txSeries

Execute a series of statements in series, in a transaction

const config = { ... };

/* generate code from SQL files */
const {generate, withTransaction, txSeries} = require('sql-fn')(config);

/* use sql key to access raw sql query */
const {
    sql: {
        updateOnePersonById,
    }
} = generate('./sql');

(async () => {
    const results = await withTransaction(txSeries(
        updateOnePersonById,
        ['12345', 'John', 'Deere', 23, '123-345-6789'],
        updateOnePersonById,
        ['9999', 'Some', 'Guy', 99, '555-555-1212']
    ));
});

txParallel

Execute a series of statements in parallel, in a transaction

const config = { ... };

/* generate code from SQL files */
const {generate, withTransaction, txParalel} = require('sql-fn')(config);

/* use sql key to access raw sql query */
const {
    sql: {
        updateOnePersonById,
    }
} = generate('./sql');

(async () => {
    const results = await withTransaction(txParallel(
        updateOnePersonById,
        ['12345', 'John', 'Deere', 23, '123-345-6789'],
        updateOnePersonById,
        ['9999', 'Some', 'Guy', 99, '555-555-1212']
    ));
});

txWaterfall

Execute a series of statements in series, where the prior results is passed into the next

const config = { ... };

/* generate code from SQL files */
const {generate, withTransaction, txWaterfall} = require('sql-fn')(config);

/* use sql key to access raw sql query */
const {
    sql: {
        updateOnePersonById,
    }
} = generate('./sql');

(async () => {
    const results = await withTransaction(txWaterfall(
        updateOnePersonById,
        () => ['12345', 'John', 'Deere', 23, '123-345-6789'],
        updateOnePersonById,
        (prior) => ['9999', 'Some', 'Guy', prior.rows[0].age + 1, '555-555-1212']
    ));
});