Registry / database / mysql2-orm

mysql2-orm

JSON →
library3.0.0jsnpmunverified

MySQL2 ORM v3.0.0 is a lightweight, intuitive ORM built on top of mysql2/promise, focusing on essential CRUD operations (INSERT, SELECT, UPDATE, DELETE) with automatic prepared statements and smart connection handling. It ships TypeScript types and avoids `any`. The library provides a MySQL pool wrapper and a QueryBuilder for custom queries. It is actively maintained with regular releases. Key differentiators: strictly typed (no `any`, `as`, or `satisfies`), automatic limit/offset parameterization, and transaction connection release detection. Node.js >=14.18.0 required.

npm install mysql2-orm
INSTALL
IMPORT
SIG · MYSQL2-ORM
M
mysql2-orm
databasejavascriptv3.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.

MySQL
import { MySQL } from 'mysql2-orm'
const MySQL = require('mysql2-orm')
ESM-only since v3. Named export, not default.
OP
import { OP } from 'mysql2-orm'
import OP from 'mysql2-orm'
Named export for operator helpers.
QueryBuilder
import { QueryBuilder } from 'mysql2-orm'
import { QueryBuilder } from 'mysql2-orm/build'
Direct export from package root; no subpath.

Shows creating a MySQL pool, selecting a single user by ID using OP.eq, and closing the connection.

import { MySQL, OP } from 'mysql2-orm'; const pool = new MySQL({ host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'root', database: process.env.DB_NAME ?? 'test', password: process.env.DB_PASSWORD ?? '', waitForConnections: true, connectionLimit: 10, }); async function run() { const user = await pool.select({ table: 'users', where: OP.eq('id', 16), limit: 1, }); console.log(user); await pool.end(); } run().catch(console.error);
Debug
Known issues
breakingVersion 3.0.0 drops CommonJS support; only ESM imports work.
fix
Use import syntax instead of require().
affects: >=3.0.0
breakingMySQL class constructor no longer accepts a connection string; use object config.
fix
Pass an object with host, user, database, password, etc.
affects: >=3.0.0
deprecatedOP.eq, OP.neq, etc. are deprecated in favor of OP.equal, OP.notEqual, etc.
fix
Use OP.equal('id', 16) instead of OP.eq('id', 16).
affects: >=3.0.0
gotchaselect() with limit:1 returns a RowDataPacket object or false; without limit returns an array.
fix
Check if result is an array or object before using properties.
affects: >=3.0.0
gotchaTransactions must use pool.beginTransaction() and pool.commit()/rollback(); the pool detects release automatically.
fix
Do not manually call pool.release() after transaction; it's handled.
affects: >=3.0.0
gotchaTypeScript users must install @types/node as a devDependency.
fix
Run npm i -D @types/node.
affects: >=3.0.0
Errors
Common errors & fixes
import { MySQL } from 'mysql2-orm'; ^^^^^^ SyntaxError: Cannot use import statement outside a module
Package is ESM-only, but project is configured as CommonJS.
fix
Set "type": "module" in package.json or rename file to .mjs.
const MySQL = require('mysql2-orm'); TypeError: mysql2_orm_1.MySQL is not a constructor
Using require() on an ESM-only package; exports are not available via CommonJS.
fix
Use dynamic import: const { MySQL } = await import('mysql2-orm'); or switch to ESM.
TypeError: pool.select is not a function
Missing import of MySQL or using wrong export (e.g., default import instead of named).
fix
Ensure correct import: import { MySQL } from 'mysql2-orm' and instantiate with new MySQL({...}).
Error: Cannot find module 'mysql2'
mysql2 is a peer dependency but not installed.
fix
Run npm install mysql2.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
mysql2requiredPeer dependency for database connection
Agent activity
4 hits · last 30 days
node
4
Resources
mysql2-orm — npm install mysql2-orm · libregistry