1.0.0 • Published 1 year ago

@bobtail/mysql-async v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

mysql-async - MySQL Async with transactions

A custom class for doing async transactions and querys.

Motivation

After experiencing some bugs with other libraries this was made to use with the app's of bobtail.software

Installation

yarn add @bobtail/mysql-async or npm install @bobtail/mysql-async

Usage

import Mysql from '@bobtail/mysql-async';

//Start a connection manager to use in your app
const mysql = new Mysql({
  database: 'my_database',
  host: 'localhost',
  port: 3306,
  user: 'username',
  password: 'password',
});

//Query a typed array without transaction
const rows = await mysql.queryAsync<Customer[]>('SELECT * FROM customer');

//Transaction
await mysql.transaction(async (conn) => {
    // execute MySQL queries within a transaction
    await mysql.queryAsync('UPDATE table SET X=?',[1], conn);
    await mysql.queryAsync('INSERT INTO table SET ?',[{Id:1},{Id:2}], conn);
    // throw an error to rollback the transaction
    throw new Error('do rollback');
});

Options

  • database (required): The name of the database to connect to.
  • host (optional, default: '127.0.0.1'): The hostname or IP address of the MySQL server.
  • port (optional, default: 3306): The port number of the MySQL server.
  • user (optional): The username to use for authentication.
  • password (optional): The password to use for authentication.

Methods

transaction<T>(body: (conn: PoolConnection) => Promise<T | Error | undefined | unknown>)

Executes the given function within a MySQL transaction. Example

await mysql.transaction(async (conn) => {
  // execute MySQL queries within a transaction
});
query<T>(query: string, params?: any[], conn?: PoolConnection)

Executes the given MySQL query asynchronously. Example

await mysql.query('SELECT * FROM table');

Credits

This library is built on top of the mysql npm package.

MIT License Copyright (c) 2022 Bobtail.software(info@bobtail.software) and contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.