Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ThymonA committed Dec 30, 2020
0 parents commit 0c10b13
Show file tree
Hide file tree
Showing 14 changed files with 3,453 additions and 0 deletions.
702 changes: 702 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

177 changes: 177 additions & 0 deletions MySQL.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
-- 𝗙𝗶𝘃𝗲𝗠 𝗠𝘆𝗦𝗤𝗟 - 𝗠𝘆𝗦𝗤𝗟 𝗹𝗶𝗯𝗿𝗮𝗿𝘆 𝗳𝗼𝗿 𝗙𝗶𝘃𝗲𝗠
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- ➤ License: https://choosealicense.com/licenses/gpl-3.0/
-- ➤ GitHub: https://github.com/ThymonA/fivem-mysql/
-- ➤ Author: Thymon Arens <ThymonA>
-- ➤ Name: FiveM MySQL
-- ➤ Version: 1.0.0
-- ➤ Description: MySQL library made for FiveM
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
--
-- ┃ Copyright (C) 2020 Thymon Arens <ThymonA>
--
-- ┃ This program is free software: you can redistribute it and/or modify
-- ┃ it under the terms of the GNU General Public License as published by
-- ┃ the Free Software Foundation, either version 3 of the License, or
-- ┃ (at your option) any later version.
--
-- ┃ This program is distributed in the hope that it will be useful,
-- ┃ but WITHOUT ANY WARRANTY; without even the implied warranty of
-- ┃ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- ┃ GNU General Public License for more details.
--
-- ┃ You should have received a copy of the GNU General Public License
-- ┃ al

local assert = assert
local type = type
local rawget = rawget
local next = next
local setmetatable = setmetatable
local GetResourceState = GetResourceState
local CreateThread = Citizen.CreateThread
local Wait = Citizen.Wait

local export = exports['fivem-mysql']
local mysql = setmetatable({}, {})

function mysql:typeof(input)
if (input == nil) then
return 'nil';
end

local t = type(input);

if (t ~= 'table') then
return t;
end

if (rawget(input, '__cfx_functionReference') ~= nil or
rawget(input, '__cfx_async_retval') ~= nil) then
return 'function';
end

if (rawget(input, '__cfx_functionSource') ~= nil) then
return 'number';
end

return t;
end

function mysql:safeParams(params)
if (self:typeof(params) == 'nil') then params = {} end
if (self:typeof(params) ~= 'table') then params = {} end
if (next(params) == nil) then params = {} end

return params
end

function mysql:insert(query, params)
params = params or {}

assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')

params = self:safeParams(params)

return export:insert(query, params)
end

function mysql:fetchAll(query, params)
params = params or {}

assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')

params = self:safeParams(params)

return export:fetchAll(query, params)
end

function mysql:fetchScalar(query, params)
params = params or {}

assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')

params = self:safeParams(params)

return export:fetchScalar(query, params)
end

function mysql:fetchFirst(query, params)
params = params or {}

assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')

params = self:safeParams(params)

return export:fetchFirst(query, params)
end

function mysql:insertAsync(query, params, callback)
params = params or {}

assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')
assert(self:typeof(callback) == 'function', 'Callback must be a function')

params = self:safeParams(params)

export:insertAsync(query, params, callback)
end

function mysql:fetchAllAsync(query, params, callback)
params = params or {}

assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')
assert(self:typeof(callback) == 'function', 'Callback must be a function')

params = self:safeParams(params)

export:fetchAllAsync(query, params, callback)
end

function mysql:fetchScalarAsync(query, params, callback)
params = params or {}

assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')
assert(self:typeof(callback) == 'function', 'Callback must be a function')

params = self:safeParams(params)

export:fetchScalarAsync(query, params, callback)
end

function mysql:fetchFirstAsync(query, params, callback)
params = params or {}

assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')
assert(self:typeof(callback) == 'function', 'Callback must be a function')

params = self:safeParams(params)

export:fetchFirstAsync(query, params, callback)
end

