Skip to content

Commit

Permalink
feat(lib): Introduce wrapper for typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
dunak-debug committed Dec 27, 2021
1 parent 0290ea1 commit f6b6803
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,8 @@ yarn.lock
.pnp.*

# FiveM
.yarn.installed
.yarn.installed

# Wrapper
lib/MySQL.js
lib/MySQL.d.ts
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 140,
"printWidth": 120,
"singleQuote": true,
"useTabs": false,
"tabWidth": 2,
Expand Down
105 changes: 105 additions & 0 deletions lib/MySQL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
type Query = string | number;
type Params = Record<string, unknown> | unknown[];
type Callback<T> = (result: T | null) => void;

type Transaction = {
query: Query;
} & ({ parameters?: Params } | { values?: Params });

const Wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const assert = (condition: boolean, message: string) => {
if (!condition) throw new TypeError(message);
};

const QueryStorage: string[] = [];

const safeArgs = (
query: Transaction[] | string[] | Query,
parameters?: Params,
cb?: Callback<any>,
transaction?: true
) => {
if (typeof query === 'number') query = QueryStorage[query];

if (transaction) assert(Array.isArray(query), `Transaction query expects an array, received ${typeof query}`);
else assert(typeof query === 'string', `Query expects a string, received ${typeof query}`);

if (parameters !== undefined)
assert(
typeof parameters === 'object' || Array.isArray(parameters),
`Params expects object or array, received ${typeof parameters}`
);

if (cb !== undefined) assert(typeof cb === 'function', `Callback expects function, received ${typeof cb}`);

return [query, parameters, cb];
};

function store(query: string) {
assert(typeof query !== 'string', `Query expects a string, received ${typeof query}`);

return QueryStorage.push(query);
}

function ready(callback: () => void) {
setImmediate(() => {
while (GetResourceState('oxmysql') !== 'started') Wait(50);
callback();
});
}

function query<T = any>(query: Query, parameters?: Params): Promise<T | null>;
function query<T = any>(query: Query, parameters?: Params, cb?: Callback<T>): void;
function query<T = any>(query: Query, parameters?: Params, cb?: Callback<T>): Promise<T | null> | void {
if (cb) return exports.oxmysql.query(...safeArgs(query, parameters, cb));
return exports.oxmysql.query_async(...safeArgs(query, parameters));
}

function single<T = Record<string | number, any>>(query: Query, parameters?: Params): Promise<T | null>;
function single<T = Record<string | number, any>>(query: Query, parameters?: Params, cb?: Callback<T>): void;
function single<T = Record<string | number, any>>(
query: Query,
parameters?: Params,
cb?: Callback<T>
): Promise<T | null> | void {
if (cb) return exports.oxmysql.single(...safeArgs(query, parameters, cb));
return exports.oxmysql.single_async(...safeArgs(query, parameters));
}

function scalar<T = unknown>(query: Query, parameters?: Params): Promise<T | null>;
function scalar<T = unknown>(query: Query, parameters?: Params, cb?: Callback<T>): void;
function scalar<T = unknown>(query: Query, parameters?: Params, cb?: Callback<T>): Promise<T | null> | void {
if (cb) return exports.oxmysql.scalar(...safeArgs(query, parameters, cb));
return exports.oxmysql.scalar_async(...safeArgs(query, parameters));
}

function update(query: Query, parameters?: Params): Promise<number | null>;
function update(query: Query, parameters?: Params, cb?: Callback<number>): void;
function update(query: Query, parameters?: Params, cb?: Callback<number>): Promise<number | null> | void {
if (cb) return exports.oxmysql.update(...safeArgs(query, parameters, cb));
return exports.oxmysql.update_async(...safeArgs(query, parameters));
}

function insert(query: Query, parameters?: Params): Promise<number | null>;
function insert(query: Query, parameters?: Params, cb?: Callback<number>): void;
function insert(query: Query, parameters?: Params, cb?: Callback<number>): Promise<number | null> | void {
if (cb) return exports.oxmysql.insert(...safeArgs(query, parameters, cb));
return exports.oxmysql.insert_async(...safeArgs(query, parameters));
}

function prepare<T = unknown>(query: Query, parameters?: Params): Promise<T | null>;
function prepare<T = unknown>(query: Query, parameters?: Params, cb?: Callback<T>): void;
function prepare<T = unknown>(query: Query, parameters?: Params, cb?: Callback<T>): Promise<T | null> | void {
if (cb) return exports.oxmysql.prepare(...safeArgs(query, parameters, cb));
return exports.oxmysql.prepare_async(...safeArgs(query, parameters));
}

function transaction(query: Query, parameters?: Params): Promise<boolean>;
function transaction(query: Query, parameters?: Params, cb?: Callback<boolean>): void;
function transaction(query: Query, parameters?: Params, cb?: Callback<boolean>): Promise<boolean> | void {
if (cb) return exports.oxmysql.transaction(...safeArgs(query, parameters, cb, true));
return exports.oxmysql.transaction_async(...safeArgs(query, parameters, undefined, true));
}

export { store, query, single, scalar, update, insert, transaction, prepare, ready };
export default { store, query, single, scalar, update, insert, transaction, prepare, ready };
10 changes: 10 additions & 0 deletions lib/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ESNext",
"moduleResolution": "node",
"types": ["@citizenfx/server", "@types/node"],
"declaration": true
},
"files": ["MySQL.ts"]
}
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{
"name": "oxmysql",
"version": "1.9.0",
"version": "1.0.0",
"description": "FXServer to MySQL communication via node-mysql2",
"repository": "git@github.com:overextended/oxmysql.git",
"author": "dunak-debug <19434539+dunak-debug@users.noreply.github.com>",
"license": "LGPL-3.0-or-later",
"type": "module",
"private": true,
"main": "lib/MySQL.js",
"types": "lib/MySQL.d.ts",
"files": [
"lib/MySQL.js",
"lib/MySQL.d.ts"
],
"scripts": {
"build": "esbuild --bundle --platform=node --target=node16.9.1 src/index.ts --outfile=dist/build.js",
"watch": "esbuild --watch --bundle --platform=node --target=node16.9.1 src/index.ts --outfile=dist/build.js",
"lib": "tsc --project lib/tsconfig.lib.json",
"postinstall": "patch-package"
},
"dependencies": {
Expand All @@ -21,6 +27,7 @@
"@types/node": "^17.0.4",
"esbuild": "^0.14.7",
"patch-package": "^6.4.7",
"postinstall-postinstall": "^2.1.0"
"postinstall-postinstall": "^2.1.0",
"typescript": "^4.5.4"
}
}

0 comments on commit f6b6803

Please sign in to comment.