Socket
Book a DemoInstallSign in
Socket

@loke/db-kit

Package Overview
Dependencies
Maintainers
0
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@loke/db-kit

Wrapper around knex to provide some sugar such as metrics and slow query logging

2.1.3
latest
npmnpm
Version published
Weekly downloads
100
-37.11%
Maintainers
0
Weekly downloads
 
Created
Source

LOKE DB Kit

A lib for use with database libraries

const dbKit = require("@loke/db-kit");

Knex

createConfig(options)

const knex = require("knex");
const dbUri = "postgres://localhost/test-db";

const dbClient = knex(dbKit.knex.createConfig({ connection: dbUri }));

or if you're using a knexfile.js

module.exports = dbKit.knex.createConfig({
  connection: process.env.DATABASE_URL || { database: "test-link" }
});

The options are as follows

connection

Gets passed directly to knex

client

The sql client to use, defaults to "pg".

migrationsDirectory

Where to place the db migrations, defaults to "./lib/postgres/migrations".

async setup(dbClient, logger, options)

const knex = require("knex");

const dbClient = knex(/* ... */);

dbKit.knex.setup(dbClient, console);

Options are:

slowQueryThreshold

Threshold for logging slow queries in ms, defaults to 200

migrateUp

weather or not setup should migrate the db to the latest schema. Defaults to true.

registerMetrics(registry)

Regester metrics with prom-client

const { register } = require("prom-client");

dbKit.knex.registerMetrics(register);

formatConnection(connection)

Formats a connection object/string that is valid in knex(), redacts the password.

console.log("Using database", dbKit.knex.formatConnection(uri));

Best Practice

Bringing this all together you should probably have something like...

knexfile.js

const { knex } = require("@loke/db-kit");

module.exports = knex.createConfig({
  connection: process.env.DATABASE_URL || { database: "service-name" }
});

server.js

const dbKit = require("@loke/db-kit");
const lokeLogger = require("@loke/logger");
const promClient = require("prom-client");

const dbConfig = require("./knexfile");


dbKit.knex.registerMetrics(promClient.register);


function main() {
  const logger = lokeLogger.create({ metricsRegistry: promClient.register });
  
  logger.info("Using database", dbKit.knex.formatConnection(dbConfig.connection));
  const dbClient = knex(dbConfig, logger.withPrefix("db"));
  
  await dbKit.knex.setup(dbClient, dbLogger);
  
  // ...
}

FAQs

Package last updated on 12 Sep 2024

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.