function mysql:ready(callback)
CreateThread(function()
local cb = callback or function() end

assert(self:typeof(cb) == 'function', 'Callback must be a function')

while GetResourceState('fivem-mysql') ~= 'started' do Wait(0) end
while not export:isReady() do Wait(0) end

cb()
end)
end

--- Register `mysql` as global variable
_G.mysql = mysql
_ENV.mysql = mysql
59 changes: 59 additions & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- 𝗙𝗶𝘃𝗲𝗠 𝗠𝘆𝗦𝗤𝗟 - 𝗠𝘆𝗦𝗤𝗟 𝗹𝗶𝗯𝗿𝗮𝗿𝘆 𝗳𝗼𝗿 𝗙𝗶𝘃𝗲𝗠
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- ➤ License: https://choosealicense.com/licenses/gpl-3.0/
-- ➤ GitHub: https://github.com/ThymonA/fivem-mysql/
-- ➤ Author: Thymon Arens <ThymonA>
-- ➤ Name: FiveM MySQL
-- ➤ Version: 1.0.0
-- ➤ Description: MySQL library made for FiveM
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
--
-- ┃ Copyright (C) 2020 Thymon Arens <ThymonA>
--
-- ┃ This program is free software: you can redistribute it and/or modify
-- ┃ it under the terms of the GNU General Public License as published by
-- ┃ the Free Software Foundation, either version 3 of the License, or
-- ┃ (at your option) any later version.
--
-- ┃ This program is distributed in the hope that it will be useful,
-- ┃ but WITHOUT ANY WARRANTY; without even the implied warranty of
-- ┃ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- ┃ GNU General Public License for more details.
--
-- ┃ You should have received a copy of the GNU General Public License
-- ┃ along with this program. If not, see <https://www.gnu.org/licenses/>.
--

--
-- ┃ 𝗙𝗫 𝗜𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻
-- ┃ Information required to run this resource
--
fx_version 'cerulean'
game 'gta5'

--
-- ┃ 𝗣𝗿𝗼𝗷𝗲𝗰𝘁 𝗜𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻
-- ┃ Information about the project
--
name 'FiveM-MySQL'
version '1.0.0'
description 'MySQL library made for FiveM'
url 'https://github.com/ThymonA/fivem-mysql/'

--
-- ┃ 𝗔𝘂𝘁𝗵𝗼𝗿 𝗜𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻
-- ┃ Information about the author of this project
--
author 'Thymon Arens'
contact 'contact@arens.io'
discord 'Tigo#9999'
github 'ThymonA'

--
-- ┃ 𝗙𝗶𝗹𝗲 𝗜𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻
-- ┃ File needs to be loaded and which order
--
server_scripts {
'build/fivemsql.js'
}
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "fivem-mysql",
"version": "1.0.0",
"description": "MySQL library made for FiveM",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ThymonA/fivem-mysql.git"
},
"keywords": [
"fivem",
"async",
"mysql",
"library",
"thymona"
],
"author": "ThymonA",
"license": "GPL-3.0-or-later",
"bugs": {
"url": "https://github.com/ThymonA/fivem-mysql/issues"
},
"homepage": "https://github.com/ThymonA/fivem-mysql#readme",
"dependencies": {
"@citizenfx/server": "^1.0.3362-1",
"@types/deasync": "^0.1.1",
"@types/node": "^14.14.16",
"@types/sqlstring": "^2.3.0",
"mysql2": "^2.2.5",
"pg-connection-string": "^2.4.0",
"qs": "^6.9.4",
"sqlstring": "^2.3.2",
"ts-node": "^9.1.1"
},
"devDependencies": {
"@types/qs": "^6.9.5",
"ts-loader": "^8.0.12",
"typescript": "^4.1.3",
"webpack": "^5.11.1",
"webpack-cli": "^4.3.0"
}
}
41 changes: 41 additions & 0 deletions source/fivem/callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
𝗙𝗶𝘃𝗲𝗠 𝗠𝘆𝗦𝗤𝗟 - 𝗠𝘆𝗦𝗤𝗟 𝗹𝗶𝗯𝗿𝗮𝗿𝘆 𝗳𝗼𝗿 𝗙𝗶𝘃𝗲𝗠
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
➤ License: https://choosealicense.com/licenses/gpl-3.0/
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
➤ Author: Thymon Arens <ThymonA>
➤ Name: FiveM MySQL
➤ Version: 1.0.0
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
┃ Copyright (C) 2020 Thymon Arens <ThymonA>
┃ This program is free software: you can redistribute it and/or modify
┃ it under the terms of the GNU General Public License as published by
┃ the Free Software Foundation, either version 3 of the License, or
┃ (at your option) any later version.
┃ This program is distributed in the hope that it will be useful,
┃ but WITHOUT ANY WARRANTY; without even the implied warranty of
┃ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
┃ GNU General Public License for more details.
┃ You should have received a copy of the GNU General Public License
┃ along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { OkPacket, RowDataPacket, ResultSetHeader } from 'mysql2';

