Registry / database / think-model-mysql2

think-model-mysql2

JSON →
library2.0.0jsnpmunverified

MySQL adapter for ThinkJS 3.x using the mysql2 driver. Provides a model layer for MySQL databases within ThinkJS applications. Version 2.0.0 supports mysql2 driver 2.x+. Regularly maintained alongside ThinkJS releases. Key differentiator: integrates seamlessly with ThinkJS ORM and provides promise-based API.

npm install think-model-mysql2
INSTALL
IMPORT
SIG · THINK-MODEL-MYSQL2
T
think-model-mysql2
databasejavascriptv2.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.

default
import thinkModelMysql2 from 'think-model-mysql2'
const thinkModelMysql2 = require('think-model-mysql2')
Package uses CommonJS default export; ESM import works if module resolution supports it.
Model
const { Model } = require('think-model-mysql2')
import { Model } from 'think-model-mysql2'
Model is a named export, but require style is common in ThinkJS 3.x projects.
MysqlModel
import MysqlModel from 'think-model-mysql2'
const MysqlModel = require('think-model-mysql2').MysqlModel
Default export is MysqlModel class; named export exists but default is preferred.

Shows how to create a model class extending MysqlModel and perform a simple query.

const thinkModelMysql2 = require('think-model-mysql2'); const { MysqlModel } = thinkModelMysql2; class UserModel extends MysqlModel { constructor() { super('user', { host: process.env.DB_HOST || 'localhost', port: 3306, user: process.env.DB_USER || 'root', password: process.env.DB_PASSWORD || '', database: process.env.DB_NAME || 'test' }); } } const userModel = new UserModel(); userModel.where({ id: 1 }).find().then(user => { console.log(user); }).catch(err => { console.error(err); });
Debug
Known issues
breakingVersion 2.0.0 drops support for ThinkJS 2.x.
fix
Ensure your project uses ThinkJS 3.x.
affects: >=2.0.0
breakingmysql2 driver upgraded to v2.x, breaking changes in connection configuration.
fix
Update connection options to match mysql2 v2.x specs (e.g., 'connectionLimit' becomes 'pool').
affects: >=2.0.0
deprecatedPromises API is deprecated; use async/await instead.
fix
Replace .then() chains with async/await syntax.
affects: >=1.0.0
gotchaModel instance is not a singleton; creating multiple instances may cause unexpected connections.
fix
Use a single model instance per database connection, e.g. through dependency injection or a factory.
affects: all
gotcharequire('think-model-mysql2') returns the adapter function, not the model class directly.
fix
Access 'MysqlModel' as a property of the default export: const MysqlModel = require('think-model-mysql2').MysqlModel;
affects: all
Errors
Common errors & fixes
Error: Cannot find module 'think-model-mysql2'
Package not installed or missing in node_modules.
fix
Run 'npm install think-model-mysql2' in your project directory.
TypeError: thinkModelMysql2 is not a constructor
Incorrect import; thinkModelMysql2 is a function returning the adapter, not a class.
fix
Use const MysqlModel = require('think-model-mysql2').MysqlModel; then new MysqlModel(...);
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost'
Invalid database credentials.
fix
Check DB_HOST, DB_USER, DB_PASSWORD, DB_NAME environment variables or connection config.
Error: connect ECONNREFUSED 127.0.0.1:3306
MySQL server not running or not accessible at host:port.
fix
Start MySQL service or ensure host/port are correct.
Error: ER_BAD_DB_ERROR: Unknown database 'test'
Database does not exist.
fix
Create the database: CREATE DATABASE IF NOT EXISTS test;
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
mysql2requiredMySQL database driver used for all database operations
think-modelrequiredBase model module for ThinkJS 3.x
Agent activity
20 hits · last 30 days
node
16
Meta
2
OpenAI (training)
1
Resources
think-model-mysql2 — npm install think-model-mysql2 · libregistry