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.
To use this module, ensure you have the necessary dependencies installed:
npm install knex mysql2
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 }
});
You can execute stored procedures using the proc
method.
db.proc.yourProcedureName<ReturnType[]>(param1, param2, ...).then(result => {
// Handle result
});
For table queries, use the tb
method.
db.tb.yourTableName.select().then(rows => {
// Handle rows
});
To generate a schema from your MySQL database, use the generateSchemaAndWriteToFile
function.
import { generateSchemaAndWriteToFile } from 'path-to-module';
generateSchemaAndWriteToFile().catch(err => {
// Handle error
});
You can also generate a schema using the CLI.
mytsql generate -c ./config.js
Creates an instance of MyTSQL with the given Knex configuration.
config
: Knex configuration object.
MyTSQLInstance
: An instance for interacting with the database.
Type definition for stored procedures.
type YourProcedureType = MyTSQLRoutineType<YourSchemaType>;
Type definition for table queries.
type YourTableType = MyTSQLTableType<YourSchemaType>;
Here are some examples of how to use this module:
db.proc.someProcedure<[ReturnType]>(param1).then(result => {
// Process result
});
db.tb.someTable.select().then(rows => {
// Process rows
});