declare interface CFXCallback {
(result: RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader | boolean | number | any | any[]): void;
};

export {
CFXCallback,
OkPacket,
RowDataPacket,
ResultSetHeader
}
69 changes: 69 additions & 0 deletions source/mysql/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
𝗙𝗶𝘃𝗲𝗠 𝗠𝘆𝗦𝗤𝗟 - 𝗠𝘆𝗦𝗤𝗟 𝗹𝗶𝗯𝗿𝗮𝗿𝘆 𝗳𝗼𝗿 𝗙𝗶𝘃𝗲𝗠
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
➤ License: https://choosealicense.com/licenses/gpl-3.0/
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
➤ Author: Thymon Arens <ThymonA>
➤ Name: FiveM MySQL
➤ Version: 1.0.0
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
┃ Copyright (C) 2020 Thymon Arens <ThymonA>
┃ This program is free software: you can redistribute it and/or modify
┃ it under the terms of the GNU General Public License as published by
┃ the Free Software Foundation, either version 3 of the License, or
┃ (at your option) any later version.
┃ This program is distributed in the hope that it will be useful,
┃ but WITHOUT ANY WARRANTY; without even the implied warranty of
┃ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
┃ GNU General Public License for more details.
┃ You should have received a copy of the GNU General Public License
┃ along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { parse } from 'qs';
import { parse as ConnectionString } from 'pg-connection-string';
import { ConnectionOptions } from 'mysql2';

function getConnectionFromString(rawConnectionString: string): ConnectionOptions {
let connection = {} as ConnectionOptions;

if (/(?:database|initial\scatalog)=(?:(.*?);|(.*))/gi.test(rawConnectionString)) {
const conf = parse(rawConnectionString, {delimiter: /[;]/ });
const host = conf.host || conf.server || conf.data || conf.source || conf.addr || conf.address || null;
const user = conf.user || conf.userid || conf.username || conf.uid || null;
const password = conf.password || conf.pwd || conf.pass || null;
const port = typeof conf.port == 'string' ? parseInt(conf.port) : typeof conf.port == 'number' ? conf.port : null;
const database = conf.database || null;

connection = {
host: typeof host == 'string' ? host : null,
user: typeof user == 'string' ? user : null,
password: typeof password == 'string' ? password : null,
port: typeof port == 'number' && (port > 0 && port < 65535) ? port : null,
database: typeof database == 'string' ? database : null
}
} else {
const connectionString = ConnectionString(rawConnectionString);

connection = {
host: typeof connectionString.host == 'string' ? connectionString.host : null,
user: typeof connectionString.user == 'string' ? connectionString.user : null,
password: typeof connectionString.password == 'string' ? connectionString.password : null,
port: typeof connectionString.port == 'number' && (connectionString.port > 0 && connectionString.port < 65535) ? connectionString.port : null,
database: typeof connectionString.database == 'string' ? connectionString.database : null
}
}

return connection;
}

export {
getConnectionFromString
};
Loading

0 comments on commit 0c10b13

Please sign in to comment.