Skip to content

Commit

Permalink
chore(lib): update types
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Jul 28, 2023
1 parent 924fd6a commit f59ca17
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 98 deletions.
165 changes: 86 additions & 79 deletions lib/MySQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,48 @@ type Query = string | number;
type Params = Record<string, unknown> | unknown[] | Function;
type Callback<T> = (result: T | null) => void;

type Transaction = Object[]
type Transaction =
| string[]
| [string, Params][]
| { query: string; values: Params }[]
| { query: string; parameters: Params }[];

interface Result {
[column: string | number]: any;
affectedRows?: number;
fieldCount?: number;
info?: string;
insertId?: number;
serverStatus?: number;
warningStatus?: number;
changedRows?: number;
[column: string | number]: any;
affectedRows?: number;
fieldCount?: number;
info?: string;
insertId?: number;
serverStatus?: number;
warningStatus?: number;
changedRows?: number;
}

interface Row {
[column: string | number]: unknown;
[column: string | number]: unknown;
}

interface OxMySQL {
store: (query: string) => void;
ready: (callback: () => void) => void;
query: <T = Result | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
single: <T = Row | null> (query: Query, params?: Params | Callback<Exclude<T, []>>, cb?: Callback<Exclude<T, []>>) => Promise<Exclude<T, []>>
scalar: <T = unknown | null> (query: Query, params?: Params | Callback<Exclude<T, []>>, cb?: Callback<Exclude<T, []>>) => Promise<Exclude<T, []>>
update: <T = number | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
insert: <T = number | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
prepare: <T = any> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
rawExecute: <T = Result | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
transaction: (query: Transaction, params?: Params | Callback<boolean>, cb?: Callback<boolean>) => Promise<boolean>
query: <T = Result | null>(query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>;
single: <T = Row | null>(
query: Query,
params?: Params | Callback<Exclude<T, []>>,
cb?: Callback<Exclude<T, []>>
) => Promise<Exclude<T, []>>;
scalar: <T = unknown | null>(
query: Query,
params?: Params | Callback<Exclude<T, []>>,
cb?: Callback<Exclude<T, []>>
) => Promise<Exclude<T, []>>;
update: <T = number | null>(query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>;
insert: <T = number | null>(query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>;
prepare: <T = any>(query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>;
rawExecute: <T = Result | null>(query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>;
transaction: (query: Transaction, params?: Params | Callback<boolean>, cb?: Callback<boolean>) => Promise<boolean>;
isReady: () => boolean;
awaitConnection: () => Promise<true>;
}

const QueryStore: string[] = [];
Expand All @@ -38,114 +52,107 @@ function assert(condition: boolean, message: string) {
if (!condition) throw new TypeError(message);
}

const safeArgs = (
query: Query | Transaction,
params?: any,
cb?: Function,
transaction?: true
) => {
if (typeof query === "number") query = QueryStore[query];
const safeArgs = (query: Query | Transaction, params?: any, cb?: Function, transaction?: true) => {
if (typeof query === 'number') query = QueryStore[query];

if (transaction) {
assert(
typeof query === "object",
`First argument expected object, recieved ${typeof query}`
);
assert(typeof query === 'object', `First argument expected object, recieved ${typeof query}`);
} 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) {
const paramType = typeof params;
assert(
paramType === "object" || paramType === "function",
paramType === 'object' || paramType === 'function',
`Second argument expected object or function, received ${paramType}`
);

if (!cb && paramType === "function") {
cb = params
params = undefined
if (!cb && paramType === 'function') {
cb = params;
params = undefined;
}
}

if (cb !== undefined)
assert(
typeof cb === "function",
`Third argument expected function, received ${typeof cb}`
);
if (cb !== undefined) assert(typeof cb === 'function', `Third argument expected function, received ${typeof cb}`);

return [query, params, cb];
};

const exp = global.exports.oxmysql;
const currentResourceName = GetCurrentResourceName()
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
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 = {
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);
},
ready(callback) {
setImmediate(async () => {
while (GetResourceState("oxmysql") !== "started")
await new Promise((resolve) => setTimeout(resolve, 50));
while (GetResourceState('oxmysql') !== 'started') await new Promise((resolve) => setTimeout(resolve, 50));
callback();
});
},
async query(query, params, cb) {
[query, params, cb] = safeArgs(query, params, cb)
const result = await execute('query', query, params)
return cb ? cb(result) : result
[query, params, cb] = safeArgs(query, params, cb);
const result = await execute('query', query, params);
return cb ? cb(result) : result;
},
async single(query, params, cb) {
[query, params, cb] = safeArgs(query, params, cb)
const result = await execute('single', query, params)
return cb ? cb(result) : result
[query, params, cb] = safeArgs(query, params, cb);
const result = await execute('single', query, params);
return cb ? cb(result) : result;
},
async scalar(query, params, cb) {
[query, params, cb] = safeArgs(query, params, cb)
const result = await execute('scalar', query, params)
return cb ? cb(result) : result
[query, params, cb] = safeArgs(query, params, cb);
const result = await execute('scalar', query, params);
return cb ? cb(result) : result;
},
async update(query, params, cb) {
[query, params, cb] = safeArgs(query, params, cb)
const result = await execute('update', query, params)
return cb ? cb(result) : result
[query, params, cb] = safeArgs(query, params, cb);
const result = await execute('update', query, params);
return cb ? cb(result) : result;
},
async insert(query, params, cb) {
[query, params, cb] = safeArgs(query, params, cb)
const result = await execute('insert', query, params)
return cb ? cb(result) : result
[query, params, cb] = safeArgs(query, params, cb);
const result = await execute('insert', query, params);
return cb ? cb(result) : result;
},
async prepare(query, params, cb) {
[query, params, cb] = safeArgs(query, params, cb)
const result = await execute('prepare', query, params)
return cb ? cb(result) : result
[query, params, cb] = safeArgs(query, params, cb);
const result = await execute('prepare', query, params);
return cb ? cb(result) : result;
},
async rawExecute(query, params, cb) {
[query, params, cb] = safeArgs(query, params, cb)
const result = await execute('rawExecute', query, params)
return cb ? cb(result) : result
[query, params, cb] = safeArgs(query, params, cb);
const result = await execute('rawExecute', query, params);
return cb ? cb(result) : result;
},
async transaction(query, params, cb) {
[query, params, cb] = safeArgs(query, params, cb, true)
const result = await execute('transaction', query, params)
return cb ? cb(result) : result
[query, params, cb] = safeArgs(query, params, cb, true);
const result = await execute('transaction', query, params);
return cb ? cb(result) : result;
},
isReady() {
return exp.isReady();
},
async awaitConnection() {
return await exp.awaitConnection();
},
};
62 changes: 44 additions & 18 deletions lib/define.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,112 +16,138 @@ MySQL = {}
---@alias MySQLColumn unknown
---@alias MySQLRow { [string]?: MySQLColumn }
---@alias MySQLQuery QueryResult | { [number]?: { [string]: MySQLColumn } }
---@alias MySQLParameters any[] | { [string]: any }
---@alias MySQLTransaction
---| string[]
---| { [1]: string, [2]: MySQLParameters }[]
---| { query: string, values: MySQLParameters }[]
---| { query: string, parameters: MySQLParameters }[]

---@param query string
---@param parameters? table | fun(result: number)
---@param parameters? MySQLParameters | fun(result: number)
---@param cb? fun(affectedRows: number)
function MySQL.update(query, parameters, cb) end

MySQL.Async.execute = MySQL.update

---@param query string
---@param parameters? table
---@param parameters? MySQLParameters
---@return number affectedRows
---@async
function MySQL.update.await(query, parameters) end

MySQL.Sync.execute = MySQL.update.await

---@param query string
---@param parameters? table | fun(result: MySQLQuery)
---@param parameters? MySQLParameters | fun(result: MySQLQuery)
---@param cb? fun(result: MySQLQuery)
function MySQL.query(query, parameters, cb) end

MySQL.Async.fetchAll = MySQL.query

---@param query string
---@param parameters? table
---@param parameters? MySQLParameters
---@return MySQLQuery result
---@async
function MySQL.query.await(query, parameters) end

MySQL.Sync.fetchAll = MySQL.query.await

---@param query string
---@param parameters? table | fun(column?: MySQLColumn)
---@param parameters? MySQLParameters | fun(column?: MySQLColumn)
---@param cb? fun(column?: MySQLColumn)
function MySQL.scalar(query, parameters, cb) end

MySQL.Async.fetchScalar = MySQL.scalar

---@param query string
---@param parameters? table | fun(row?: MySQLRow)
---@param parameters? MySQLParameters | fun(row?: MySQLRow)
---@param cb? fun(row?: MySQLRow)
function MySQL.single(query, parameters, cb) end

MySQL.Async.fetchSingle = MySQL.single

---@param query string
---@param parameters? table
---@param parameters? MySQLParameters
---@return table<string, unknown> | nil row
---@async
function MySQL.single.await(query, parameters) end

MySQL.Sync.fetchSingle = MySQL.single.await

---@param query string
---@param parameters? table
---@param parameters? MySQLParameters
---@return unknown | nil column
---@async
function MySQL.scalar.await(query, parameters) end

MySQL.Sync.fetchScalar = MySQL.scalar.await

---@param query string
---@param parameters? table | fun(insertId: number)
---@param parameters? MySQLParameters | fun(insertId: number)
---@param cb? fun(insertId: number)
function MySQL.insert(query, parameters, cb) end

MySQL.Async.insert = MySQL.insert

---@param query string
---@param parameters? table
---@param parameters? MySQLParameters
---@return number insertId
---@async
function MySQL.insert.await(query, parameters) end

MySQL.Sync.insert = MySQL.insert.await

---@param queries table
---@param parameters? table | fun(success: boolean)
---@param queries MySQLTransaction
---@param parameters? MySQLParameters | fun(success: boolean)
---@param cb? fun(success: boolean)
function MySQL.transaction(queries, parameters, cb) end

MySQL.Async.transaction = MySQL.transaction

---@param queries table
---@param parameters? table
---@param queries MySQLTransaction
---@param parameters? MySQLParameters
---@return boolean success
---@async
function MySQL.transaction.await(queries, parameters) end

MySQL.Sync.transaction = MySQL.transaction.await

---@param query string
---@param parameters? table
---@param parameters? MySQLParameters | fun(result?: MySQLColumn | MySQLRow | MySQLQuery)
---@param cb? fun(result?: MySQLColumn | MySQLRow | MySQLQuery)
function MySQL.prepare(query, parameters, cb) end

MySQL.Async.prepare = MySQL.prepare

---@param query string
---@param parameters? table
---@param parameters? MySQLParameters
---@return MySQLColumn | MySQLRow | MySQLQuery | nil result
---@async
function MySQL.prepare.await(query, parameters) end

MySQL.Sync.prepare = MySQL.prepare.await

---@param query string
---@param parameters? table
---@param parameters? MySQLParameters | fun(result?: MySQLQuery)
---@param cb? fun(result?: MySQLQuery)
function MySQL.rawExecute(query, parameters, cb) end

---@param query string
---@param parameters? table
---@param parameters? MySQLParameters
---@return MySQLQuery? result
---@async
function MySQL.rawExecute.await(query, parameters) end

---@param cb function
function MySQL.ready(cb) end

---@async
function MySQL.ready.await() end

---@return boolean
function MySQL.isReady() end

---@return true
---@async
function MySQL.awaitConnection() end
Loading

0 comments on commit f59ca17

Please sign in to comment.