Registry / database / sand-mysql

sand-mysql

JSON →
library2.2.6jsnpmunverified

MySQL wrapper for Sand.js applications providing convenience functions like autoloading config, automatic connection cleanup, and promise-based querying. Current version 2.2.6 (stable, no recent updates). Wraps node-mysql with built-in query, selectOne, insert, and update builders. Automatically attaches to sand.ctx.mysql when sand-http is loaded, managing connection lifecycle per request. Config via config/mysql.js file. Key differentiator: tight Sand.js integration with automatic context injection; no direct MySQL driver usage—user relies on sand infrastructure.

npm install sand-mysql
INSTALL
IMPORT
SIG · SAND-MYSQL
S
sand-mysql
databasejavascriptv2.2.6
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.

sand.mysql
// No explicit import needed; grain is auto-loaded when sand-mysql is installed and required by sand
const mysql = require('sand-mysql') // Not the top-level grain object
sand.mysql is the global grain reference, not the mysql npm package. Accessible after sand loads the grain.
Connection
const conn = sand.mysql.createConnection(config);
const conn = require('sand-mysql/lib/Connection.js') // Internal class, not exported
Create connection instances via the grain method, not by requiring internal files.
sand.ctx.mysql
sand.ctx.mysql.query('SELECT * FROM users', []).then(rows => ...);
const mysql = require('mysql'); mysql.createConnection(...).query(...) // Not using sand's context or config
In a Sand.js HTTP request, sand.ctx.mysql is auto-populated. Use it instead of manual connection management.

Shows how to run queries using the auto-injected sand.ctx.mysql connection in a Sand.js app, including query and selectOne methods.

// Ensure sand-http and sand-mysql are installed and loaded. // sand.ctx.mysql is auto-initialized per request. const promise = sand.ctx.mysql.query('SELECT * FROM users WHERE age > ?', [18]); promise.then(rows => { console.log(rows); // Array of row objects }).catch(err => { console.error(err); }); // Single row sand.ctx.mysql.selectOne('SELECT * FROM users ORDER BY id LIMIT 1', []) .then(row => { console.log(row); // Single object or null });
Debug
Known issues
gotchanode-mysql does not use real prepared statements; it emulates them by escaping values on the client side. This may expose to SQL injection if manual escaping is bypassed.
fix
Always use the bindings array for user input; never concatenate values directly into SQL strings.
affects: >=0.0.0
gotchaIf sand-http is not loaded, sand.ctx.mysql will not be auto-initialized and will be undefined.
fix
Ensure sand-http is included in your package.json and loaded before using sand.ctx.mysql, or manually create a connection via sand.mysql.createConnection().
affects: >=1.0.0
gotchaThe config/mysql.js file is required; if not present, the grain will fail to initialize or throw errors.
fix
Create a config/mysql.js file with valid connection options (host, user, password, database).
affects: >=0.0.0
gotchaThe modifyRows option is only applied to query() results, not to insert() or update().
fix
If you need result transformation for all operations, implement separate hooks per method or use the query() method exclusively.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'query' of undefined
sand.ctx.mysql is undefined because sand-http is not loaded or the grain was not properly initialized.
fix
Install sand-http and ensure it is required before using sand.ctx.mysql, or create a custom connection with sand.mysql.createConnection().
Error: getaddrinfo ENOTFOUND hostname
The MySQL host specified in config/mysql.js is unreachable or incorrect.
fix
Check the host property in config/mysql.js; ensure the MySQL server is running and accessible from your application.
Error: Cannot enqueue Query after invokeClose
Attempting to use a connection after it has been closed (e.g., after request end).
fix
Do not reuse connections across requests; use sand.ctx.mysql which is scoped per request and cleaned up automatically.
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host'
Invalid MySQL credentials in config/mysql.js.
fix
Verify the user, password, and host in config/mysql.js. Check that the user has access from the specified host.
Upgrade
Version history
2.2.6latest on npm
Audit
Dependencies
mysqlrequirednode-mysql is the underlying MySQL driver used for all queries and connections.
Agent activity
15 hits · last 30 days
node
12
Meta
2
Amazon
1
Resources
sand-mysql — npm install sand-mysql · libregistry