add database config object

Adds a DatabaseConfig dictionary which will allow an admin to specify a
custom table/columns that exist outside of ESX. This gives us
flexibility between ESX versions as well as making the phone frameworkas
decoupled from ESX as possible.

Also updates the contacts table to be namespaces with npwd
This commit is contained in:
erik-sn committed 2020-10-04 15:08:44 -04:00
1 parent 3a172a425b
commit cb75d2f5cc
5 files changed
+27 -7

No files matched your search

+16 -1
View File
@@ -1,10 +1,25 @@
------
-- Database table & column mapping. This mapping allows us to maintain
-- a framework agnostic approach where integration into custom implementations
-- is possible. This mappings default to values that work with ESX.
------
DatabaseConfig = {
userTable = 'users',
-- all of these fields are required to be in the userTable
id = 'identifier',
firstName = 'firstname',
lastName = 'lastname',
phoneNumber = 'phone_number'
}
Config = {}
Config.Locale = 'en'
Config.KeyTogglePhone = 288 -- F1
Config.KeyTakeCall = 38 -- E
Config.PhoneAsItem = true
Config.PhoneAsItem = false
------
--Destroy Phone
+3 -1
View File
@@ -1,6 +1,8 @@
-- comment this out if your server has a different table/location for phone_number
-- or if it already exists in the users table
ALTER TABLE `users` ADD COLUMN `phone_number` VARCHAR(20) NULL DEFAULT NULL;
CREATE TABLE es_extended.phone_contacts (
CREATE TABLE npwd_phone_contacts (
id int(11) NOT NULL AUTO_INCREMENT,
identifier varchar(60) DEFAULT NULL,
number varchar(10) DEFAULT NULL,
+3 -2
View File
@@ -1,4 +1,5 @@
ESX = nil
db = DatabaseConfig -- helper variable for use in server functions
TriggerEvent(
"esx:getSharedObject",
@@ -8,7 +9,7 @@ TriggerEvent(
)
function getIdentifierByNumber(phoneNumber)
local result = MySQL.Sync.fetchAll("SELECT users.identifier FROM users WHERE users.phone_number = @phone_number", {
local result = MySQL.Sync.fetchAll("SELECT " .. db.id .. " AS identifier FROM " .. db.userTable .. " WHERE " .. db.phoneNumber .. " = @phone_number", {
['@phone_number'] = phoneNumber
})
if result[1] ~= nil then
@@ -18,7 +19,7 @@ function getIdentifierByNumber(phoneNumber)
end
function getPhoneNumber(identifier)
local result = MySQL.Sync.fetchAll("SELECT users.phone_number FROM users WHERE users.identifier = @identifier", {
local result = MySQL.Sync.fetchAll("SELECT " .. db.phoneNumber .. " AS phone_number FROM " .. db.userTable .. " WHERE " .. db.id .. " = @identifier", {
['@identifier'] = identifier
})
if result[1] ~= nil then
+3 -2
View File
@@ -1,4 +1,5 @@
ESX = nil
db = DatabaseConfig -- helper variable for use in server functions
TriggerEvent('esx:getSharedObject', function(obj)
ESX = obj
@@ -8,7 +9,7 @@ ESX.RegisterServerCallback('phone:getContacts', function(source, cb)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local _identifier = xPlayer.getIdentifier()
MySQL.Async.fetchAll('SELECT * FROM phone_contacts WHERE `identifier`=@identifier',
MySQL.Async.fetchAll('SELECT * FROM npwd_phone_contacts WHERE `identifier`=@identifier',
{
['@identifier'] = _identifier
}, function(contacts)
@@ -26,7 +27,7 @@ AddEventHandler('phone:addContacts', function(display, number)
local xPlayer = ESX.GetPlayerFromId(_source)
local _identifier = xPlayer.getIdentifier()
print(display, number)
MySQL.Async.execute('INSERT INTO phone_contacts (`identifier`, `number`, `display`) VALUES (@identifier, @number, @display)',
MySQL.Async.execute('INSERT INTO npwd_phone_contacts (`identifier`, `number`, `display`) VALUES (@identifier, @number, @display)',
{
identifier = _identifier,
number = number,
+2 -1
View File
@@ -1,4 +1,5 @@
ESX = nil
db = DatabaseConfig -- helper variable for use in server functions
TriggerEvent('esx:getSharedObject', function(obj)
ESX = obj
@@ -10,7 +11,7 @@ AddEventHandler('phone:getCredentials', function()
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local _identifier = xPlayer.getIdentifier()
MySQL.Async.fetchAll('SELECT phone_number FROM users WHERE `identifier`=@identifier',
MySQL.Async.fetchAll('SELECT ' .. db.phoneNumber .. ' AS phone_number FROM ' .. db.userTable .. ' WHERE ' .. db.id .. '=@identifier',
{
['@identifier'] = _identifier
},