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

@dialexa/pleco-knex

v0.4.0

Published

# Pleco Knex

Downloads

44

Readme

Pleco logo

Pleco Knex

Table of Contents

Overview

pleco-knex provides all the same exports as pleco, but overrides the functions for ease of use.

Function Usage

getFilterQuery

import { getFilterQuery } from '@dialexa/pleco-knex';

// Suppose vehicles has the columns id, make, model, highwayMPG, cityMPG
// Note that you do not have to create subqueries for columns that exist on the table already

// Create our subqueries
const numberOfUsers = knex
  .select('vehicles.id as resource_id', 'count(*) as value', 'count(*) as sort')
  .from('vehicles')
  .leftJoin('vehicles_users', 'vehicles_users.vehicle_id', 'vehicles.id') // left join so we don't lose vehicles that don't have users
  .groupBy('vehicles');

... // Subqueries for the other filter fields

const subqueries = {
  numberOfUsers,
  ...
};

const filter = {
  AND: [
    { make: { eq: 'nissan' } },
    { model: { in: ['altima', 'sentra'] } },
    { numberOfUsers: { AND: [{ gt: 1000 }, { lt: 1999 }] } },
    {
      OR: [
        { highwayMPG: { gt: 30 } },
        { cityMPG: { gte: 20 } }
      ]
    },
    { userSurveyRating: { gte: 80.5 } }
  ]
};

let query = knex('vehicles').where(builder =>
  // mutate tells us to edit the builder object passed by reference instead of cloning
  getFilterQuery({ filter, subqueries }, { knex, query: builder, mutate: true });
);

Additionally, you can denote filter as

const filter = { // implicit AND
  make: 'nissan', // implicit eq
  model: ['atlima', 'sentra'], // implicit in
  numberOfUsers: { gt: 1000, lt: 1999 },
  OR: [
    { highwayMPG: { gt: 30 } },
    { cityMPG: { gte: 20 } }
  ],
  userSurveyRating: { gte: 80.5 }
}

getSortQuery

Continuing from the code snippet for the filter function. Note that due to the way that the sort query is generated, passing mutate: true will not mutate the original query.

import { getSortQuery } from '@dialexa/pleco-knex';

const sort = { userSurveyRating: 'ASC' };

query = getSortQuery({ sort, subqueries }, { knex, query });

getPageLimitOffsetQuery

import { getPageLimitOffsetQuery  } from '@dialexa/pleco-knex';

let query = knex('vehicles');
// Page 3 with page sizes as 25
const page = { limit: 25, offset: 50 };

query = getPageLimitOffsetQuery(page, { knex, query });

Notes on Subqueries

Due to how flexible the library is for filtering arbitrary data, the generated SQL can be quite large. If just filtering on columns on the table, it is recommended to not include a subquery for the column. If not subquery is found for a filter key, the library will assume the filter key is a column on the table. For example:

// vehicles has make and model columns
import { getFilterQuery } from '@dialexa/pleco-knex';

const filter = {
  make: 'nissan',
  model: 'altima',
};

let query = knex('vehicles').where(builder =>
  getFilterQuery({ filter }, { knex, query: builder, mutate: true });