Registry / database / mysql-easy-type

mysql-easy-type

JSON →
library1.23.2jsnpmunverified

MySQL Easy Type is an ORM-like library for Node.js that provides a simple, typed interface for MySQL querying with automatic TypeScript declaration generation. Version 1.23.2 is current, with stable release cadence. It wraps mysql2 to provide model-based CRUD operations, join support, transactions, and a CLI tool to generate type definitions and model files from database schema. Key differentiators include its zero-config setup, automatic type generation from the database, and a concise API with chaining methods like find, limit, join, and get. It is ideal for developers who want strong typing without writing manual schemas, and it supports both connection and pool queries.

npm install mysql-easy-type
INSTALL
IMPORT
SIG · MYSQL-EASY-TYPE
M
mysql-easy-type
databasejavascriptv1.23.2
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.

model
const { model } = require('mysql-easy-type');
import { model } from 'mysql-easy-type'; // Cannot use ESM imports directly as the package is CJS only
The package is CommonJS only; try to import with ESM may fail unless using a bundler or dynamic import.
Query
const { Query, PoolQuery } = require('mysql-easy-type');
const Query = require('mysql-easy-type').query; // Case-sensitive; must be capitalized 'Query'
Query is for single connections, PoolQuery for pool connections. Both are named exports.
PoolQuery
const { PoolQuery } = require('mysql-easy-type');
PoolQuery is a named export; used with mysql2 pool instances.

Shows basic CRUD operations: creating a user, finding by primary key, updating, querying with filter, and counting records.

const mysql = require('mysql2'); const { model, Query } = require('mysql-easy-type'); const connection = mysql.createConnection({ host: process.env.MYSQL_HOST ?? 'localhost', user: process.env.MYSQL_USER ?? 'root', database: process.env.MYSQL_DATABASE ?? 'test', password: process.env.MYSQL_PASSWORD ?? '' }); const query = new Query(connection); const User = model('user'); async function main() { // Create a user const id = await User(query).create({ name: 'John', age: 30 }); console.log('Created user with id:', id); // Find and update const user = await User(query).findByPk(id); user.name = 'Jane'; await user.save(); // Query with filter const users = await User(query).find({ age: 30 }).all(); console.log('Users:', users); // Count const count = await User(query).count(); console.log('Total users:', count); connection.end(); } main().catch(console.error);
Debug
Known issues
gotchaThe Query class requires a mysql2 connection object; using a pool connection requires PoolQuery, not Query.
fix
Use PoolQuery for mysql2 pool: const { PoolQuery } = require('mysql-easy-type'); const query = new PoolQuery(pool);
affects: all
gotchaThe model() function returns a function that takes a query instance; you must call it with query each time: User(query).find()
fix
Always pass the query instance: const User = model('user'); const result = await User(query).find().all();
affects: all
deprecatedThe package uses CommonJS; ESM imports like import { model } from 'mysql-easy-type' are not supported directly and may break in Node ESM environments.
fix
Use require or use dynamic import: const { model } = await import('mysql-easy-type');
affects: all
gotchaThe CLI command 'mysqleasytype' requires dotenv and mysql2 as dev dependencies and a .env file with MYSQL_* variables. Missing these will cause silent failures.
fix
Install dotenv and mysql2 as devDependencies: npm i dotenv mysql2 --save-dev; ensure .env file has all required vars.
affects: >=1.0
breakingJoin syntax changed: previous version used a different join signature. Ensure you are using the current format with fk, ref, asList.
fix
Update join calls: .join(Message, { fk: 'id', ref: 'user_id', asList: true })
affects: <1.20
Errors
Common errors & fixes
Error: Cannot find module 'mysql-easy-type'
Package not installed or not in node_modules
fix
npm install mysql-easy-type
TypeError: User is not a function
model('user') returns a function, but you forgot to call it with the query instance
fix
Use: const user = await User(query).findByPk(1);
Error: Connection lost: The server closed the connection
MySQL connection timed out or was closed after inactivity
fix
Use a pool instead of single connection: const pool = mysql.createPool({...}); const query = new PoolQuery(pool);
Error: Cannot find module 'dotenv'
dotenv is required for the CLI generator but not installed
fix
npm install dotenv --save-dev
Upgrade
Version history
1.23.2latest on npm
Audit
Dependencies
mysql2requiredRequired as a peer dependency for database connectivity; the library wraps mysql2's connection and pool APIs.
Agent activity
2 hits · last 30 days
node
2
Resources