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

bookshelf-consul-pilot

v0.1.0

Published

Create a Knex/Bookshelf connection that updates dynamically using consul-pilot.

Downloads

6

Readme

bookshelf-consul-pilot

Version Build Status Downloads

Installation

Your database connection file might look something like this:

const BookshelfConsulPilot = require('bookshelf-consul-pilot');
const knexfile = require('../../knexfile').development;
const path = require('path');

// Argument 1: The knex config that you would use to instantiate Bookshelf
// Argument 2: The Consul database service name to listen for changes in connections on
// Argument 3: The path to your Bookshelf models folder that bookshelf-consul-pilot will read models from
// Argument 4: A function for configuring Bookshelf plugins. This will called every time a new connection is reported
// Argument 5: Optionally bypass the new connection watching by passing true. Useful for dev and in some cases unit testing
module.exports = new BookshelfConsulPilot(knexfile, 'database', path.join(__dirname, '/../models'), (bookshelf) => {
    bookshelf.plugin('pagination');
    bookshelf.plugin(require('bookshelf-signals')());
});

Usage

Defining Models

Ensure that your model files are wrapped in a function that accepts bookshelf as an argument:

module.exports = (bookshelf) => {
    return bookshelf.Model.extend({
        tableName: 'books',
    });
};

Querying

// include the file created in the Installation step
const db = require('database');

// you can fetch the bookshelf and knex instances like so
// db.bookshelf
// db.knex

function getBooks() {
    // when using Bookshelf models, always fetch the model instance like so. The 'book' argument is
    // the filename of your model in the models directory you specified in the Installation step
    db.model('book').fetchPage()
        .then((books) => {
            console.log(books);
        });
}

Registering Events

Anything that must modify the Bookshelf instance or its models must be wrapped in a register. This allows bookshelf-consul-pilot to completely reset the Bookshelf instance if a new connection were to be reported. An example is events using the bookshelf-signals plugin:

const db = require('database');

db.register((bookshelf) => {

    bookshelf.on('created', db.model('book'), (model) => {
        console.log('created fired!');
    });

    bookshelf.on('updated', db.model('book'), (model) => {
        console.log('updated fired!');
    });

});