New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

larvitdb

Package Overview
Dependencies
Maintainers
1
Versions
163
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

larvitdb

DB wrapper module for node.js

  • 0.3.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1K
increased by174.09%
Maintainers
1
Weekly downloads
 
Created
Source

larvitdb

DB pool wrapper for node.js This module is used to share a mysql/mariadb pool of connections between modules, classes, files etc.

It also logs with winston if there is a database error, so you do not need to fetch the database error manually each time.

Installation

npm i larvitdb

Usage

The module must first be required and then configured. Make this in your main application file:

var db = require('larvitdb');

db.setup({
	"connectionLimit":   10,
	"socketPath":        "/var/run/mysqld/mysqld.sock",
	"user":              "foo",
	"password":          "bar",
	"charset":           "utf8_general_ci",
	"supportBigNumbers": true,
	"database":          "my_database_name"
});

See list of native options here. Then you can just require the module in your other files for usage, like this:

A direct query

var db = require('larvitdb');

db.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
	console.log('dbmodel: The solution is: ', rows[0].solution);
});

Or, if a connection is needed:

var db = require('larvitdb');

db.pool.getConnection(function(err, dbCon) {
	var sql = 'SELECT * FROM users WHERE username LIKE ' +
	    	dbCon.escape(postData);

	dbCon.query(sql, function(err, rows) {
		dbCon.release(); // Always release your connection when the query is done

		if (err)
			throw err;

You dont need to get a connection to escape though. You can do like this:

var db = require('larvitdb');

db.query('SELECT * FROM users WHERE id = ?', [userId], function(err, results) {
  // ...
});

Advanced configuration - recoverable errors

Sometimes recoverable errors happend in the database. One such example is deadlocks in a cluster. Here we'll provide an example of how to make the database layer retry a query 5 times if a deadlock happends, before giving up.

var db = require('larvitdb');

db.setup({
	"connectionLimit":   10,
	"socketPath":        "/var/run/mysqld/mysqld.sock",
	"user":              "foo",
	"password":          "bar",
	"charset":           "utf8_general_ci",
	"supportBigNumbers": true,
	"database":          "my_database_name",
	"retries":           5, // Defaults to 3 if omitted
	"recoverableErrors": ["ER_LOCK_DEADLOCK"] // What error codes to retry
});

// If this query fails with a deadlock, it will be retried up to 5 times.
// On each retry a warning will be logged with winston
// If the 5th retry fails, an error will be logged and the callback will be called with an error
db.query('DELETE FROM tmpTable LIMIT 10');

Keywords

FAQs

Package last updated on 22 Jan 2016

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