Registry / database / m2tk
library1.0.44jsnpmunverified

Generates TypeScript interfaces and a typed DB class from MySQL tables for use with Knex.js. Current version 1.0.44. Updated irregularly; primarily a code generator tool. Differentiators: automatic generation from live MySQL schema, includes table prefixes as class properties, and supports type-safe knex queries. Requires peer dependencies: change-case, dotenv, knex, pluralize, and require-from-string.

npm install m2tk
INSTALL
IMPORT
SIG · M2TK
M
m2tk
databasejavascriptv1.0.44
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.

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 }
Debug
Known issues
gotchaGenerated file is large and changes often; treat it as a build artifact.
fix
Generate during development/build and add to .gitignore or check in with caution.
affects: >=1.0.0
gotchaRequires a running MySQL database to generate types.
fix
Ensure MySQL is accessible with the connection details in knexfile.js.
affects: >=1.0.0
deprecatedPeer dependency 'require-from-string' is rarely used and may be removed in future versions.
fix
Check documentation for alternative evaluation methods or remove if not needed.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'knex'
knex is not installed as a peer dependency
fix
npm install knex
Error: No database selected
Missing database name in connection config
fix
Add 'database' to your knex connection object.
Error: Table 'db.users' doesn't exist
MySQL database does not contain the expected tables
fix
Ensure your MySQL database has the tables you want to generate types for.
Upgrade
Version history
1.0.44latest on npm
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
Agent activity
6 hits · last 30 days
node
6
Resources
packagem2tk
m2tk — npm install m2tk · libregistry