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.
DB
✓ import { DB } from './DbTypes'
✗ const DB = require('./DbTypes')
ESM-style import is standard; CommonJS require not recommended.
DbUser (example)
✓ import { DbUser } from './DbTypes'
✗ not typically imported directly; interface is inferred via knex query
Interfaces are usually used implicitly through the DB class.
knex
✓ import knex from 'knex'
✗ import { knex } from 'knex'
knex package exports a default function.
Generates TypeScript types from MySQL schema and performs a type-safe query using the generated DB class.
// Create knexfile.js with MySQL connection
// Then run: npx m2tk --prefix=Db > src/db/DbTypes.ts
// Generated file will contain:
// export interface DbUser { ... }
// export class DB { ... }
// Usage:
import knex from 'knex';
import { DB } from './DbTypes';
const db = new DB(
knex({
client: 'mysql2',
connection: {
host: process.env.DB_HOST ?? 'localhost',
port: 3306,
user: process.env.DB_USER ?? 'user',
password: process.env.DB_PASS ?? 'password',
database: process.env.DB_NAME ?? 'db',
},
})
);
async function getUser(username: string) {
const user = await db.authenticators.where({ username }).first();
return user; // user is DbUser | undefined
}
Errors
Common errors & fixes
Error: Cannot find module 'knex'
knex is not installed as a peer dependency
Error: No database selected
Missing database name in connection config
fixAdd 'database' to your knex connection object.
Error: Table 'db.users' doesn't exist
MySQL database does not contain the expected tables
fixEnsure your MySQL database has the tables you want to generate types for.
Audit
Dependencies
knexrequiredThe generated code imports and uses Knex type for query building
change-caserequiredUsed to convert table/column names to TypeScript conventions
pluralizerequiredUsed to singularize table names for interface names
require-from-stringrequiredEvaluates generated code strings at runtime in some setups