Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

knex-utils

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

knex-utils

knex.js utils

  • 4.1.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1K
increased by30.83%
Maintainers
1
Weekly downloads
 
Created
Source

Knex Utils

Common utilities for knex.js

npm version npm downloads

Connection init example

const config = require('config');
const { connectionUtils, heartbeatChecker } = require('knex-utils');
const HEARTBEAT_QUERY = heartbeatChecker.HEARTBEAT_QUERIES.POSTGRESQL;

const knexConfig = config.get('db'); //knex-utils directly passes config entity to knex, so see knex documentation for exact format
let _knex;

function getKnexInstance() {
	return _knex || _initKnexInstance();
}

function _initKnexInstance() {
	_knex = connectionUtils.getKnexInstance(knexConfig, connectionUtils.getRegistry(), logger);
	return _knex;
}

/**
 *
 * @returns {Object} heartbeat check result
 */
function checkHeartBeat() {
	return heartbeatChecker.checkHeartbeat(_knex, HEARTBEAT_QUERY);
}

function close() {
	if (_knex) {
		const promise = _knex.destroy();
		_knex = undefined;
		connectionUtils.getRegistry().length = 0; //ToDo implement more specific removal of connections from registry
		return promise;
	}
	return Promise.resolve();
}

module.exports = {
	getKnexInstance,
	checkHeartBeat,
	close
};

Hearbeat usage example

const { heartbeatChecker } = require('knex-utils');
const HEARTBEAT_QUERY = heartbeatChecker.HEARTBEAT_QUERIES.POSTGRESQL;
const knex = require('./db/db.service').getKnexInstance(); //replace with whatever method you use to obtain knex instance

const appPromise = swaggerService.generateSwagger(true) // could be any async startup activity, or you can start the chain with 'db.checkHeartBeat'
	.then(async () => {
		const dbCheckResult = await heartbeatChecker.checkHeartbeat(knex, HEARTBEAT_QUERY);;
		if (dbCheckResult.isOk === false) {
			console.error('DB connection error: ', dbCheckResult.error);
			throw (dbCheckResult.error);
		}
	})
	.then(async () => {
			const app = express();
			// <...> complete app initialization omitted
			return app;
		}
	).catch((e) => {
		console.error('Error while starting application: ', e);
		throw e;
	});

function getAppAsync() {
	return appPromise;
}

module.exports = {
	getAppAsync
};

Keywords

FAQs

Package last updated on 28 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc