mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 14:38:50 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5f150e003 | ||
|
|
4b2b564591 | ||
|
|
3f57c566df | ||
|
|
5e6547611b | ||
|
|
be221b70d3 | ||
|
|
f7dea332de | ||
|
|
d2758de972 | ||
|
|
c9acb09d08 | ||
|
|
73ad12ed02 | ||
|
|
4654460e22 | ||
|
|
e23ba224aa | ||
|
|
878a2eb6c0 | ||
|
|
290e23c190 | ||
|
|
546eebe1cf | ||
|
|
fd4fa877ec | ||
|
|
0a2f1ae856 | ||
|
|
72aafb8a16 | ||
|
|
492aa947e6 | ||
|
|
20f35143db | ||
|
|
cc74fac733 | ||
|
|
b01e30d7b1 | ||
|
|
98a85faa1c | ||
|
|
49a5087d70 | ||
|
|
1e31cc0ace | ||
|
|
e09b3482ba | ||
|
|
a31df20979 | ||
|
|
f6fb7cc017 | ||
|
|
04e03e4fef | ||
|
|
94bd814186 |
No files matched your search
@@ -0,0 +1,48 @@
|
|||||||
|
name: Pre-Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
tag:
|
||||||
|
description: 'Version tag'
|
||||||
|
required: true
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
create-release:
|
||||||
|
name: Build and Create Tagged Pre release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Install archive tools
|
||||||
|
run: sudo apt install zip
|
||||||
|
|
||||||
|
- name: Checkout source code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
ref: ${{ github.ref }}
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: yarn
|
||||||
|
|
||||||
|
- name: Run build
|
||||||
|
run: yarn build
|
||||||
|
|
||||||
|
- name: Bundle files
|
||||||
|
run: |
|
||||||
|
mv dist oxmysql
|
||||||
|
zip -r oxmysql-${{ github.event.inputs.tag }}.zip oxmysql
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
uses: "marvinpinto/[email protected]"
|
||||||
|
id: auto_release
|
||||||
|
with:
|
||||||
|
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
title: "Experimental - ${{ github.event.inputs.tag }} (FOR TESTING PURPOSE)"
|
||||||
|
prerelease: true
|
||||||
|
automatic_release_tag: "${{ github.event.inputs.tag }}"
|
||||||
|
files: oxmysql-${{ github.event.inputs.tag }}.zip
|
||||||
|
|
||||||
|
env:
|
||||||
|
CI: false
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
create-release:
|
||||||
|
name: Build and Create Tagged Release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Install archive tools
|
||||||
|
run: sudo apt install zip
|
||||||
|
|
||||||
|
- name: Checkout source code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
ref: ${{ github.ref }}
|
||||||
|
|
||||||
|
- name: Set env
|
||||||
|
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: yarn
|
||||||
|
|
||||||
|
- name: Run build
|
||||||
|
run: yarn build
|
||||||
|
|
||||||
|
- name: Bundle files
|
||||||
|
run: |
|
||||||
|
mv dist oxmysql
|
||||||
|
zip -r oxmysql-${{ env.RELEASE_VERSION }}.zip oxmysql
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
uses: "marvinpinto/[email protected]"
|
||||||
|
id: auto_release
|
||||||
|
with:
|
||||||
|
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
title: ${{ env.RELEASE_VERSION }}
|
||||||
|
prerelease: false
|
||||||
|
files: oxmysql-${{ env.RELEASE_VERSION }}.zip
|
||||||
|
|
||||||
|
env:
|
||||||
|
CI: false
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@@ -117,4 +117,5 @@ dist
|
|||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
|
# FiveM
|
||||||
.yarn.installed
|
.yarn.installed
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"printWidth": 120,
|
||||||
|
"singleQuote": true,
|
||||||
|
"useTabs": false,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"semi": true,
|
||||||
|
"bracketSpacing": true,
|
||||||
|
"trailingComma": "es5"
|
||||||
|
}
|
||||||
+8
-2
@@ -3,9 +3,15 @@ game 'gta5'
|
|||||||
|
|
||||||
name 'oxmysql'
|
name 'oxmysql'
|
||||||
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
||||||
version '1.1.0'
|
version '1.3.3'
|
||||||
url 'https://github.com/overextended/oxmysql'
|
url 'https://github.com/overextended/oxmysql'
|
||||||
author 'overextended'
|
author 'overextended'
|
||||||
|
use_fxv2_oal 'yes'
|
||||||
|
lua54 'yes'
|
||||||
|
|
||||||
server_scripts {'oxmysql.js', 'wrapper.lua'}
|
|
||||||
server_only 'true'
|
server_only 'true'
|
||||||
|
|
||||||
|
server_scripts {
|
||||||
|
'oxmysql.js',
|
||||||
|
'wrapper.lua'
|
||||||
|
}
|
||||||
-109
@@ -1,109 +0,0 @@
|
|||||||
/// <reference path="node_modules\@citizenfx\server\index.d.ts" />
|
|
||||||
const { createPool, format, escape } = require("mysql2/promise");
|
|
||||||
const { ConnectionStringParser } = require("connection-string-parser");
|
|
||||||
|
|
||||||
const connectionString = GetConvar("mysql_connection_string", "");
|
|
||||||
|
|
||||||
if (connectionString === "") throw new Error(`Set up mysql_connection_string in server.cfg`);
|
|
||||||
|
|
||||||
const connectionStringParser = new ConnectionStringParser({
|
|
||||||
scheme: "mysql",
|
|
||||||
hosts: [],
|
|
||||||
});
|
|
||||||
|
|
||||||
const config = connectionStringParser.parse(connectionString);
|
|
||||||
|
|
||||||
if (Object.keys(config).length === 0)
|
|
||||||
throw new Error(
|
|
||||||
`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`
|
|
||||||
);
|
|
||||||
|
|
||||||
const slowQueryWarning = GetConvarInt("mysql_slow_query_warning", 100);
|
|
||||||
|
|
||||||
const debug = GetConvar("mysql_debug", "false") === "true";
|
|
||||||
|
|
||||||
const pool = createPool({
|
|
||||||
host: config.hosts[0].host,
|
|
||||||
user: config.username,
|
|
||||||
password: config.password,
|
|
||||||
database: config.endpoint,
|
|
||||||
charset: "utf8mb4_unicode_ci",
|
|
||||||
namedPlaceholders: true,
|
|
||||||
...config.options,
|
|
||||||
});
|
|
||||||
|
|
||||||
pool
|
|
||||||
.getConnection()
|
|
||||||
.then(() => {
|
|
||||||
console.info("Database server connection established.");
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error(error.message);
|
|
||||||
});
|
|
||||||
|
|
||||||
const formatQuery = (query, params) => {
|
|
||||||
if (!Array.isArray(params)) {
|
|
||||||
query = query.replace(/@(\w+)/g, (raw, match) => `:${match}`);
|
|
||||||
|
|
||||||
let newParams = {};
|
|
||||||
Object.keys(params).forEach((param) => {
|
|
||||||
newParams[param.replace("@", "")] = params[param];
|
|
||||||
});
|
|
||||||
|
|
||||||
params = newParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [query, params];
|
|
||||||
};
|
|
||||||
|
|
||||||
const execute = async (query, parameters) => {
|
|
||||||
ScheduleResourceTick(GetCurrentResourceName());
|
|
||||||
try {
|
|
||||||
const [formattedQuery, formattedParameters] = formatQuery(query, parameters);
|
|
||||||
|
|
||||||
const startTime = process.hrtime.bigint();
|
|
||||||
const [result] = await pool.execute(formattedQuery, formattedParameters);
|
|
||||||
const executionTime = Number(process.hrtime.bigint() - startTime) / 1000000;
|
|
||||||
|
|
||||||
if (executionTime >= slowQueryWarning)
|
|
||||||
console.warn(`${formattedQuery} took ${executionTime}ms!`, formattedParameters);
|
|
||||||
else if (debug) console.log(`${formattedQuery} took ${executionTime}ms!`, formattedParameters);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
|
||||||
if (debug) {
|
|
||||||
console.error(query);
|
|
||||||
console.error(parameters);
|
|
||||||
}
|
|
||||||
return console.error(error.message);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
global.exports("execute", (query, parameters, callback = () => {}) => {
|
|
||||||
execute(query, parameters).then(
|
|
||||||
(result) => typeof callback === "function" && callback(result && result.affectedRows)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
global.exports("fetch", (query, parameters, callback = () => {}) => {
|
|
||||||
execute(query, parameters).then((result) => typeof callback === "function" && callback(result));
|
|
||||||
});
|
|
||||||
|
|
||||||
global.exports("single", (query, parameters, callback = () => {}) => {
|
|
||||||
execute(query, parameters).then(
|
|
||||||
(result) => typeof callback === "function" && callback(result && result[0])
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
global.exports("scalar", (query, parameters, callback = () => {}) => {
|
|
||||||
execute(query, parameters).then(
|
|
||||||
(result) =>
|
|
||||||
typeof callback === "function" && callback(result && result[0] && Object.values(result[0])[0])
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
global.exports("insert", (query, parameters, callback = () => {}) => {
|
|
||||||
execute(query, parameters).then(
|
|
||||||
(result) => typeof callback === "function" && callback(result && result.insertId)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
+16
-3
@@ -1,15 +1,28 @@
|
|||||||
{
|
{
|
||||||
"name": "oxmysql",
|
"name": "oxmysql",
|
||||||
"private": "true",
|
"version": "1.3.2",
|
||||||
"version": "1.1.0",
|
|
||||||
"main": "oxmysql.js",
|
"main": "oxmysql.js",
|
||||||
"description": "FiveM MySQL wrapper using faster mysql2",
|
"description": "FiveM MySQL wrapper using faster mysql2",
|
||||||
"repository": "[email protected]:overextended/oxmysql.git",
|
"repository": "[email protected]:overextended/oxmysql.git",
|
||||||
"author": "dunak-debug <[email protected]>",
|
"author": "dunak-debug <[email protected]>",
|
||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
|
"scripts": {
|
||||||
|
"build": "webpack",
|
||||||
|
"watch": "webpack --watch",
|
||||||
|
"postinstall": "patch-package"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@citizenfx/server": "^2.0.4376-1",
|
"@citizenfx/server": "^2.0.4376-1",
|
||||||
"connection-string-parser": "^1.0.4",
|
"connection-string-parser": "^1.0.4",
|
||||||
"mysql2": "^2.3.0"
|
"mysql2": "^2.3.0",
|
||||||
|
"postinstall-postinstall": "^2.1.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"copy-webpack-plugin": "^9.0.1",
|
||||||
|
"patch-package": "^6.4.7",
|
||||||
|
"ts-loader": "^9.2.5",
|
||||||
|
"typescript": "^4.4.3",
|
||||||
|
"webpack": "^5.52.1",
|
||||||
|
"webpack-cli": "^4.8.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
diff --git a/node_modules/named-placeholders/index.js b/node_modules/named-placeholders/index.js
|
||||||
|
index 527d723..f47b591 100644
|
||||||
|
--- a/node_modules/named-placeholders/index.js
|
||||||
|
+++ b/node_modules/named-placeholders/index.js
|
||||||
|
@@ -3,7 +3,7 @@
|
||||||
|
// based on code from Brian White @mscdex mariasql library - https://github.com/mscdex/node-mariasql/blob/master/lib/Client.js#L272-L332
|
||||||
|
// License: https://github.com/mscdex/node-mariasql/blob/master/LICENSE
|
||||||
|
|
||||||
|
-const RE_PARAM = /(?:\?)|(?::(\d+|(?:[a-zA-Z][a-zA-Z0-9_]*)))/g,
|
||||||
|
+const RE_PARAM = /(?:\?)|(?:[:,@](\d+|(?:[a-zA-Z][a-zA-Z0-9_]*)))/g,
|
||||||
|
DQUOTE = 34,
|
||||||
|
SQUOTE = 39,
|
||||||
|
BSLASH = 92;
|
||||||
|
@@ -94,15 +94,24 @@ function createCompiler(config) {
|
||||||
|
if (typeof params == 'undefined')
|
||||||
|
throw new Error('Named query contains placeholders, but parameters object is undefined');
|
||||||
|
|
||||||
|
+ for(const key in params) {
|
||||||
|
+ if(key.charAt(0) === '@' || key.charAt(0) === ':') {
|
||||||
|
+ params[key.substring(1)] = params[key];
|
||||||
|
+ delete params[key];
|
||||||
|
+ }
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
const tokens = tree[1];
|
||||||
|
for (let i=0; i < tokens.length; ++i) {
|
||||||
|
+ if(params[tokens[i]] === undefined) throw new Error(`Undefined parameter ${tokens[i]}`);
|
||||||
|
arr.push(params[tokens[i]]);
|
||||||
|
}
|
||||||
|
+
|
||||||
|
return [tree[0], arr];
|
||||||
|
}
|
||||||
|
|
||||||
|
function noTailingSemicolon(s) {
|
||||||
|
- if (s.slice(-1) == ':') {
|
||||||
|
+ if (s.slice(-1) == ':' || s.slice(-1) == '@') {
|
||||||
|
return s.slice(0, -1);
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
@@ -115,7 +124,7 @@ function createCompiler(config) {
|
||||||
|
|
||||||
|
let unnamed = noTailingSemicolon(tree[0][0]);
|
||||||
|
for (let i=1; i < tree[0].length; ++i) {
|
||||||
|
- if (tree[0][i-1].slice(-1) == ':') {
|
||||||
|
+ if (tree[0][i-1].slice(-1) == ':' || tree[0][i-1].slice(-1) == '@') {
|
||||||
|
unnamed += config.placeholder;
|
||||||
|
}
|
||||||
|
unnamed += config.placeholder;
|
||||||
|
@@ -124,7 +133,7 @@ function createCompiler(config) {
|
||||||
|
|
||||||
|
const last = tree[0][tree[0].length -1];
|
||||||
|
if (tree[0].length == tree[1].length) {
|
||||||
|
- if (last.slice(-1) == ':') {
|
||||||
|
+ if (last.slice(-1) == ':' || last.slice(-1) == '@') {
|
||||||
|
unnamed += config.placeholder;
|
||||||
|
}
|
||||||
|
unnamed += config.placeholder;
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { ConnectionStringParser } from 'connection-string-parser';
|
||||||
|
|
||||||
|
const connectionString = GetConvar('mysql_connection_string', '');
|
||||||
|
|
||||||
|
if (connectionString === '') throw new Error(`Set up mysql_connection_string in server.cfg`);
|
||||||
|
|
||||||
|
const config = new ConnectionStringParser({
|
||||||
|
scheme: 'mysql',
|
||||||
|
hosts: [],
|
||||||
|
}).parse(connectionString);
|
||||||
|
|
||||||
|
if (Object.keys(config).length === 0)
|
||||||
|
throw new Error(`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`);
|
||||||
|
|
||||||
|
const slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 100);
|
||||||
|
const debug = GetConvar('mysql_debug', 'false') === 'true';
|
||||||
|
|
||||||
|
export { config, slowQueryWarning, debug };
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
class FormatError extends Error {
|
||||||
|
constructor(message, query, parameters) {
|
||||||
|
super(message);
|
||||||
|
if (parameters) this.sql = `${query} ${JSON.stringify(parameters)}`;
|
||||||
|
else this.sql = query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { FormatError };
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import { pool } from './pool';
|
||||||
|
import { parseParameters } from './parser';
|
||||||
|
import { slowQueryWarning, debug } from './config';
|
||||||
|
|
||||||
|
const resourceName = GetCurrentResourceName() || 'oxmysql';
|
||||||
|
|
||||||
|
const execute = async (query, parameters, resource) => {
|
||||||
|
ScheduleResourceTick(resourceName);
|
||||||
|
try {
|
||||||
|
const time = debug ? process.hrtime.bigint() : Date.now();
|
||||||
|
|
||||||
|
[query, parameters] = parseParameters(query, parameters);
|
||||||
|
|
||||||
|
const [result] = await pool.query(query, parameters);
|
||||||
|
|
||||||
|
const executionTime = debug ? Number(process.hrtime.bigint() - time) / 1e6 : Date.now() - time;
|
||||||
|
|
||||||
|
if (executionTime >= slowQueryWarning || debug)
|
||||||
|
console.log(
|
||||||
|
`^3[${debug ? 'DEBUG' : 'WARNING'}] ${resource} took ${executionTime}ms to execute a query!
|
||||||
|
${query} ${JSON.stringify(parameters)}^0`
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(
|
||||||
|
`^1[ERROR] ${resource} was unable to execute a query!
|
||||||
|
${error.message}
|
||||||
|
${error.sql || `${query} ${JSON.stringify(parameters)}`}^0`
|
||||||
|
);
|
||||||
|
debug && console.trace(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { execute };
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
import { pool } from './pool';
|
||||||
|
import { execute } from './execute';
|
||||||
|
|
||||||
|
setImmediate(async () => {
|
||||||
|
try {
|
||||||
|
await (await pool.getConnection()).ping();
|
||||||
|
console.log(`^2Database server connection established!^0`);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`^3Unable to establish a connection to the database! [${error.code}]\n${error.message}^0`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const safeCallback = (callback, result, resource, query) => {
|
||||||
|
if (typeof callback === 'function')
|
||||||
|
return callback(result || false);
|
||||||
|
else if (callback)
|
||||||
|
return console.log(`^3[WARNING] ${resource} executed a query, but no callback function was defined!\n ^3 ${query}^0`);
|
||||||
|
};
|
||||||
|
|
||||||
|
global.exports('execute', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
|
execute(query, parameters, resource).then((result) =>
|
||||||
|
safeCallback(cb, result, resource, query));
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('insert', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
|
execute(query, parameters, resource).then((result) =>
|
||||||
|
safeCallback(cb, result && result.insertId, resource, query));
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('update', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
|
execute(query, parameters, resource).then((result) =>
|
||||||
|
safeCallback(cb, result && result.affectedRows, resource, query));
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('fetch', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
|
execute(query, parameters, resource).then((result) =>
|
||||||
|
safeCallback(cb, result, resource, query));
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('single', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
|
execute(query, parameters, resource).then((result) =>
|
||||||
|
safeCallback(cb, result && result[0], resource, query));
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('scalar', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
|
execute(query, parameters, resource).then((result) =>
|
||||||
|
safeCallback(cb, result && result[0] && Object.values(result[0])[0], resource, query));
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
|
||||||
|
global.exports('executeSync', async (query, parameters) => {
|
||||||
|
const result = await execute(query, parameters, GetInvokingResource());
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('insertSync', async (query, parameters) => {
|
||||||
|
const result = await execute(query, parameters, GetInvokingResource());
|
||||||
|
return result && result.insertId;
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('updateSync', async (query, parameters) => {
|
||||||
|
const result = await execute(query, parameters, GetInvokingResource());
|
||||||
|
return result && result.affectedRows;
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('fetchSync', async (query, parameters) => {
|
||||||
|
const result = await execute(query, parameters, GetInvokingResource());
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('singleSync', async (query, parameters) => {
|
||||||
|
const result = await execute(query, parameters, GetInvokingResource());
|
||||||
|
return result && result[0];
|
||||||
|
});
|
||||||
|
|
||||||
|
global.exports('scalarSync', async (query, parameters) => {
|
||||||
|
const result = await execute(query, parameters, GetInvokingResource());
|
||||||
|
return result && result[0] && Object.values(result[0])[0];
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { FormatError } from './errors';
|
||||||
|
import * as createCompiler from 'named-placeholders';
|
||||||
|
|
||||||
|
const convertNamedPlaceholders = createCompiler();
|
||||||
|
|
||||||
|
const parseTypes = (field, next) => {
|
||||||
|
//https://github.com/GHMatti/ghmattimysql/blob/37f1d2ae5c53f91782d168fe81fba80512d3c46d/packages/ghmattimysql/src/server/utility/typeCast.ts#L3
|
||||||
|
switch (field.type) {
|
||||||
|
case 'DATETIME':
|
||||||
|
case 'DATETIME2':
|
||||||
|
case 'TIMESTAMP':
|
||||||
|
case 'TIMESTAMP2':
|
||||||
|
case 'NEWDATE':
|
||||||
|
case 'DATE':
|
||||||
|
return field.type === 'DATE'
|
||||||
|
? new Date(field.string() + ' 00:00:00').getTime()
|
||||||
|
: new Date(field.string()).getTime();
|
||||||
|
case 'TINY':
|
||||||
|
if (field.length === 1) return field.string() === '1';
|
||||||
|
else return next();
|
||||||
|
case 'BIT':
|
||||||
|
return field.buffer()[0] === 1;
|
||||||
|
default:
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const parseParameters = (query, parameters) => {
|
||||||
|
if (query === undefined) throw new FormatError(`Undefined query passed`);
|
||||||
|
|
||||||
|
if (query.includes('@') || query.includes(':')) return [query, parameters];
|
||||||
|
|
||||||
|
const queryParams = query.match(/\?(?!\?)/g);
|
||||||
|
|
||||||
|
if (queryParams === null) return [query, []];
|
||||||
|
|
||||||
|
if (parameters === undefined)
|
||||||
|
throw new FormatError(`Placeholders was defined in query but no parameters provided!`, query);
|
||||||
|
|
||||||
|
if (Array.isArray(parameters)) {
|
||||||
|
if (parameters.length !== queryParams.length)
|
||||||
|
throw new Error(`Undefined array parameter #${parameters.length}`, query, parameters);
|
||||||
|
} else {
|
||||||
|
queryParams.forEach((_, i) => {
|
||||||
|
if (parameters[`${i + 1}`] === undefined) throw new FormatError(`Undefined object parameter #${i + 1}`, query, parameters);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return [query, parameters];
|
||||||
|
};
|
||||||
|
|
||||||
|
export { parseParameters, parseTypes };
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
import { createPool } from 'mysql2/promise';
|
||||||
|
import { config } from './config';
|
||||||
|
import { parseTypes } from './parser';
|
||||||
|
|
||||||
|
export const pool = createPool({
|
||||||
|
host: config.hosts[0].host || 'localhost',
|
||||||
|
user: config.username || 'root',
|
||||||
|
password: config.password || '',
|
||||||
|
database: config.endpoint || 'es_extended',
|
||||||
|
charset: 'utf8mb4_unicode_ci',
|
||||||
|
...config.options,
|
||||||
|
namedPlaceholders: true,
|
||||||
|
typeCast: parseTypes,
|
||||||
|
});
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
const path = require("path");
|
||||||
|
const { DefinePlugin, IgnorePlugin } = require("webpack");
|
||||||
|
const CopyPlugin = require("copy-webpack-plugin");
|
||||||
|
|
||||||
|
const filesToCopy = ["fxmanifest.lua", "wrapper.lua", "README.md", "LICENSE.md"];
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mode: "development",
|
||||||
|
devtool: "source-map",
|
||||||
|
entry: "./src/index.js",
|
||||||
|
resolve: {
|
||||||
|
extensions: [".js"],
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
new DefinePlugin({ "global.GENTLY": false }),
|
||||||
|
new IgnorePlugin({
|
||||||
|
resourceRegExp: /^cardinal$/,
|
||||||
|
contextRegExp: /./,
|
||||||
|
}),
|
||||||
|
new CopyPlugin({
|
||||||
|
patterns: filesToCopy,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
output: {
|
||||||
|
filename: "oxmysql.js",
|
||||||
|
path: path.resolve(__dirname, "dist"),
|
||||||
|
},
|
||||||
|
target: "node",
|
||||||
|
};
|
||||||
+38
-27
@@ -1,84 +1,95 @@
|
|||||||
---
|
---
|
||||||
-- Execute query and fetch affected rows
|
-- Execute unprepared query and fetch result
|
||||||
--
|
--
|
||||||
-- @param query tring
|
-- @param query string
|
||||||
-- @param parameters table
|
-- @param parameters table
|
||||||
--
|
--
|
||||||
-- @return table with result
|
-- @return table with result | false
|
||||||
--
|
--
|
||||||
local function executeSync(query, parameters)
|
exports('executeSync', function(query, parameters)
|
||||||
local p = promise.new()
|
local p = promise.new()
|
||||||
exports['oxmysql']:execute(query, parameters, function(result)
|
exports['oxmysql']:execute(query, parameters, function(result)
|
||||||
p:resolve(result)
|
p:resolve(result)
|
||||||
end)
|
end, GetInvokingResource())
|
||||||
return Citizen.Await(p)
|
return Citizen.Await(p)
|
||||||
end
|
end)
|
||||||
exports('executeSync', executeSync)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
-- Execute query and fetch all result
|
-- Execute query and fetch all result
|
||||||
--
|
--
|
||||||
-- @param query tring
|
-- @param query string
|
||||||
-- @param parameters table
|
-- @param parameters table
|
||||||
--
|
--
|
||||||
-- @return table with result
|
-- @return table with result
|
||||||
--
|
--
|
||||||
local function fetchSync(query, parameters)
|
exports('fetchSync', function(query, parameters)
|
||||||
local p = promise.new()
|
local p = promise.new()
|
||||||
exports['oxmysql']:fetch(query, parameters, function(result)
|
exports['oxmysql']:fetch(query, parameters, function(result)
|
||||||
p:resolve(result)
|
p:resolve(result)
|
||||||
end)
|
end, GetInvokingResource())
|
||||||
return Citizen.Await(p)
|
return Citizen.Await(p)
|
||||||
end
|
end)
|
||||||
exports('fetchSync', fetchSync)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
-- Execute query and fetch first row
|
-- Execute query and fetch first row
|
||||||
--
|
--
|
||||||
-- @param query tring
|
-- @param query string
|
||||||
-- @param parameters table
|
-- @param parameters table
|
||||||
--
|
--
|
||||||
-- @return table with result row
|
-- @return table with result row
|
||||||
--
|
--
|
||||||
local function singleSync(query, parameters)
|
exports('singleSync', function(query, parameters)
|
||||||
local p = promise.new()
|
local p = promise.new()
|
||||||
exports['oxmysql']:single(query, parameters, function(result)
|
exports['oxmysql']:single(query, parameters, function(result)
|
||||||
p:resolve(result)
|
p:resolve(result)
|
||||||
end)
|
end, GetInvokingResource())
|
||||||
return Citizen.Await(p)
|
return Citizen.Await(p)
|
||||||
end
|
end)
|
||||||
exports('singleSync', singleSync)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
-- Execute query and fetch first column of first row
|
-- Execute query and fetch first column of first row
|
||||||
--
|
--
|
||||||
-- @param query tring
|
-- @param query string
|
||||||
-- @param parameters table
|
-- @param parameters table
|
||||||
--
|
--
|
||||||
-- @return result
|
-- @return result
|
||||||
--
|
--
|
||||||
local function scalarSync(query, parameters)
|
exports('scalarSync', function(query, parameters)
|
||||||
local p = promise.new()
|
local p = promise.new()
|
||||||
exports['oxmysql']:scalar(query, parameters, function(result)
|
exports['oxmysql']:scalar(query, parameters, function(result)
|
||||||
p:resolve(result)
|
p:resolve(result)
|
||||||
end)
|
end, GetInvokingResource())
|
||||||
return Citizen.Await(p)
|
return Citizen.Await(p)
|
||||||
end
|
end)
|
||||||
exports('scalarSync', scalarSync)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
-- Insert data and return inserted id
|
-- Insert data and return inserted id
|
||||||
--
|
--
|
||||||
-- @param query tring
|
-- @param query string
|
||||||
-- @param parameters table
|
-- @param parameters table
|
||||||
--
|
--
|
||||||
-- @return insert data result
|
-- @return insert data result
|
||||||
--
|
--
|
||||||
local function insertSync(query, parameters)
|
exports('insertSync', function(query, parameters)
|
||||||
local p = promise.new()
|
local p = promise.new()
|
||||||
exports['oxmysql']:insert(query, parameters, function(result)
|
exports['oxmysql']:insert(query, parameters, function(result)
|
||||||
p:resolve(result)
|
p:resolve(result)
|
||||||
end)
|
end, GetInvokingResource())
|
||||||
return Citizen.Await(p)
|
return Citizen.Await(p)
|
||||||
end
|
end)
|
||||||
exports('insertSync', insertSync)
|
|
||||||
|
---
|
||||||
|
-- Update data and return affected rows
|
||||||
|
--
|
||||||
|
-- @param query string
|
||||||
|
-- @param parameters table
|
||||||
|
--
|
||||||
|
-- @return number affected rows
|
||||||
|
--
|
||||||
|
exports('updateSync', function(query, parameters)
|
||||||
|
local p = promise.new()
|
||||||
|
exports['oxmysql']:update(query, parameters, function(result)
|
||||||
|
p:resolve(result)
|
||||||
|
end, GetInvokingResource())
|
||||||
|
return Citizen.Await(p)
|
||||||
|
end)
|
||||||
Reference in new issue
Block a user