From 779ecca76f17001edff3e7e726b829472550fa69 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sat, 30 Dec 2023 17:32:04 +1100 Subject: [PATCH] feat: provide additional compatibility exports --- src/compatibility/ghmattimysql.ts | 6 ++++ src/compatibility/mysql-async.ts | 8 ++++++ src/index.ts | 48 +++++++++++++++++++------------ 3 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 src/compatibility/ghmattimysql.ts create mode 100644 src/compatibility/mysql-async.ts diff --git a/src/compatibility/ghmattimysql.ts b/src/compatibility/ghmattimysql.ts new file mode 100644 index 0000000..983e441 --- /dev/null +++ b/src/compatibility/ghmattimysql.ts @@ -0,0 +1,6 @@ +export default { + query: 'execute', + scalar: 'scalar', + transaction: 'transaction', + store: 'store', +}; diff --git a/src/compatibility/mysql-async.ts b/src/compatibility/mysql-async.ts new file mode 100644 index 0000000..2d162e5 --- /dev/null +++ b/src/compatibility/mysql-async.ts @@ -0,0 +1,8 @@ +export default { + update: 'mysql_execute', + insert: 'mysql_insert', + query: 'mysql_fetch_all', + scalar: 'mysql_fetch_scalar', + transaction: 'mysql_transaction', + store: 'mysql_store', +}; diff --git a/src/index.ts b/src/index.ts index 5a419a7..692068b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -93,30 +93,26 @@ MySQL.rawExecute = ( rawExecute(invokingResource, query, parameters, cb, isPromise); }; +// provide the store export for compatibility (ghmatti/mysql-async); simply returns the query as-is +MySQL.store = (query: string, cb: Function) => { + cb(query); +}; + +// deprecated export names MySQL.execute = MySQL.query; MySQL.fetch = MySQL.query; -function provide(name: string, cb: Function, sync: Function) { - on(`__cfx_export_ghmattimysql_${name}`, (setCb: Function) => setCb(cb)); - on(`__cfx_export_ghmattimysql_${name}Sync`, (setCb: Function) => setCb(sync)); +function provide(resourceName: string, method: string, cb: Function) { + on(`__cfx_export_${resourceName}_${method}`, (setCb: Function) => setCb(cb)); } -// provide the "store" and "storeSync" exports, to provide compatibility for ghmattimysql -// these are not actually used to do anything, simply returning the query as-is -provide( - 'store', - (query: string, cb: Function) => { - cb(query); - }, - (query: string) => { - return query; - } -); +import ghmatti from './compatibility/ghmattimysql'; +import mysqlAsync from './compatibility/mysql-async'; for (const key in MySQL) { - global.exports(key, MySQL[key]); + const exp = MySQL[key]; - const exp = (query: string, parameters: CFXParameters, invokingResource = GetInvokingResource()) => { + const async_exp = (query: string, parameters: CFXParameters, invokingResource = GetInvokingResource()) => { return new Promise((resolve, reject) => { MySQL[key]( query, @@ -131,8 +127,22 @@ for (const key in MySQL) { }); }; - global.exports(`${key}_async`, exp); - global.exports(`${key}Sync`, exp); + global.exports(key, exp); + // async_retval + global.exports(`${key}_async`, async_exp); + // deprecated aliases for async_retval + global.exports(`${key}Sync`, async_exp); - if (key === 'execute' || key === 'scalar' || key === 'transaction') provide(key, MySQL[key], exp); + let alias = (ghmatti as any)[key]; + + if (alias) { + provide('ghmattimysql', alias, exp); + provide('ghmattimysql', `${alias}Sync`, async_exp); + } + + alias = (mysqlAsync as any)[key]; + + if (alias) { + provide('mysql-async', alias, exp); + } }