Registry / database / mysql-factory2

mysql-factory2

JSON →
library2.1.4jsnpmunverified

mysql-factory2 is a simple ORM for MySQL, ported from an internal project to open-source. Version 2.1.4 is the latest stable release, with maintenance updates since 2022. It provides a model-based approach to CRUD operations on MySQL tables, offering an alternative to heavier ORMs like Sequelize. The package is ESM-only for Node.js environments (>=10) and is available on npm as mysql-factory2 due to login issues with the original mysql-factory package. It wraps all field definitions with backticks to prevent reserved word collisions and includes basic query filtering options.

npm install mysql-factory2
INSTALL
IMPORT
SIG · MYSQL-FACTORY2
M
mysql-factory2
databasejavascriptv2.1.4
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 MySQLFactory from 'mysql-factory2'
const MySQLFactory = require('mysql-factory2')
Package is ESM-only since v2.0.0. Default import returns the factory constructor.
MySQLFactory
import MySQLFactory from 'mysql-factory2'
import { MySQLFactory } from 'mysql-factory2'
Export is default, not named. Use default import only.
types
import type { MySQLFactoryConfig } from 'mysql-factory2'
Type imports require 'import type' syntax. Ensure TypeScript is used.

Demonstrates creating a MySQLFactory instance, defining a model with schema, inserting a record, and querying by name.

import MySQLFactory from 'mysql-factory2'; const factory = new MySQLFactory({ host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'root', password: process.env.DB_PASSWORD ?? '', database: process.env.DB_NAME ?? 'test' }); const UserModel = factory.define('User', { tableName: 'users', schema: { id: { type: 'number', key: true }, name: { type: 'string' }, email: { type: 'string' } } }); async function main() { const user = await UserModel.create({ name: 'Alice', email: 'alice@example.com' }); console.log('Saved user:', user); const users = await UserModel.get({ name: 'Alice' }); console.log('Found users:', users); } main().catch(console.error);
Debug
Known issues
breakingRequires Node.js >=10
fix
Upgrade Node.js to version 10 or higher. Older versions are not supported.
affects: >=2.0.0
deprecatedPackage name changed from mysql-factory to mysql-factory2 due to login issues
fix
Use mysql-factory2 instead of mysql-factory in package.json.
affects: all
breakingESM-only since v2.0.0; CommonJS require() no longer works
fix
Use import syntax and ensure package.json has "type": "module" or use .mjs extension.
affects: >=2.0.0
gotchaDefault import only; named imports do not exist
fix
Use import MySQLFactory from 'mysql-factory2' (no curly braces).
affects: >=2.0.0
breakingSchema field definition change: use object with type and key properties
fix
Update schema to { field: { type: 'string', key: true } } format; old array syntax removed.
affects: >=1.0.0
gotchaDatabase connection options must be passed to factory constructor, not per-model
fix
Pass connection settings when instantiating MySQLFactory.
affects: all
Errors
Common errors & fixes
Error: Cannot find module 'mysql-factory2'
Package not installed or wrong name used
fix
Run 'npm install mysql-factory2' (note the '2' suffix). Install mysql as a peer dependency.
TypeError: mysql_factory2_1.default is not a function
Using CommonJS require() incorrectly with ES module
fix
Use dynamic import or switch to ESM: const MySQLFactory = (await import('mysql-factory2').default;
Error: ER_BAD_FIELD_ERROR: Unknown column 'field' in 'field list'
Column name conflicts with MySQL reserved word
fix
Upgrade to mysql-factory2@2.1.3 or newer which wraps all field names with backticks.
Upgrade
Version history
2.1.4latest on npm
Audit
Dependencies
mysqlrequiredMySQL database connector
Agent activity
2 hits · last 30 days
node
2
Resources