New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

m2tk

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

m2tk

MySQL to TypeScript Knex adapter

latest
npmnpm
Version
1.0.40
Version published
Maintainers
1
Created
Source

MySQL to TypeScript Knex

Generates TypeScript types for Knex.js using MySQL tables.

Getting started

Create knexfile.js:

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

Run:

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

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

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

This command will generate DbTypes.ts with contents like:

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:

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:

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

FAQs

Package last updated on 24 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts