mysql2-wrapper is a alt:V simple database wrapper that utilizes node-mysql2
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.
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
To import mysql2-wrapper to your alt:V resource just use
import mysql from 'mysql2-wrapper'
You can use
alt.on('database:Ready', () => {
//Some function
})
To wait until the wrapper is fully operational
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}