mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 14:38:50 +00:00
refactor(lib/ts): improve typing and methods
This commit is contained in:
1 parent
e982bd7f89
commit
31212e0198
4 files changed
+123
-73
No files matched your search
+100
-66
@@ -1,111 +1,145 @@
|
|||||||
type Query = string | number;
|
type Query = string | number;
|
||||||
type Params = Record<string, unknown> | unknown[];
|
type Params = Record<string, unknown> | unknown[] | Function;
|
||||||
type Callback<T> = (result: T | null) => void;
|
type Callback<T> = (result: T | null) => void;
|
||||||
|
|
||||||
type Transaction = {
|
type Transaction = Object[]
|
||||||
query: Query;
|
|
||||||
} & ({ params?: Params } | { values?: Params });
|
interface Result {
|
||||||
|
[column: string | number]: any;
|
||||||
|
affectedRows?: number;
|
||||||
|
fieldCount?: number;
|
||||||
|
info?: string;
|
||||||
|
insertId?: number;
|
||||||
|
serverStatus?: number;
|
||||||
|
warningStatus?: number;
|
||||||
|
changedRows?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Row {
|
||||||
|
[column: string | number]: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
interface OxMySQL {
|
interface OxMySQL {
|
||||||
store: (query: string) => void;
|
store: (query: string) => void;
|
||||||
ready: (callback: () => void) => void;
|
ready: (callback: () => void) => void;
|
||||||
query:
|
query: <T = Result | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
|
||||||
| (<T = any>(query: Query, params?: Params, cb?: Callback<T>) => Promise<T | null>)
|
single: <T = Row | null> (query: Query, params?: Params | Callback<Exclude<T, []>>, cb?: Callback<Exclude<T, []>>) => Promise<Exclude<T, []>>
|
||||||
| (<T = any>(query: Query, params?: Params, cb?: Callback<T>) => void)
|
scalar: <T = unknown | null> (query: Query, params?: Params | Callback<Exclude<T, []>>, cb?: Callback<Exclude<T, []>>) => Promise<Exclude<T, []>>
|
||||||
| (<T = any>(query: Query, params?: Params, cb?: Callback<T>) => Promise<T | null> | void);
|
update: <T = number | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
|
||||||
single:
|
insert: <T = number | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
|
||||||
| (<T = Record<string | number, any>>(query: Query, params?: Params) => Promise<T | null>)
|
prepare: <T = any> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
|
||||||
| (<T = Record<string | number, any>>(query: Query, params?: Params, cb?: Callback<T>) => void)
|
transaction: (query: Transaction, params?: Params | Callback<boolean>, cb?: Callback<boolean>) => Promise<boolean>
|
||||||
| (<T = Record<string | number, any>>(query: Query, params?: Params, cb?: Callback<T>) => Promise<T | null | void>);
|
|
||||||
scalar:
|
|
||||||
| (<T = unknown>(query: Query, params?: Params) => Promise<T | null>)
|
|
||||||
| (<T = unknown>(query: Query, params?: Params, cb?: Callback<T>) => void)
|
|
||||||
| (<T = unknown>(query: Query, params?: Params, cb?: Callback<T>) => Promise<T | null> | void);
|
|
||||||
update:
|
|
||||||
| ((query: Query, params?: Params) => Promise<number | null>)
|
|
||||||
| ((query: Query, params?: Params, cb?: Callback<number>) => void)
|
|
||||||
| ((query: Query, params?: Params, cb?: Callback<number>) => Promise<number | null> | void);
|
|
||||||
insert:
|
|
||||||
| ((query: Query, params?: Params) => Promise<number | null>)
|
|
||||||
| ((query: Query, params?: Params, cb?: Callback<number>) => void)
|
|
||||||
| ((query: Query, params?: Params, cb?: Callback<number>) => Promise<number | null> | void);
|
|
||||||
prepare:
|
|
||||||
| (<T = unknown>(query: Query, params?: Params) => Promise<T | null>)
|
|
||||||
| (<T = unknown>(query: Query, params?: Params, cb?: Callback<T>) => void)
|
|
||||||
| (<T = unknown>(query: Query, params?: Params, cb?: Callback<T>) => Promise<T | null> | void);
|
|
||||||
transaction:
|
|
||||||
| ((query: Query, params?: Params) => Promise<boolean>)
|
|
||||||
| ((query: Query, params?: Params, cb?: Callback<boolean>) => void)
|
|
||||||
| ((query: Query, params?: Params, cb?: Callback<boolean>) => Promise<boolean> | void);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const QueryStore: string[] = [];
|
const QueryStore: string[] = [];
|
||||||
|
|
||||||
const Wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
function assert(condition: boolean, message: string) {
|
||||||
const assert = (condition: boolean, message: string) => {
|
|
||||||
if (!condition) throw new TypeError(message);
|
if (!condition) throw new TypeError(message);
|
||||||
};
|
}
|
||||||
|
|
||||||
const safeArgs = (query: Transaction[] | string[] | Query, params?: any, cb?: Callback<any>, transaction?: true) => {
|
const safeArgs = (
|
||||||
if (typeof query === 'number') query = QueryStore[query];
|
query: Query | Transaction,
|
||||||
|
params?: any,
|
||||||
|
cb?: Function,
|
||||||
|
transaction?: true
|
||||||
|
) => {
|
||||||
|
if (typeof query === "number") query = QueryStore[query];
|
||||||
|
|
||||||
if (transaction) {
|
if (transaction) {
|
||||||
assert(Array.isArray(query), `First argument expected array, recieved ${typeof query}`);
|
assert(
|
||||||
|
typeof query === "object",
|
||||||
|
`First argument expected object, recieved ${typeof query}`
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
assert(typeof query === 'string', `First argument expected string, received ${typeof query}`);
|
assert(
|
||||||
|
typeof query === "string",
|
||||||
|
`First argument expected string, received ${typeof query}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params) {
|
if (params) {
|
||||||
const paramType = typeof params;
|
const paramType = typeof params;
|
||||||
assert(
|
assert(
|
||||||
paramType === 'object' || paramType === 'function',
|
paramType === "object" || paramType === "function",
|
||||||
`Second argument expected table or function, received ${paramType}`
|
`Second argument expected object or function, received ${paramType}`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!cb && paramType === "function") {
|
||||||
|
cb = params
|
||||||
|
params = undefined
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cb !== undefined) assert(typeof cb === 'function', `Callback expects function, received ${typeof cb}`);
|
if (cb !== undefined)
|
||||||
|
assert(
|
||||||
|
typeof cb === "function",
|
||||||
|
`Third argument expected function, received ${typeof cb}`
|
||||||
|
);
|
||||||
|
|
||||||
return [query, params, cb];
|
return [query, params, cb];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const exp = exports.oxmysql;
|
||||||
|
const currentResourceName = GetCurrentResourceName()
|
||||||
|
|
||||||
|
function execute(method: string, query: Query | Transaction, params?: Params) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
exp[method](query, params, (result, error) => {
|
||||||
|
if (error) return reject(error)
|
||||||
|
resolve(result)
|
||||||
|
}, currentResourceName, true)
|
||||||
|
}) as any
|
||||||
|
}
|
||||||
|
|
||||||
export const oxmysql: OxMySQL = {
|
export const oxmysql: OxMySQL = {
|
||||||
store(query) {
|
store(query) {
|
||||||
assert(typeof query !== 'string', `Query expects a string, received ${typeof query}`);
|
assert(
|
||||||
|
typeof query !== "string",
|
||||||
|
`Query expects a string, received ${typeof query}`
|
||||||
|
);
|
||||||
|
|
||||||
return QueryStore.push(query);
|
return QueryStore.push(query);
|
||||||
},
|
},
|
||||||
ready(callback) {
|
ready(callback) {
|
||||||
setImmediate(() => {
|
setImmediate(async () => {
|
||||||
while (GetResourceState('oxmysql') !== 'started') Wait(50);
|
while (GetResourceState("oxmysql") !== "started")
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
query(query, params, cb) {
|
async query(query, params, cb) {
|
||||||
if (cb) return global.exports.oxmysql.query(...safeArgs(query, params, cb));
|
[query, params, cb] = safeArgs(query, params, cb)
|
||||||
return global.exports.oxmysql.query_async(...safeArgs(query, params));
|
const result = await execute('query', query, params)
|
||||||
|
return cb ? cb(result) : result
|
||||||
},
|
},
|
||||||
single(query, params, cb) {
|
async single(query, params, cb) {
|
||||||
if (cb) return global.exports.oxmysql.single(...safeArgs(query, params, cb));
|
[query, params, cb] = safeArgs(query, params, cb)
|
||||||
return global.exports.oxmysql.single_async(...safeArgs(query, params));
|
const result = await execute('single', query, params)
|
||||||
|
return cb ? cb(result) : result
|
||||||
},
|
},
|
||||||
scalar(query, params, cb) {
|
async scalar(query, params, cb) {
|
||||||
if (cb) return global.exports.oxmysql.scalar(...safeArgs(query, params, cb));
|
[query, params, cb] = safeArgs(query, params, cb)
|
||||||
return global.exports.oxmysql.scalar_async(...safeArgs(query, params));
|
const result = await execute('scalar', query, params)
|
||||||
|
return cb ? cb(result) : result
|
||||||
},
|
},
|
||||||
update(query, params, cb) {
|
async update(query, params, cb) {
|
||||||
if (cb) return global.exports.oxmysql.update(...safeArgs(query, params, cb));
|
[query, params, cb] = safeArgs(query, params, cb)
|
||||||
return global.exports.oxmysql.update_async(...safeArgs(query, params));
|
const result = await execute('update', query, params)
|
||||||
|
return cb ? cb(result) : result
|
||||||
},
|
},
|
||||||
insert(query, params, cb) {
|
async insert(query, params, cb) {
|
||||||
if (cb) return global.exports.oxmysql.insert(...safeArgs(query, params, cb));
|
[query, params, cb] = safeArgs(query, params, cb)
|
||||||
return global.exports.oxmysql.insert_async(...safeArgs(query, params));
|
const result = await execute('insert', query, params)
|
||||||
|
return cb ? cb(result) : result
|
||||||
},
|
},
|
||||||
prepare(query, params, cb) {
|
async prepare(query, params, cb) {
|
||||||
if (cb) return global.exports.oxmysql.prepare(...safeArgs(query, params, cb));
|
[query, params, cb] = safeArgs(query, params, cb)
|
||||||
return global.exports.oxmysql.prepare_async(...safeArgs(query, params));
|
const result = await execute('prepare', query, params)
|
||||||
|
return cb ? cb(result) : result
|
||||||
},
|
},
|
||||||
transaction(query, params, cb) {
|
async transaction(query, params, cb) {
|
||||||
if (cb) return global.exports.oxmysql.transaction(...safeArgs(query, params, cb, true));
|
[query, params, cb] = safeArgs(query, params, cb, true)
|
||||||
return global.exports.oxmysql.transaction_async(...safeArgs(query, params, undefined, true));
|
const result = await execute('transaction', query, params)
|
||||||
|
return cb ? cb(result) : result
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
+21
-5
@@ -1,4 +1,6 @@
|
|||||||
# OxMySQL exports wrapper package for Node.js
|
# OxMySQL exports wrapper for FiveM
|
||||||
|
|
||||||
|
Types are fully supported and you will get intellisense on the `oxmysql` object when using it.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -16,11 +18,13 @@ npm install @overextended/oxmysql
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Import as module:
|
Import as module:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { oxmysql } from '@overextended/oxmysql'
|
import { oxmysql } from '@overextended/oxmysql';
|
||||||
```
|
```
|
||||||
|
|
||||||
Import with require:
|
Import with require:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const { oxmysql } = require('@overextended/oxmysql');
|
const { oxmysql } = require('@overextended/oxmysql');
|
||||||
```
|
```
|
||||||
@@ -29,7 +33,19 @@ const { oxmysql } = require('@overextended/oxmysql');
|
|||||||
|
|
||||||
[View documentation](https://overextended.github.io/docs/oxmysql)
|
[View documentation](https://overextended.github.io/docs/oxmysql)
|
||||||
|
|
||||||
## License
|
```js
|
||||||
LGPL-3.0
|
MySQL.scalar('SELECT username FROM uses', (result) => {
|
||||||
|
console.log(result)
|
||||||
|
}).catch(console.error)
|
||||||
|
|
||||||
Types are fully supported and you will get intellisense on the `oxmysql` object when using it.
|
MySQL.scalar('SELECT username FROM uses').then((result) => {
|
||||||
|
console.log(result)
|
||||||
|
}).catch(console.error)
|
||||||
|
|
||||||
|
const result = await MySQL.scalar('SELECT username FROM uses').catch(console.error)
|
||||||
|
console.log(result)
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
LGPL-3.0
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@overextended/oxmysql",
|
"name": "@overextended/oxmysql",
|
||||||
"version": "1.0.2",
|
"version": "1.1.0",
|
||||||
"description": "Exports wrapper for oxmysql",
|
"description": "Exports wrapper for oxmysql",
|
||||||
"types": "MySQL.d.ts",
|
"types": "MySQL.d.ts",
|
||||||
"main": "MySQL.js",
|
"main": "MySQL.js",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "oxmysql",
|
"name": "oxmysql",
|
||||||
"version": "2.3.1",
|
"version": "2.3.4",
|
||||||
"description": "FXServer to MySQL communication via node-mysql2",
|
"description": "FXServer to MySQL communication via node-mysql2",
|
||||||
"repository": "[email protected]:overextended/oxmysql.git",
|
"repository": "[email protected]:overextended/oxmysql.git",
|
||||||
"author": "dunak-debug <[email protected]>",
|
"author": "dunak-debug <[email protected]>",
|
||||||
|
|||||||
Reference in new issue
Block a user