Updates
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import path from "path";
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import { DsqlCrudQueryObject, SQLDeleteData } from "../../types";
|
||||
import grabAPIBasePath from "../../utils/grab-api-base-path";
|
||||
|
||||
type Params<T extends { [key: string]: any } = { [key: string]: any }> = {
|
||||
dbName: string;
|
||||
tableName: string;
|
||||
deleteSpec?: T & { deleteKeyValues?: SQLDeleteData<T>[] };
|
||||
targetID?: string | number;
|
||||
};
|
||||
|
||||
export default async function apiCrudDELETE<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({ dbName, tableName, deleteSpec, targetID }: Params<T>) {
|
||||
const basePath = grabAPIBasePath({ paradigm: "crud" });
|
||||
|
||||
const finalID = typeof targetID === "number" ? String(targetID) : targetID;
|
||||
|
||||
const finalPath = path.join(basePath, dbName, tableName, finalID || "");
|
||||
|
||||
const GET_RES = await queryDSQLAPI<DsqlCrudQueryObject<T>>({
|
||||
method: "DELETE",
|
||||
path: finalPath,
|
||||
body: deleteSpec,
|
||||
});
|
||||
|
||||
return GET_RES;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import path from "path";
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import { APIResponseObject, DsqlCrudQueryObject } from "../../types";
|
||||
import grabAPIBasePath from "../../utils/grab-api-base-path";
|
||||
|
||||
type Params<T extends { [key: string]: any } = { [key: string]: any }> = {
|
||||
dbName: string;
|
||||
tableName: string;
|
||||
query?: DsqlCrudQueryObject<T>;
|
||||
targetId?: string | number;
|
||||
};
|
||||
|
||||
export default async function apiCrudGET<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({
|
||||
dbName,
|
||||
tableName,
|
||||
query,
|
||||
targetId,
|
||||
}: Params<T>): Promise<APIResponseObject> {
|
||||
const basePath = grabAPIBasePath({ paradigm: "crud" });
|
||||
|
||||
const finalID = typeof targetId === "number" ? String(targetId) : targetId;
|
||||
|
||||
const finalPath = path.join(basePath, dbName, tableName, finalID || "");
|
||||
|
||||
const GET_RES = await queryDSQLAPI<DsqlCrudQueryObject<T>>({
|
||||
method: "GET",
|
||||
path: finalPath,
|
||||
query,
|
||||
});
|
||||
|
||||
return GET_RES;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import apiCrudGET from "./get";
|
||||
import apiCrudPOST from "./post";
|
||||
import apiCrudPUT from "./put";
|
||||
import apiCrudDELETE from "./delete";
|
||||
|
||||
const crud = {
|
||||
get: apiCrudGET,
|
||||
insert: apiCrudPOST,
|
||||
update: apiCrudPUT,
|
||||
delete: apiCrudDELETE,
|
||||
options: async () => {},
|
||||
};
|
||||
|
||||
export default crud;
|
||||
@@ -0,0 +1,42 @@
|
||||
import path from "path";
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import { APIResponseObject, DsqlCrudQueryObject } from "../../types";
|
||||
import grabAPIBasePath from "../../utils/grab-api-base-path";
|
||||
|
||||
export type APICrudPostParams<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
> = {
|
||||
dbName: string;
|
||||
tableName: string;
|
||||
body: T;
|
||||
update?: boolean;
|
||||
};
|
||||
|
||||
export default async function apiCrudPOST<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({
|
||||
dbName,
|
||||
tableName,
|
||||
body,
|
||||
update,
|
||||
}: APICrudPostParams<T>): Promise<APIResponseObject> {
|
||||
const basePath = grabAPIBasePath({ paradigm: "crud" });
|
||||
|
||||
const passedID = body.id as string | number | undefined;
|
||||
|
||||
const finalID = update
|
||||
? typeof passedID === "number"
|
||||
? String(passedID)
|
||||
: passedID
|
||||
: undefined;
|
||||
|
||||
const finalPath = path.join(basePath, dbName, tableName, finalID || "");
|
||||
|
||||
const GET_RES = await queryDSQLAPI<DsqlCrudQueryObject<T>>({
|
||||
method: update ? "PUT" : "POST",
|
||||
path: finalPath,
|
||||
body,
|
||||
});
|
||||
|
||||
return GET_RES;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import apiCrudPOST, { APICrudPostParams } from "./post";
|
||||
|
||||
type Params<T extends { [key: string]: any } = { [key: string]: any }> = Omit<
|
||||
APICrudPostParams<T>,
|
||||
"update"
|
||||
> & {
|
||||
targetID: string | number;
|
||||
};
|
||||
|
||||
export default async function apiCrudPUT<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({ dbName, tableName, body, targetID }: Params<T>) {
|
||||
const updatedBody = { ...body } as any;
|
||||
|
||||
if (targetID) {
|
||||
updatedBody["id"] = targetID;
|
||||
}
|
||||
|
||||
return await apiCrudPOST({
|
||||
dbName,
|
||||
tableName,
|
||||
body: updatedBody,
|
||||
update: true,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user