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

@bluealba/microservices-toolkit

v1.9.4

Published

Library that contains common elements used in microservices

Downloads

350

Readme

Microservices Toolkit

Little library with a few "common" functionality that we use in our microservices.

Installation

npm i @bluealba/microservices-toolkit

Use Cases

Data

Pool

Pool is a wrapper on pg-pool that provides the possibility to query centralizing error handling using retries and mapping responses

Pool provides:

  • getPool: returns the pg-pool object for direct use.
  • query: queries the database with the given statement and parameters logging any possible error before rethrowing the exception.
  • recoverQuery: same as query but with retries (configured by maxQueryRetries).
  • mappedQuery: same as recoverQuery but adding a mapping parameter that is used to map each value of the result set.

Code example

In order to create a connection pool you should use the following code.

const config = ...;
const toolkit = require("@bluealba/microservices-toolkit");

const pgPool = toolkit.buildPGClient(config);

We use the config object to configure the pool expecting the following attributes.

  {
    host: config.host,
    database: config.dbName,
    user: config.user,
    password: config.password,
    port: config.port,
    max: config.maxPoolSize,
    idleTimeoutMillis: config.idleTimeout,
    connectionTimeoutMillis: config.dbTimeout,
    maxUses: config.maxUses,
  }

Also, the configuration should have the following value to specify the amount of retries

maxQueryRetries  

Error

graphqlCustomErrorFn

Is a custom error handler for graphql that allows the user to provide errors with extensions that

Configure the custom error handling using:

const toolkit = require("@bluealba/microservices-toolkit");

const graphqlCustomErrorFn = toolkit.graphqlCustomErrorFn;

app.use(
  "/graphql",
  graphqlHTTP({
    schema,
    pretty: true,
    graphiql: true,
    customFormatErrorFn: graphqlCustomErrorFn(),
  })
);

Errors should be created adding an extension field like in this example:

class StatusCodeError extends Error {

  constructor(statusCode, errorCode, message) {
    super(message);
    this.extensions = { errorCode: errorCode, statusCode: statusCode, message: message };
  }

}

Middelware

errorHandler

Simple error handler for express that catches any loose error and turns it into a well-formed JSON error with a 500 status code.


const toolkit = require("@bluealba/microservices-toolkit");

const errorHandler =  toolkit.errorHandler;
app.use(errorHandler(logger));

gatewayAuth

Filter that adds an auth object to the request using the x-forwarded-user-* headers added by the proxy this object provides:

  • username:
  • displayName:
  • operations:
  • can: method that check if the user has the given operation
  • matchesLoggedUser: checks if the parameter is the username

Code example


const toolkit = require("@bluealba/microservices-toolkit");

const gatewayAuth =  toolkit.gatewayAuth; 
app.use(gatewayAuth());

getAuthHeaders


const toolkit = require("@bluealba/microservices-toolkit");
const getAuthHeaders = toolkit.getAuthHeaders;

const authHeaders = getAuthHeaders(req);

Performance

run

wraps any give call with a method that logs the time the method takes to run when called


const toolkit = require("@bluealba/microservices-toolkit");
const run = toolkit.run;

module.exports = run(getSegments, "getSegments");

SQL

Util class that provides methods to generate sql parts.

  • formatDate,
  • escapeQuotes,
  • wrapInQuotes,
  • wrapInDoublesQuotes,
  • generateAnyClause,
  • generateInClause,

Code example


const toolkit = require("@bluealba/microservices-toolkit");
const { generateAnyClause } = toolkit.sqlUtils;