Skip to content

Commit

Permalink
refactor: minor cleanup and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Nov 30, 2021
1 parent dbe107e commit e685ff5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 57 deletions.
30 changes: 11 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,34 @@ setImmediate(async () => {
}
});

const safeCallback = (callback, result, resource, query) => {
if (typeof callback === 'function')
return callback(result);
else if (debug)
return console.log(`^3[WARNING] ${resource} executed a query, but no callback function was defined!\n ^3 ${query}^0`);
const safeCallback = (callback, result) => {
if (typeof callback === 'function') callback(result);
};

global.exports('execute', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb || parameters, result, resource, query));
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result));
});

global.exports('insert', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb || parameters, result && result.insertId, resource, query));
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result && result.insertId));
});

global.exports('update', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb || parameters, result && result.affectedRows, resource, query));
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result && result.affectedRows));
});

global.exports('fetch', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb || parameters, result, resource, query));
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result));
});

global.exports('single', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb || parameters, result && result[0], resource, query));
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result && result[0]));
});

global.exports('scalar', (query, parameters, cb, resource = GetInvokingResource()) => {
execute(query, parameters, resource).then((result) =>
safeCallback(cb || parameters, result && result[0] && Object.values(result[0])[0], resource, query));
safeCallback(cb || parameters, result && result[0] && Object.values(result[0])[0])
);
});

global.exports('transaction', (queries, parameters, cb, resource = GetInvokingResource()) => {
Expand All @@ -56,8 +49,7 @@ global.exports('transaction', (queries, parameters, cb, resource = GetInvokingRe
});

global.exports('prepare', (query, parameters, cb, resource = GetInvokingResource()) => {
preparedStatement(query, parameters, resource).then((result) =>
safeCallback(cb || parameters, result, resource, query))
preparedStatement(query, parameters, resource).then((result) => safeCallback(cb || parameters, result));
});

if (GetEntityAttachedTo || !GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
Expand Down Expand Up @@ -100,4 +92,4 @@ if (GetEntityAttachedTo || !GetResourceMetadata(GetCurrentResourceName(), 'serve
const result = await transaction(queries, parameters, GetInvokingResource());
return result;
});
}
}
38 changes: 21 additions & 17 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import * as createCompiler from 'named-placeholders';

const convertNamedPlaceholders = createCompiler();

// DATE compatibility with mysql-async
// https://github.com/brouznouf/fivem-mysql-async/blob/master/mysql-async.js#L15499-L15536
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':
Expand All @@ -16,8 +17,7 @@ const parseTypes = (field, next) => {
? 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();
return field.length === 1 ? field.string() === '1' : next();
case 'BIT':
return field.buffer()[0] === 1;
default:
Expand All @@ -26,24 +26,28 @@ const parseTypes = (field, next) => {
};

const parseParameters = (query, parameters) => {
if (typeof query !== 'string')
throw new Error(`Non-string query passed. Make sure you use "oxmysql:execute" instead of "oxmysql.execute" when calling exports.`);
if (typeof query !== 'string')
throw new Error(
`Non-string query passed. Make sure you use "oxmysql:execute" instead of "oxmysql.execute" when calling exports.`
);

if (!parameters || typeof parameters === 'function') return [query, []];

if (query.includes('@') || query.includes(':')) {
const obj = parameters.length !== 0 ? parameters : (() => {
let obj = {};
const [_, paramNames] = convertNamedPlaceholders.parse(query);
if (paramNames) {
for (let i = 0; i < paramNames.length; i++)
obj[paramNames[i]] = null;
}
return obj;
})();

return [query, obj]
};
const obj =
parameters.length !== 0
? parameters
: (() => {
let obj = {};
const [_, paramNames] = convertNamedPlaceholders.parse(query);
if (paramNames) {
for (let i = 0; i < paramNames.length; i++) obj[paramNames[i]] = null;
}
return obj;
})();

return [query, obj];
}

const queryParams = query.match(/\?(?!\?)/g);

Expand Down
38 changes: 17 additions & 21 deletions src/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,24 @@ if (connectionString === '') {
}

const dbOptions = (() => {
if (connectionString.includes('mysql://')) return { uri: connectionString };
const options = connectionString
.replace(/(?:host(?:name)|ip|server|data\s?source|addr(?:ess)?)=/gi, 'host=')
.replace(/(?:user\s?(?:id|name)?|uid)=/gi, 'user=')
.replace(/(?:pwd|pass)=/gi, 'password=')
.replace(/(?:db)=/gi, 'database=')
.split(';')
.reduce((connectionInfo, parameter) => {
const [key, value] = parameter.split('=');
connectionInfo[key] = value;
return connectionInfo;
}, {});
const options = connectionString.includes('mysql://')
? { uri: connectionString }
: (options = connectionString
.replace(/(?:host(?:name)|ip|server|data\s?source|addr(?:ess)?)=/gi, 'host=')
.replace(/(?:user\s?(?:id|name)?|uid)=/gi, 'user=')
.replace(/(?:pwd|pass)=/gi, 'password=')
.replace(/(?:db)=/gi, 'database=')
.split(';')
.reduce((connectionInfo, parameter) => {
const [key, value] = parameter.split('=');
connectionInfo[key] = value;
return connectionInfo;
}, {}));

options.namedPlaceholders = true;
options.typeCast = parseTypes;

return options;
})();

const createConnection = () => {
return createPool({
...dbOptions,
namedPlaceholders: true,
typeCast: parseTypes,
});
};

export const pool = createConnection();
export const pool = createPool(dbOptions);

0 comments on commit e685ff5

Please sign in to comment.