Registry / database / node-mysql-nesting

node-mysql-nesting

JSON →
library0.0.3jsnpmunverified

A utility that converts flat MySQL join query results into nested JavaScript objects. Version 0.0.3 (latest as of 2015) is stable with no recent updates. It works with the mysql npm package by requiring nestTables: true and providing a nesting options array to define table relationships. Differentiators: lightweight, explicit foreign key mapping, and simple API. No TypeScript types, ESM support, or active maintenance.

npm install node-mysql-nesting
INSTALL
IMPORT
SIG · NODE-MYSQL-NESTING
N
node-mysql-nesting
databasejavascriptv0.0.3
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.

convertToNested
import { convertToNested } from 'node-mysql-nesting'
const convertToNested = require('node-mysql-nesting');
Package is CJS-only; no ESM export. Use require() for CommonJS or dynamic import(). Default export is an object with convertToNested.
default
const func = require('node-mysql-nesting'); func.convertToNested(...)
Package exports a single object with convertToNested method. No named export for default.
func
const func = require('node-mysql-nesting');
import func from 'node-mysql-nesting';
ESM import may fail due to lack of default export; use require().

Shows how to use node-mysql-nesting with mysql package: query with nestTables:true, define nesting options, and convert flat rows to nested objects.

const mysql = require('mysql'); const func = require('node-mysql-nesting'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: process.env.MYSQL_PASSWORD || '', database: 'mydb' }); const sql = `SELECT * FROM courses LEFT JOIN departments ON departments.id = courses.department_id LEFT JOIN course_sections ON course_sections.course_id = courses.id`; const options = { sql, nestTables: true }; connection.query(options, function(err, rows) { if (err) throw err; const nestingOptions = [ { tableName: 'courses', pkey: 'id', fkeys: [{ table: 'departments', col: 'department_id' }] }, { tableName: 'departments', pkey: 'id' }, { tableName: 'course_sections', pkey: 'id', fkeys: [{ table: 'courses', col: 'course_id' }] } ]; const nestedRows = func.convertToNested(rows, nestingOptions); console.log(JSON.stringify(nestedRows, null, 2)); connection.end(); });
Debug
Known issues
gotchaThe nestingOptions table names must be in the exact order as they appear in the SELECT statement, otherwise nesting fails silently.
fix
List nestingOptions in the same order as tables in the FROM and JOIN clauses.
affects: >=0.0.0
gotchaRequires the deprecated 'mysql' package which is no longer maintained; prefer 'mysql2' with promise support.
fix
Use mysql2 and adapt the callback pattern or use promise-based wrappers.
affects: >=0.0.0
deprecatedPackage has not been updated since 2015; no TypeScript types, ESM, or modern API.
fix
Consider alternatives like 'mysql2' with 'nestTables' built-in or manual mapping.
affects: 0.0.3
breakingnestTables: true must be set in query options; without it, rows have flat column names and nesting will produce incorrect results.
fix
Always include nestTables: true in the mysql query options object.
affects: >=0.0.0
gotchaForeign keys in fkeys must reference tables defined earlier in the nestingOptions array; circular dependencies are not handled.
fix
Order tables so that parent tables appear before child tables.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: func.convertToNested is not a function
Incorrect import; package exports an object with convertToNested, not a function directly.
fix
Use const func = require('node-mysql-nesting'); then func.convertToNested(rows, options);
Cannot read property 'id' of undefined
nestingOptions tables are out of order or missing a table that appears in the SQL result.
fix
Ensure nestingOptions entries match the exact order of tables in the SELECT statement and all tables are included.
Error: ER_BAD_FIELD_ERROR: Unknown column 'department_id' in 'field list'
Foreign key column name misspelled or missing in the schema.
fix
Check the actual column names in your database and update fkeys accordingly.
Upgrade
Version history
0.0.3latest on npm
Audit
Dependencies
mysqlrequiredRequired for querying MySQL – the nesting function processes rows from mysql package queries.
Agent activity
2 hits · last 30 days
node
2
Resources
node-mysql-nesting — npm install node-mysql-nesting · libregistry