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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@dvsa/database-helpers

v3.0.0

Published

Common package to be used for database operations

Readme

database-operations

Database wrapper to help with connection creation and use of MyBatis, published as a GitHub package.

Pre-requisites

  • Node.js (Please see .nvmrc for specific version)
  • npm (If using n or nvm, this will be automatically managed)
  • Security
    • Git secrets
    • ScanRepo
      • Unzip repo-security-scanner_<version>_Darwin_<architercture>.tar.gz and rename the executable inside the folder to scanrepo - Add executable to path (using echo $PATH to find your path)

Getting started

Run the following command after cloning the project
  1. npm install (or npm i)
The code that will be published lives inside the ./src directory.

If wishing to add new top level directories to the output, then they must be included in the files array inside package.json as well as included in the clean:temp command.

Publishing

In order to see the output of what will be published, run the following command:

npm publish --dry-run

There are two ways in which this package can/should be published:

Requires manual version bump via the PR
  • Upon merge into main branch, the package will be published via a GHA workflow.

Contents

MyBatisSession

Overview

MyBatisSession is a utility class that integrates MyBatis mapping with MySQL, allowing for structured and efficient query execution. It supports query execution, result transformation, and silent error handling while providing debugging capabilities.

Usage

Importing the MyBatisSession Class

import { MyBatisSession } from "@dvsa/database-helpers";
import { createConnection } from "mysql2/promise";

Initializing a MyBatis Session

To use MyBatis, you need a MySQL connection, a namespace, and mapper files.

async function initializeSession() {
  const connection = await createConnection({
    host: "localhost",
    user: "root",
    password: "password",
    database: "test_db",
  });

  const session = new MyBatisSession(connection, "UserNamespace", ["./mappers/user.xml"], true);
  return session;
}

Executing a Query

async function fetchUsers() {
  const session = await initializeSession();
  const users = await session.query("getUsers", {});
  console.log("Users:", users);
  await session.end();
}

fetchUsers();

Parameters

#{name} values, including <foreach> items and paths such as #{item.id} or #{list.length}, are no longer escaped into the SQL by mybatis-mapper, whose escaping could be broken out of. <if> and <when> tests still see the real values, and the parameters object is used as before (getters, class instances and <foreach>/<bind> writes all behave the same).

  • How values reach the database
    • MySQL query/selectOne/selectList: each value is escaped by mysql2 itself (connection.escape, the same escaping its client-side ? placeholders use) and the statement is sent as text. This relies on the default sql_mode, without NO_BACKSLASH_ESCAPES. Reads are kept off server-side prepared statements because mysql2 caches those per connection.
    • MySQL execute: server-side prepared statement with ? placeholders.
    • Oracle: positional :1, :2… binds.
  • ${name} is still substituted into the SQL as-is. Only use it for trusted values you have validated yourself, such as an integer LIMIT.
  • Value types:
    • strings are passed as they are;
    • numbers and booleans are passed as the same text as before ('25', 'true'), so a number compared with a VARCHAR column still uses its index;
    • Date and Buffer values are passed to the driver as they are;
    • objects and arrays are passed as JSON;
    • null becomes NULL.
  • Escaping changes:
    • a % or _ in a value is no longer escaped, so it is a wildcard if the value is used in a LIKE pattern. Use escapeLikePattern(value) (exported alongside MyBatisSession) to match it literally, e.g. LIKE CONCAT('%', #{name}, '%') with name: escapeLikePattern(input);
    • double quotes are no longer altered.
  • Placeholder limits: where placeholders are used (MySQL execute, Oracle), a #{} must sit where the database accepts a bind value. Forms like IS #{v} or DATE #{d} only work on MySQL reads.
  • Namespace copy: the session renders from a copy of its namespace, registered as <namespace>::bound and kept in step with the original, so calling MyBatis.getStatement directly behaves as before.
  • Dependency: mybatis-mapper is pinned to an exact version because this relies on its internals.

Selecting a Single Record and Mapping to a Model

class User {
  id!: number;
  name!: string;
}

async function getUserById(id: number) {
  const session = await initializeSession();
  const user = await session.selectOne("getUserById", { id }, User);
  console.log("User:", user);
  await session.end();
}

getUserById(1);

Selecting Multiple Records and Mapping to a Model

async function getAllUsers() {
  const session = await initializeSession();
  const users = await session.selectList("getAllUsers", {}, User);
  console.log("Users:", users);
  await session.end();
}

getAllUsers();

Handling Errors Silently

If you want to execute a query and return an empty array on failure, use selectAndCatchSilently.

async function getUsersSafely() {
  const session = await initializeSession();
  const users = await session.selectAndCatchSilently("getAllUsers", {}, User);
  console.log("Users:", users);
  await session.end();
}

getUsersSafely();

Closing the Connection

async function closeSession() {
  const session = await initializeSession();
  await session.end();
}

Debugging Mode

If debugMode is enabled (true), queries will be logged before execution.

*** Query for namespace: UserNamespace & mapperID: getUsers ***
SELECT * FROM users;
***

Error Handling

Errors will be thrown if:

  • A query fails due to an invalid SQL statement or missing parameters.
  • A MyBatis mapper file is incorrect.
  • The MySQL connection is unavailable.

Using selectAndCatchSilently, errors will be logged but return an empty array.

try {
  const result = await session.query("invalidQuery", {});
} catch (error) {
  console.error("Error executing query:", error);
}