Update CRUD functions type defs
This commit is contained in:
@@ -3,15 +3,23 @@ import _ from "lodash";
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
|
||||
type Params<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
table: string;
|
||||
query?: ServerQueryParam<T>;
|
||||
type Params<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
> = {
|
||||
table: Table;
|
||||
query?: ServerQueryParam<Schema>;
|
||||
targetId?: number | string;
|
||||
};
|
||||
|
||||
export default async function DbDelete<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ table, query, targetId }: Params<T>): Promise<APIResponseObject> {
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
>({
|
||||
table,
|
||||
query,
|
||||
targetId,
|
||||
}: Params<Schema, Table>): Promise<APIResponseObject> {
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
|
||||
|
||||
@@ -2,14 +2,18 @@ import DbClient from ".";
|
||||
import type { APIResponseObject } from "../../types";
|
||||
import sqlInsertGenerator from "../../utils/sql-insert-generator";
|
||||
|
||||
type Params<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
table: string;
|
||||
data: T[];
|
||||
type Params<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
> = {
|
||||
table: Table;
|
||||
data: Schema[];
|
||||
};
|
||||
|
||||
export default async function DbInsert<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ table, data }: Params<T>): Promise<APIResponseObject> {
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
>({ table, data }: Params<Schema, Table>): Promise<APIResponseObject> {
|
||||
try {
|
||||
const finalData: { [k: string]: any }[] = data.map((d) => ({
|
||||
...d,
|
||||
|
||||
@@ -4,16 +4,25 @@ import _ from "lodash";
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
|
||||
type Params<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
query?: ServerQueryParam<T>;
|
||||
table: string;
|
||||
type Params<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
> = {
|
||||
query?: ServerQueryParam<Schema>;
|
||||
table: Table;
|
||||
count?: boolean;
|
||||
targetId?: number | string;
|
||||
};
|
||||
|
||||
export default async function DbSelect<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ table, query, count, targetId }: Params<T>): Promise<APIResponseObject<T>> {
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
>({
|
||||
table,
|
||||
query,
|
||||
count,
|
||||
targetId,
|
||||
}: Params<Schema, Table>): Promise<APIResponseObject<Schema>> {
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
|
||||
@@ -38,10 +47,10 @@ export default async function DbSelect<
|
||||
|
||||
const sql = mysql.format(sqlObj.string, sqlObj.values);
|
||||
|
||||
const res = DbClient.query<T, T[]>(sql);
|
||||
const res = DbClient.query<Schema, Schema[]>(sql);
|
||||
const batchRes = res.all();
|
||||
|
||||
let resp: APIResponseObject<T> = {
|
||||
let resp: APIResponseObject<Schema> = {
|
||||
success: Boolean(batchRes[0]),
|
||||
payload: batchRes,
|
||||
singleRes: batchRes[0],
|
||||
|
||||
@@ -3,16 +3,25 @@ import _ from "lodash";
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
|
||||
type Params<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
table: string;
|
||||
data: T;
|
||||
query?: ServerQueryParam<T>;
|
||||
type Params<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
> = {
|
||||
table: Table;
|
||||
data: Schema;
|
||||
query?: ServerQueryParam<Schema>;
|
||||
targetId?: number | string;
|
||||
};
|
||||
|
||||
export default async function DbUpdate<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ table, data, query, targetId }: Params<T>): Promise<APIResponseObject> {
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
>({
|
||||
table,
|
||||
data,
|
||||
query,
|
||||
targetId,
|
||||
}: Params<Schema, Table>): Promise<APIResponseObject> {
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user