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

db-sql-toolkit

v2.2.0

Published

Helps with SQL statements, database migration, and bulk execution.

Downloads

204

Readme

db-sql-toolkit

Helps with SQL statements, database migration, and bulk execution.

Installation

npm install db-sql-toolkit

Usage

sql

The sql function is a tagged template function to write SQL statements and include parameters in the correct places.

const id = "1234";
const statement = sql`
    SELECT
        name
        , version
        , author
    FROM package
    WHERE
        id = ${id}
`;

The sql function returns a tuple consisting of the statement and an array of passed parameters. In the returned statement all parameters are replaced by question marks (?).

Hint: You can get syntax highlighting in VS Code by installing an extension: es6-string-html

Nesting

You can nest sql statements:

const where = sql`
    id = ${1234}
`;
const statement = sql`
    SELECT
        name
        , version
        , author
    FROM package
    WHERE
        ${where}
`;

SQL literals

If you want to use variables as SQL literals (where no variables are supported), you can use the sqlLiteral function:

const concatCharacter = "|";
const statement = sql`
    SELECT
        GROUP_CONCAT(name, '${sqlLiteral(concatCharacter)}') AS package_names
    FROM package
    GROUP BY author
`;

This inserts the variable concatCharacter as a literal into the SQL statement.

bulkInsertEntities

Insert many entities in as few operations as possible.

const packages: Package[] = [
    // a lot of packages
]

const getParameters = (package: Package): unknown[] => [
    package.id,
    package.name,
    package.version,
    package.author,
];
const statement = sql`
    INSERT INTO package (
        id
        , name
        , version
        , author
    )
    VALUES (${getParameters})
`;

await bulkInsertEntities(database, packages, statement);

This function (and all bulk* functions) uses the MaxVariableNumber property of the Database and splits the operation into multiple operations, if required.

Important: To use this function you have to pass the function to get the parameters for one entity as the only parameter into the sql function.

bulkExecuteCommand

Executes a SQL statement in as few operations as possible.

Important: To use this function one parameter of the sql function has to be an array.

Warning: Do not use the NOT IN operator. If you do so and the operation cannot be executed in one run, you get wrong results.

bulkGetRows

Selects entities in as few operations as possible.

bulkGetCount

Gets the total number of entities in as few operations as possible. You have to select COUNT(*) in the SQL statement.

migrate

With the help of the migrate function you can execute database upgrades.

import { migrate, Database } from "db-sql-toolkit";

async function upgradeDatabase(database: Database): Promise<void> {
    await migrate({
        database,
        // The targetVersion parameter is optional. If you omit it, the migrationMap will be executed until the last version.
        targetVersion: 3,
        migrationMap: [
            [1, createDatabase],
            [2, updateToVersion2],
            [3, updateToVersion3],
        ]
    });
}



async function createDatabase(database: Database): Promise<void> {
    // Create the initial database.
}

// updateToVersion2 and updateToVersion3 are omitted.

See Database for type information.

It calls all required upgrade functions in the correct order. In the example above, to upgrade the database from version 2 to 3, the function updateToVersion3 will be called.

By default it uses the db_version table (and creates it if needed) to store and update the current version of the database. You can change this by passing your own getCurrentVersion and updateVersion functions to the migrate function:

async function getCurrentVersion(database: Database): Promise<number> {
    // Get the current version of the database.

    return 2;
}

async function updateVersion(database: Database, version: number): Promise<void> {
    // Write the new version into the database.
}

Optionally you can pass a writeLog function to the migrate function, e.g. to print the current and updated version of the database:

function writeLog(message: string): void {
    // Log the message.
}

Types

Database

The Database type is defined as an interface:

interface Database {
    MaxVariableNumber: number;
    executeSqlCommand: (statement: string, parameters: unknown[]) => Promise<void>;
    getRows: <T>(statement: string, parameters: unknown[]) => Promise<T[]>;
}

MaxVariableNumber is the maximum number of parameters per SQL statement.