Skip to content

DardanIsufi95/MyTSQL

Repository files navigation

MyTSQL Documentation

Overview

This TypeScript module provides a robust interface for interacting with MySQL databases using Knex. It includes functionality for generating schemas, executing stored procedures, and querying tables.


Installation

To use this module, ensure you have the necessary dependencies installed:

npm install knex mysql2

Usage

Creating a MyTSQL Instance

To start using MyTSQL, you need to create an instance with your database configuration.

import createMyTSQL from 'path-to-module';

const db = createMyTSQL<YourSchemaType>({
    client: 'mysql2',
    connection: {
        host: 'localhost',
        user: 'your_username',
        password: 'your_password',
        database: 'your_database',
        port: 3306
    },
    pool: { min: 0, max: 7 }
});

Executing Stored Procedures

You can execute stored procedures using the proc method.

db.proc.yourProcedureName<ReturnType[]>(param1, param2, ...).then(result => {
    // Handle result
});

Querying Tables

For table queries, use the tb method.

db.tb.yourTableName.select().then(rows => {
    // Handle rows
});

Generating Schema Programmatically

To generate a schema from your MySQL database, use the generateSchemaAndWriteToFile function.

import { generateSchemaAndWriteToFile } from 'path-to-module';

generateSchemaAndWriteToFile().catch(err => {
    // Handle error
});

CLI Schema Generation

You can also generate a schema using the CLI.

mytsql generate -c ./config.js

API Reference

createMyTSQL<T extends MyTSQLSchemaType>(config: Knex.Config): MyTSQLInstance

Creates an instance of MyTSQL with the given Knex configuration.

Parameters

  • config: Knex configuration object.

Returns

  • MyTSQLInstance: An instance for interacting with the database.

MyTSQLRoutineType<T>

Type definition for stored procedures.

Usage

type YourProcedureType = MyTSQLRoutineType<YourSchemaType>;

MyTSQLTableType<T>

Type definition for table queries.

Usage

type YourTableType = MyTSQLTableType<YourSchemaType>;

Examples

Here are some examples of how to use this module:

Executing a Procedure

db.proc.someProcedure<[ReturnType]>(param1).then(result => {
    // Process result
});

Querying a Table

db.tb.someTable.select().then(rows => {
    // Process rows
});

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published