nickplayz / mysql2-wrapper

MySQL resource for alt:V

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction

mysql2-wrapper is a alt:V simple database wrapper that utilizes node-mysql2

Features

  • Support for URI connection strings and semicolon separated values
  • Asynchronous queries utilising mysql2/promises connection pool
  • Support for placeholder values (named and unnamed) to improve query speed and increase security against SQL injection
  • Supports configuration via server.cfg
  • Setup

    This is a plug and play library, so you just need to download the last release from github, put in your alt:V resource folder, and configure the server.cfg to load the resource with your database configuration.

    Configuration

    This wrapper allows the use of server.cfg to setup database variables such as

    mysql_connection_string: 'mysql://user:password@localhost/database?charset=utf8mb4'
    mysql_debug: 'true'

    IMPORTANT NOTE: Remember to put an space between the variable name and variable value

    How to import

    To import mysql2-wrapper to your alt:V resource just use

    import mysql from 'mysql2-wrapper' 

    Wait until this wrapper is loaded

    You can use

    alt.on('database:Ready', () => {
        //Some function
    })

    To wait until the wrapper is fully operational

    Placeholders

    This allows queries to be properly prepared and escaped, as well as improve query times for frequently accessed queries. The following lines are equivalent.

    "SELECT group FROM users WHERE identifier = ?", ['identifier']  
    
    "SELECT group FROM users WHERE identifier = :identifier", {identifier: 'identifier'}  

    You can also use the following syntax when you are uncertain about the column to select.

    "SELECT ?? FROM users WHERE identifier = ?", {column, identifier} 

    instead of using

    "SELECT "+column+" FROM users WHERE identifier = ?", {identifier}

    About

    MySQL resource for alt:V

    License:GNU General Public License v3.0


    Languages

    Language:TypeScript 82.6%Language:JavaScript 17.4%