Skip to content

Commit

Permalink
feat: provide additional compatibility exports
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Dec 30, 2023
1 parent e31d840 commit 779ecca
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
6 changes: 6 additions & 0 deletions src/compatibility/ghmattimysql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
query: 'execute',
scalar: 'scalar',
transaction: 'transaction',
store: 'store',
};
8 changes: 8 additions & 0 deletions src/compatibility/mysql-async.ts
Original file line number Diff line number Diff line change
@@ -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',
};
48 changes: 29 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

let alias = (ghmatti as any)[key];

if (key === 'execute' || key === 'scalar' || key === 'transaction') provide(key, MySQL[key], exp);
if (alias) {
provide('ghmattimysql', alias, exp);
provide('ghmattimysql', `${alias}Sync`, async_exp);
}

alias = (mysqlAsync as any)[key];

if (alias) {
provide('mysql-async', alias, exp);
}
}

0 comments on commit 779ecca

Please sign in to comment.