Registry / database / mybatis-mapper

mybatis-mapper

JSON →
library0.8.0jsnpmunverified

mybatis-mapper generates SQL statements from MyBatis3 Mapper XML files in Node.js. Version 0.8.0 supports all MyBatis dynamic SQL elements including <if>, <choose>, <where>, <foreach>, <bind>, and <include>. It parses mapper XML files with namespaces and provides methods to build parameterized SQL queries using #{...} and ${...} syntax. Different from ORM-based libraries, it focuses on reusing existing MyBatis XML mappings in Node.js environments. The library integrates with sql-formatter for output formatting. Compatible with MySQL clients like mysql2.

npm install mybatis-mapper
INSTALL
IMPORT
SIG · MYBATIS-MAPPER
M
mybatis-mapper
databasejavascriptv0.8.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.

default
✓ const mybatisMapper = require('mybatis-mapper')
✗ const mybatisMapper = require('mybatis-mapper').default
CommonJS default import; no named exports or ES module wrapper.
createMapper
✓ mybatisMapper.createMapper(['./mapper.xml'])
✗ mybatisMapper.createMapper('./mapper.xml')
Requires an array of file paths, even for a single file.
getStatement
✓ mybatisMapper.getStatement('namespace', 'sqlId', params, format)
✗ mybatisMapper.getStatement('namespace.sqlId', params, format)
Namespace and SQL ID are separate arguments, not dot-joined.

Shows loading a MyBatis XML mapper, building a SQL statement with parameters, and executing it via mysql2/promise.

const mybatisMapper = require('mybatis-mapper'); const mysql = require('mysql2/promise'); async function run() { const connection = await mysql.createConnection({ host: 'localhost', user: 'root', database: 'test', password: process.env.DB_PASSWORD ?? '' }); mybatisMapper.createMapper(['./fruits.xml']); const params = { category: 'apple', price: 100 }; const format = { language: 'sql', indent: ' ' }; const query = mybatisMapper.getStatement('fruit', 'testBasic', params, format); const [rows] = await connection.query(query); console.log(rows); await connection.end(); } run().catch(console.error);
Debug
Known issues
breakingArray of XML files required for createMapper; passing a single string fails silently.
fix
Wrap the file path in an array: ['file.xml'].
affects: >=0.0.1
dangerUses ${} for direct string substitution; vulnerable to SQL injection if user input is included without sanitization.
fix
Prefer #{} for parameter binding. Use ${} only with trusted values.
affects: >=0.0.1
gotchaNo support for ES modules; commonjs only. Cannot import via import statement.
fix
Use require() or await import('mybatis-mapper') with dynamic import.
affects: >=0.0.1
gotchaFormat parameter object must include 'language' and 'indent' properties; omit or misspelling causes error.
fix
Provide { language: 'sql', indent: '  ' } at minimum.
affects: >=0.0.1
gotchaNamespace and SQL ID are separate arguments; using a dot-joined string fails.
fix
Call getStatement('namespace', 'sqlId', ...) not getStatement('namespace.sqlId', ...).
affects: >=0.0.1
Errors
Common errors & fixes
Cannot find module 'mybatis-mapper'
Package not installed or installed as devDependency.
fix
Run 'npm install mybatis-mapper --save'.
createMapper is not a function
Incorrect import (e.g., using default export when not available).
fix
Use 'const mybatisMapper = require('mybatis-mapper')' then call 'mybatisMapper.createMapper'.
TypeError: format is undefined
Missing or undefined format object passed to getStatement.
fix
Provide a format object with 'language' and 'indent' properties, e.g., { language: 'sql', indent: '  ' }.
Mapper not found: 'namespace.id'
getStatement called with a dot-joined string instead of separate namespace and sqlId arguments.
fix
Call getStatement('namespace', 'sqlId', ...) with two separate string arguments.
Upgrade
Version history
0.8.0latest on npm
Audit
Dependencies
sql-formatteroptionalUsed to format generated SQL output with configurable language and indent.
Agent activity
12 hits · last 30 days
node
10
Resources
mybatis-mapper — npm install mybatis-mapper · libregistry