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

@earnest-labs/microservice-chassis-knex

v0.2.71

Published

A microservice-chassis plugin that provide knex connections

Downloads

2,342

Readme

microservice-chassis-knex About

A plugin for microservice-chassis that provides knex database connections

During startup, microservice-chassis-knex will scan your environment variables and connect to databases specified there accordingly.

Getting Started

Database connections are specified as environment variables. The name of each variable takes this form: KNEX[_{databaseName}]_{setting}. databaseName may contain underscores. If it is not present, the name default is used. setting may be one of JSON, PGUSER, PGPASSWORD, PGDATABASE, PGPORT, or PGHOST. If JSON is specified, it is used to construct a Knex.Config object. If not, a blank Knex.Config is created. Any of the PG* settings will modify this object with settings appropriate to a Postgresql database connection.

Sidebar: It is possible that additional database types will be directly supported as environment variable setting entries in the future. The current plan is that those will be of the form DBTYPE{SETTING}. For example, there might be SQLITE3FILENAME. In the event we go down that road, we will add checks to make sure that you don't mix PG* settings with other database-specific settings for the same databaseName.

$ npm i --save @earnest-labs/microservice-chassis
$ npm i --save @earnest-labs/microservice-chassis-knex

$ KNEX_JSON='{"client": "sqlite3", "connection": {"filename": ":memory:"}, "useNullAsDefault": true}' \
    npx chassis-start
# ^^^ connects to a new in-memory sqlite3 database, accessible as "default"

Ctrl+C

$ KNEX_DB1_PGUSER=theuser \
  KNEX_DB1_PGPASSWORD=thepassword \
  KNEX_DB1_PGDATABASE=thedatabase \
  KNEX_DB1_PGHOST=thehost.dns.record.com \
    npx chassis-start
# ^^^ connects to the specified postgresql database, accessible as "DB1"

Ctrl+C

From other microservice-chassis plugins, you may want to grab a connection and make calls:

import {Plugin} from '@earnest-labs/microservice-chassis/Plugin.js'
import {PluginContext} from '@earnest-labs/microservice-chassis/PluginContext.js'
import {KnexPlugin} from '@earnest-labs/microservice-chassis/knex.chassis-plugin.js'

export const handler = (connection: knex.Knex) => async (request, response) => {
  await connection.raw( /* some SQL stuff here */ )
  /// ...
}

export const plugin: Plugin = {
  name: 'knex-consumer-plugin',
  version: '1.0.0',
  register: async(context: PluginContext) : Promise<void> {
    const knex = context.plugins.get("knex") as KnexPlugin;
    const connection = knex.connections.get("default");
    context.application.get("/route", handler(connection))
  }
}