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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@whi/knex-orm

v0.1.2

Published

An object-relational mapping library based on Knex.js.

Readme

Knex ORM

This module helps manage table relationships, complex entity structuring, and common data procedures such as pagination.

Overview

Data relationships between tables can be quite cumbersome especially to developers less experienced in database design. Although it seems difficult to manage complex relationships, there is a simple underlying recursive nature that can be managed predictable. This ORM avoids the most common issues by implementing safe, efficient, and flexible methods for handling intricate patterns.

Features

  • Simplifies table joining syntax
  • Automatically avoids conflicting column names
  • Uses an efficient flexible pagination query structure

Usage

Install

npm i @whi/knex-orm

Example Configuration

Context (sqlite3)

CREATE TABLE IF NOT EXISTS users (
       id               integer PRIMARY KEY AUTOINCREMENT,
       email            varchar NOT NULL UNIQUE,
       first_name       varchar,
       last_name        varchar,
       created          timestamp DEFAULT CURRENT_TIMESTAMP
);

Initialization

const { Table } = require('@whi/knex-orm');

const Users = new Table( "users", database, {
    "alias": "u",

    "columns": [
        "id",
        "email",
        "first_name",
        "last_name",
        "created",
    ],

    "defaults": {
        "order_by": ["u.created", "desc"],
    },

    restruct ( row ) {
        return {
            "id":               row.u_id,
            "email":            row.u_email,
            "name": {
                "first":        row.u_first_name,
                "last":         row.u_last_name,
                "full":         `${row.u_first_name} ${row.u_last_name}`,
            },
            "created":          new Date( row.u_created ),
        };
    },
});

Table Class Reference

constructor( <table name> : string, <client> : knex, <config> : object )

Initialize a new table configuration.

config

  • Required
    • .alias : string - Table alias used in query (eg. ${table name} as ${alias})
    • .columns : array - List of columns in this table
    • .restruct : function - A callback for turning a single row of data into a structure object.
  • Optional
    • .joins : function - A function that should return an array of Join Configurations.
    • .defaults : object - Set default valuess for certain optiional parameters
      • .order_by : tuple(2) - An array with 2 values used for knex.orderBy (eg. [ <column name>, asc|desc ]).
    • ... : object - Any additional methods.
      • [method] : function - a function where method does not override reserved class properties.

this.table

A special getter method that always returns a fresh knex( <table name> ) instance.

this.base( <options> = {} )

options

  • .client : knex - Knex client to be used for query construction (default to <client>).
  • .from : knex.from input - Custom input for knex.from( from ) (defaults to <table name> as <alias>).
  • .order_by : tuple(2) - Custom input for knex.orderBy( ... ) (defaults to <defaults.order_by>).

this.join( <related column name>, <foreign column name>, <type> = "left" )

Method that creates and returns a Join Configuration using given arguments.

TODO: Implement <type>

Example

{
    "table": "<table name> as <alias>",
    "base_col": "<alias>.<related column name>",
    "foreign_col": "<foreign column name>",
    "columns": Object.assign({}, <this.columns> ),
}

this.paginate( <where> : knex.where input, <options> = {} )

Automated pagination.

options

  • .page : number - Desired page data (defaults to 1)
  • .size : number - Per page size (defaults to 25)
  • .order_by : tuple(2) - Order by configuration (defaults to <defaults.order_by>).
  • .debug : boolean - Turn on SQL logging (defaults to false)

Example return value

{
    "start":            0,
    "limit":            25,
    "total":            3832,
    "remaining":        3807,
    "pages": {
        "current":      1,
        "first":        true,
        "last":         false,
        "total":        154,
        "remaining":    153,
    },
    "data" : [ ... ],
}

this.queryToDebugString( <query> : knex )

Returns the query SQL as a pretty printed string for debugging.