1.8.2 • Published 23 days ago

@bluealba/microservices-toolkit v1.8.2

Weekly downloads
-
License
ISC
Repository
-
Last release
23 days ago

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;
1.8.2

23 days ago

1.8.1

3 months ago

1.8.0

3 months ago

1.7.3

3 months ago

1.7.2

3 months ago

1.7.1

4 months ago

1.7.0

4 months ago

1.6.6

5 months ago

1.6.5

5 months ago

1.6.4

6 months ago

1.5.5

10 months ago

1.6.3

7 months ago

1.6.2

7 months ago

1.6.1

7 months ago

1.5.7

8 months ago

1.5.6

10 months ago

1.5.4

10 months ago

1.4.5

12 months ago

1.5.3

12 months ago

1.5.2

12 months ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.0.5-beta

3 years ago