Registry / database / sql-mocha

sql-mocha

JSON →
library1.0.0jsnpmunverified

A Node.js library for managing Microsoft SQL Server databases with built-in mocking capabilities for testing. Version 1.0.0 provides an easy-to-use API for executing stored procedures, queries, and transactions, with special support for mocking procedure returns via JSON files for unit tests. It includes an Express middleware for request-scoped SQL contexts and supports a variety of SQL Server types. The library is primarily used in development and testing environments where database mocking is needed.

npm install sql-mocha
INSTALL
IMPORT
SIG · SQL-MOCHA
S
sql-mocha
databasejavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Sql
const { Sql } = require('sql-mocha');
const Sql = require('sql-mocha');
Default is not exported; must use destructuring.
Context
const { Context } = require('sql-mocha');
const Context = require('sql-mocha').Context;
Destructuring is preferred for clarity.
sql.execute
const result = await sql.execute('proc', { param: value });
const result = sql.execute('proc', { param: value });
The method is async and must be awaited.

Shows how to initialize a Sql instance with environment variables and execute a stored procedure.

const { Sql } = require('sql-mocha'); const sql = new Sql({ server: process.env.DB_SERVER ?? 'localhost', database: process.env.DB_NAME ?? 'test', user: process.env.DB_USER ?? 'sa', password: process.env.DB_PASSWORD ?? '', connectTimeout: 60000, requestTimeout: 60000, pool: { max: 5, min: 1, idleTimeoutMillis: 30000 } }); async function main() { try { const result = await sql.execute('GetUsers', { active: true }); console.log(result.content); // array of rows console.log(result.returnValue); // status code } catch (err) { console.error('Database error:', err); } finally { await sql.close(); } } main();
Debug
Known issues
gotchaMocking requires process.env.DATABASE_MOCHA = true and a mocha.json file in the same directory as the calling code.
fix
Set environment variable and create a valid mocha.json.
affects: >=1.0.0
gotchaThe `execute` method expects an object with `input` and maybe `$output`; other keys may be ignored.
fix
Ensure parameters are passed as `{ input: value, $output: type }`.
affects: >=1.0.0
gotchaContext middleware must be applied before routes; otherwise, req.sql will be undefined.
fix
Call Context.middleware(app, config) before defining routes.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: sql.execute is not a function
Forgot to `await` the async method or misconfigured Sql instance.
fix
Ensure you `await sql.execute(...)` and that `sql` is a valid Sql instance.
Error: Code 1 - Arquivo mocha não encontrado
DATABASE_MOCHA is true but mocha.json is missing in the expected directory.
fix
Create a mocha.json file in the same directory as the file calling sql.execute.
RequestError: Invalid object name 'table'.
Table or view does not exist in the database, or database not set.
fix
Check database name in config and that table exists.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
mssqlrequiredUnderlying driver for SQL Server connectivity
expressoptionalRequired for Context middleware integration
Agent activity
14 hits · last 30 days
node
10
Meta
2
OpenAI (training)
1
Resources
sql-mocha — npm install sql-mocha · libregistry