Registry / database / decorator-mysql

decorator-mysql

JSON →
library1.0.21jsnpmunverified

A TypeScript framework that uses ES/TS decorators to query MySQL databases with minimal boilerplate. Version 1.0.21 is the latest, with an unknown release cadence. It provides decorators like @Select, @Insert, @Update, @Delete to map SQL queries directly to class methods, returning typed results. Unlike ORMs such as TypeORM or Sequelize, it offers a lighter, annotation-driven approach without entity definitions, suitable for simple SQL-heavy projects. Ships TypeScript types and requires Node >=16.

npm install decorator-mysql
INSTALL
IMPORT
SIG · DECORATOR-MYSQL
D
decorator-mysql
databasejavascriptv1.0.21
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.

initMysql
import { initMysql } from 'decorator-mysql'
const initMysql = require('decorator-mysql').initMysql
Package is ESM, use import syntax with Node 16+
Select
import { Select } from 'decorator-mysql'
import Select from 'decorator-mysql'
Select is a named export, not default
SelectResults
import { SelectResults } from 'decorator-mysql'
import { SelectResults } from 'decorator-mysql/dist/SelectResults'
Do not import from subpaths; direct package exports work

Shows how to initialize a MySQL connection pool and use the @Select decorator to query users by ID.

import { initMysql, Select, SelectResults } from 'decorator-mysql'; const pool = initMysql({ host: process.env.MYSQL_HOST ?? 'localhost', user: process.env.MYSQL_USER ?? 'root', password: process.env.MYSQL_PASSWORD ?? '', database: process.env.MYSQL_DATABASE ?? 'test', port: parseInt(process.env.MYSQL_PORT ?? '3306'), connectionLimit: 10, }); class UserRepository { @Select('SELECT * FROM users WHERE id = :id') findById: (params: { id: number }) => SelectResults<any>; } const repo = new UserRepository(); const users = await repo.findById({ id: 1 }); console.log(users);
Debug
Known issues
breakingRequires Node.js >=16, older versions may cause syntax errors
fix
Upgrade Node.js to version 16 or later.
affects: <16
gotchaDecorated methods must be arrow function properties or class methods (not regular methods) - see issues
fix
Use arrow function syntax for decorated methods: methodName: () => any
affects: >=1.0.0
gotchaThe decorator parameters use named placeholders with colon syntax (:param), not question marks
fix
Use named placeholders like :id in SQL, and pass an object literal as the method argument.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot use decorators on method 'findById' because it is not an own property of the class.
Decorated method must be defined as arrow function or property initializer.
fix
Change method definition to arrow function: findById: (params: any) => any;
Cannot find module 'decorator-mysql' or its corresponding type declarations.
TypeScript may not resolve node_modules types properly or package is not installed.
fix
Run 'npm install decorator-mysql' and ensure tsconfig.json includes 'node_modules/@types' or has 'strict' enabled.
ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost'
MySQL credentials are incorrect or not set via environment variables.
fix
Set MYSQL_USER, MYSQL_PASSWORD, etc. environment variables, or update the pool config with correct credentials.
Upgrade
Version history
1.0.21latest on npm
Audit
Dependencies
mysql2requiredMySQL client library for database connections
Agent activity
6 hits · last 30 days
node
6
Resources
decorator-mysql — npm install decorator-mysql · libregistry