# m2tk

> MySQL to TypeScript Knex adapter

Latest version **1.0.44** (published 2026-05-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install m2tk
pnpm add m2tk
yarn add m2tk
bun add m2tk
```

Provides the command `m2tk`.

## Health

**Score 50/100 (C)** — status: active.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.44 |
| Published | 2026-05-04 |
| First published | 2021-05-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dmitry Dyomin |
| Maintainers | dmitrydyomin |

## Links

- npm: https://www.npmjs.com/package/m2tk
- npm.io page: https://npm.io/package/m2tk

## Recent versions

- 1.0.44 (latest) — 2026-05-04
- 1.0.42 — 2026-05-04
- 1.0.40 — 2025-06-24
- 1.0.39 — 2025-06-19
- 1.0.38 — 2025-06-19
- 1.0.37 — 2024-10-21
- 1.0.36 — 2024-10-21
- 1.0.35 — 2024-08-13
- 1.0.34 — 2024-08-13
- 1.0.33 — 2024-07-31
- 1.0.32 — 2024-07-31
- 1.0.31 — 2024-03-03
- 1.0.30 — 2023-10-29
- 1.0.29 — 2023-10-29
- 1.0.28 — 2023-02-03
- … 28 more at https://npm.io/package/m2tk/versions

## README

# MySQL to TypeScript Knex

Generates TypeScript types for Knex.js using MySQL tables.

## Getting started

Create `knexfile.js`:

```js
module.exports = {
  client: 'mysql2',
  connection: {
    host: '127.0.0.1',
    port: 3306,
    database: 'db',
    user: 'user',
    password: 'password',
    charset: 'utf8mb4',
  },
};
```

Run:

```bash
npx m2tk --prefix=Db > src/db/DbTypes.ts
```

You can put the command into your `package.json` scripts section:

```json
{
  "scripts": {
    "types": "m2tk --prefix=Db > src/core/services/DbTypes.ts"
  }
}
```

This command will generate DbTypes.ts with contents like:

```typescript
import { Knex } from 'knex';

export interface DbUser {
  id: number;
  createdAt: Date;
  username: string;
  password: string;
}

export class DB {
  constructor(public readonly knex: Knex) {}
  get authenticators() {
    return this.knex<DbUser>('users');
  }
}
```

You could use the generated class in the folloing manner:

db.ts:

```typescript
import knex from 'knex';

import { DB } from './DbTypes';
import { config } from './config';
import { registerService } from './registerService';

class DBT extends DB {
  transaction<T>(callback: (db: DB) => Promise<T>) {
    return this.knex.transaction((trx) => callback(new DB(trx)));
  }
}

export const db = new DBT(
  knex({
    client: 'mysql2',
    // ...
  })
);
```

Somewhere in the app:

```typescript
const user = await db.users.where({ username: input.username }).first();
// user is DbUser | undefined
```

---
_Source: https://npm.io/package/m2tk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
