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

knex-hooks

v0.0.9

Published

Knex before/after insert/update/select/delete hooks

Downloads

996

Readme

knex-hooks

Build Status

Allows to easily add before/after insert/update/delete/select hooks to knex.

Install

npm install --save knex-hooks

Usage

const knexHooks = require('knex-hooks');

const knex = require('knex')({
  client: 'pg',
  connection: 'postgres://localhost/db',
});

// init knex-hooks on knex instance
knexHooks(knex);

// add hook
knex.addHook('before', 'insert', 'users', (when, method, table, params) => {

  // use helper to extract insert data from query builder
  const data = knexHooks.helpers.getInsertData(params.query);

  // check if it's single row or multi row insert
  const rows = Array.isArray(data) ? data : [data];

  // modify insert data for each row
  rows.forEach(row => {
    row.created_at = new Date();
  });
});

knex('users').insert({ name: 'john' }).then(...);

Adding hooks

knex.addHook(when, method, table, function callback (when, method, table, params) {
  // if it's `before` hook you can use params.query to modify query
  // if it's `after` hook you can use params.result to modify query result by either modifying it or assigning your custom response to params.result
  // you can return promise here
});

Adds new hook to knex instance

| argument | type | possible values / description / examples |----------|----------------------|----------------------- | when | string/array | "before", "after", "*", ["before", "after"] | method | string/array | "insert", "update", "delete", "select", "*", ["insert", "update", ...] | table | string/array | "my_table", "*", ["my_table1", "my_table2", ...]

Callback arguments

| argument | type | possible values / description / examples |---------------|--------------|----------------------- | when | string | "before", "after" | method | string | "insert", "update", "delete", "select" | table | string | "my_table" | params | object | | params.query | knex Builder | knex query builder instance (ie. knex('table').insert('*')) | params.result | query result | query result (only for after hooks) - might be modified/replaced by previous hooks

Modifying query in 'before' hooks

const helpers = require('knex-hooks').helpers;

// before insert
knex.addHook('before', 'insert', 'users', (when, method, table, params) => {
  const insertData = helpers.getInsertData(params.query);
  insertData['created_at'] = insertData['updated_at'] = new Date();
});
knex('users').insert({ name: 'john' }).then(...);

// before update
knex.addHook('before', 'update', 'users', (when, method, table, params) => {
  const updateData = helpers.getUpdateData(params.query);
  updateData['updated_at'] = new Date();
});
knex('users').update({ name: 'john' }).then(...);

// before select
knex.addHook('before', 'select', 'users', (when, method, table, params) => {
  params.query.where({ is_deleted: false });
});
knex('users').select('*').then(...);

Modifying query result in 'after' hooks

knex.addHook('after', 'select', 'users', (when, method, table, params) => {
  params.result.forEach(row => {
    row['full_name'] = row['first_name'] + ' ' + row['last_name'];
  })
});
knex('users').select('*').then(...);

Disabling hooks

You can disable ALL hooks on individual query by calling .hooks(false) method on knex builder:

knex('users').insert({ name: 'john' }).hooks(false).then(...); // hooks won't run for this query

Helpers

getInsertData / getUpdateData

These helpers allows to easily get inert/update data from knex builder

const helpers = require('knex-hooks').helpers;

helpers.getInsertData( knex('users').insert({ name: 'john' }) ); // returns { name: 'john' }
helpers.getUpdateData( knex('users').update({ name: 'john' }) ); // returns { name: 'john' }

extendKnex

Allows to modify knex instance (even sub-instances created by transations)

const helpers = require('knex-hooks').helpers;

helpers.extendKnex(knex, function callback (knex, isRoot) {
  // knex is instance you passed as first argument (isRoot = true) or transaction-specific instance (isRoot = false)
  // you can modify knex/knex.client here
});

extendBuilder

Allows to modify knex builder (even sub-instances created by transations)

const helpers = require('knex-hooks').helpers;

helpers.extendBuilder(knex, function callback (builder, isRoot) {
  // builder is knex query builder instance
  // you can modify builder here (ie. add some methods/properties to it)
  // for example builder.myCustomMethod = function () { this._myCustomFlag = true; };
});