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

dbwalker

v0.0.56

Published

Library to walking on database.

Downloads

87

Readme

A Library to walk the database using serverless-mysql

The most flexible MySQL query builder, using serverless-mysql to run and retrieve data.

Downloads Version License Rate this package

You can use objects, arrays and strings to build your queries using easy-to-understand methods.

IMPORTANT: this library is under development and stil in alpha. It's a very useful, but I don't recommend using it without a supervision.

Connection

You can connect to the database in the following ways:

// using a connection string
const db = new DBWalker("mysql://user:pass@host:port/base");
// parsing inline connection data
const db = new DBWalker({ host, ?port, user, pass, base });
// using array of connection data
const db = new DBWalker([host, ?port, user, pass, base]); // my fav to testing
// getting from ".env" file
const db = new DBWalker();

.env variables:

DBWALKER_HOST=
DBWALKER_PORT=
DBWALKER_USER=
DBWALKER_PASS=
DBWALKER_BASE=

Or .env variable string:

DBWALKER_STRING="mysql://user:pass@host:port/base"

Usage

Raw SQL example:

const raw = db.query("SELECT `alias`.* FROM `database`.`table` AS `alias` WHERE `alias`.`param` = ? ORDER BY ?", ["value", "field"]);

You can use select(), insert(), update(), delete() methods to build your queries.

const select = await db.select({
    table: "table", 
    ?columns: [
        "table_alias.field_name AS field_alias",
    ],
    ?fields: {
        field_alias: "table.field_name",
        field_name: "field_name",
        function_return: "(SELECT GROUP_CONCAT(value) FROM table_other WHERE table_other.field = table.field GROUP BY table_other.field)",
        other_function_return: "(SELECT other_table.field FROM table_other AS other_table WHERE other_table.field = table.field ORDER BY other_table.field LIMIT 1)",
    }, 
    ?joins, 
    ?where: [
        "field_name > 8.9", // field_name > 8.9
        { "field_name": "value" }, // field_name = value
        { "field_name": true }, // field_name = 1
        { "field_name": 88.9 }, // field_name = 88.9
        { is_null: "field_name" }, // field_name IS NULL
        { not_null: "field_name" }, // field_name IS NOT NULL
        { is_empty: "field_name" }, // field_name = ''
        { not_empty: "field_name" }, // field_name != ''
        { field: "field_name", between: ["param", "param"] }, // field_name BETWEEN param AND param
        { field: "field_name", not_between: ["param", "param"] }, // field_name BETWEEN param AND param
        { field: "field_name", in: ["options_array"] }, // field_name IN (options_array)
        { field: "field_name", not_in: ["options_array"] }, // field_name NOT IN (options_array)
        { field: "field_name", like: "value" }, // field_name LIKE '%value%'
        { field: "field_name", not_like: "value" }, // field_name NOT LIKE '%value%'
        { field: "field_name", start_with: "value" }, // field_name LIKE 'value%'
        { field: "field_name", end_with: "value" }, // field_name LIKE '%value'
        { field: "field_name", find_in_set: "value" }, // FIND_IN_SET(value, field_name)'
    ],
    ?group_by: ["fields"], 
    ?order_by: ["fields"], 
    ?limit: int, 
    ?offset: int
});

const insert = await db.insert({
    table: "table", 
    data: {
        field_name: "value"
    }
});

const update = await db.update({
    table: "table", 
    ?joins, 
    data: {
        field_name: "value"
    }, 
    where: [(...)]
});

const delete = await db.delete({
    table: "table", 
    ?joins,
    where: [(...)]
});

Tou can use .toString() method to return a MySQL string:

const select = db.select({
    table: "database.table AS table_alias", 
    columns: [
        "table_alias.column_name", 
        "table_alias.column_name AS column_alias"
    ]
}).toString();
SELECT table_alias.column_name, table_alias.column_name AS column_alias FROM `database`.`table` AS `table_alias`

Thanks to sql-formatter, we can return a pretty sql string using the .format() method as following:

const select = db.select({
    table: "database.table AS table_alias", 
    fields: { 
        field_alias: "table_alias.real_field_name"
    }
}).format();
SELECT
    table_alias.real_columnd_name AS `field_alias`
FROM 
    `database`.`table` AS `table_alias`
WHERE (...)

Using .run() returns a <Promise> with result or throw an error catchable.

Examples of use:

const results = await dbwalker.select(select).run();
// { success: bool, rows: int, data: array }

dbwalker.insert(insert).run() // { success: bool, insert_id: int, affected_rows: int }
    .then(res => {
    if(res.success) return dbwalker.select({
        table_name, 
        where: [
            { id: res.insert_id}
        ]
    });
});

dbwalker.update(update).run()// { success: bool, insert_id: int, affected_rows: int }
    .then(res => console.log(`${res.affected_rows} changed`))
    .catch(err => console.loc(err));

dbwalker.delete(update).run().catch(err => console.loc(err)).finaly(dbwalker.quit());