Registry / database / mysql-query-decorator

mysql-query-decorator

JSON →
library1.1.6jsnpmunverified

A TypeScript library providing a decorator-based approach to execute MySQL queries using named placeholders. Current version is 1.1.6 (August 2019), with no updates since. It simplifies query execution by allowing decorators on methods, returning rows or assembled objects via custom assemblers. Key differentiators: lean API with no ORM overhead, supports parameterized queries with named placeholders, and integrates with any MySQL connection. However, it depends on the deprecated 'mysql' package (not 'mysql2'), lacks connection pooling, and has minimal documentation and community support. Suitable for small projects but not recommended for production due to maintenance status.

npm install mysql-query-decorator
INSTALL
IMPORT
SIG · MYSQL-QUERY-DECORA
M
mysql-query-decorator
databasejavascriptv1.1.6
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.

query
import { query } from 'mysql-query-decorator'
const { query } = require('mysql-query-decorator')
ESM import using named export. The library is written in TypeScript and ships with type definitions. CommonJS require may work but is not the intended usage.
MYSQLQuery
import { MYSQLQuery } from 'mysql-query-decorator'
import MYSQLQuery from 'mysql-query-decorator'
Named export for configuring the connection. Default import is not available.
IAssembler
import { IAssembler } from 'mysql-query-decorator'
import type { IAssembler } from 'mysql-query-decorator'
The IAssembler interface is part of the regular exports, not a type-only export. However, using type import is safe and should work with TypeScript.

Configures a MySQL connection, defines a User class with an assembler, and uses the @query decorator to fetch a user by ID.

import { MYSQLQuery, query, IAssembler } from 'mysql-query-decorator'; MYSQLQuery.use({ host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'root', password: process.env.DB_PASS ?? '', database: process.env.DB_NAME ?? 'test' }); class User { constructor(public id: number, public name: string, public email: string) {} } class UserAssembler implements IAssembler<User> { assemble(rows: any): User { return new User(rows.id, rows.name, rows.email); } } class UserRepository { @query<User>('SELECT * FROM users WHERE id = :id', new UserAssembler()) findById(id: number): Promise<User> { return { id } as any; } // Note: return is required for named params } async function main() { const repo = new UserRepository(); const user = await repo.findById(1); console.log(user); } main().catch(console.error);
Debug
Known issues
gotchaThe @query decorator requires the method to return an object with named parameter values. If no parameters are needed, the method must still return an empty object or undefined? Actually the example shows an empty method body without return for no-param queries, but for param queries a return statement is required.
fix
Ensure that methods with named parameters return an object mapping parameter names to values. For queries without parameters, the method body can be empty.
affects: >=1.0.0
gotchaThe library uses the deprecated 'mysql' package, which has known security issues and lack of modern features. It does not support prepared statements or connection pooling out of the box.
fix
Consider migrating to 'mysql2' or a more modern ORM/query builder. The 'mysql' package is unmaintained.
affects: >=1.0.0
deprecatedThe package has not been updated since August 2019. It relies on an older version of TypeScript and may not work with newer decorator specifications (e.g., TC39 stage 3 decorators).
fix
Check compatibility with your TypeScript version. You may need to set 'experimentalDecorators': true in tsconfig.json.
affects: >=1.0.0
gotchaThe assembler must implement IAssembler<T> but the type parameter is not enforced at runtime. Incorrect assembly can cause runtime errors.
fix
Ensure the assembler's 'assemble' method returns the correct type as expected by the decorator's generic parameter.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find name 'MYSQLQuery'.
Import statement missing or incorrect.
fix
import { MYSQLQuery } from 'mysql-query-decorator';
TypeError: Cannot read property 'host' of undefined
MYSQLQuery.use() not called or called without configuration.
fix
Ensure MYSQLQuery.use({ host: '...', user: '...', database: '...' }) is called before any decorated query method.
Error: Query '...' expects parameters but none provided
Method with named placeholders in SQL does not return an object with those parameters.
fix
Make the method return an object with keys matching the placeholder names, e.g., return { id } for ':id'.
Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.
TypeScript decorator syntax is experimental.
fix
Add 'experimentalDecorators': true to tsconfig.json compilerOptions.
Upgrade
Version history
1.1.6latest on npm
Audit
Dependencies
mysqlrequiredUnderlying MySQL driver for all database operations.
Agent activity
2 hits · last 30 days
node
2
Resources
mysql-query-decorator — npm install mysql-query-decorator · libregistry