This commit is contained in:
Benjamin Toby 2025-12-31 10:28:52 +01:00
parent 276a0db83b
commit f7a2d6e03a
7 changed files with 51 additions and 14 deletions

View File

@ -22,7 +22,7 @@ const put_result_1 = __importDefault(require("./functions/put-result"));
const delete_result_1 = __importDefault(require("./functions/delete-result"));
function apiCrudHandler(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
var _a, _b, _c;
try {
const { auth, method } = params;
const isAuthorized = yield (auth === null || auth === void 0 ? void 0 : auth());
@ -72,15 +72,20 @@ function apiCrudHandler(params) {
};
}
}
let result;
switch (method) {
case "GET":
return yield (0, get_result_1.default)(crudParams);
result = yield (0, get_result_1.default)(crudParams);
case "POST":
return yield (0, post_result_1.default)(crudParams);
result = yield (0, post_result_1.default)(crudParams);
case "PUT":
return yield (0, put_result_1.default)(crudParams);
result = yield (0, put_result_1.default)(crudParams);
case "DELETE":
return yield (0, delete_result_1.default)(crudParams);
result = yield (0, delete_result_1.default)(crudParams);
}
yield ((_c = params.postResultsFn) === null || _c === void 0 ? void 0 : _c.call(params, Object.assign(Object.assign({}, crudParams), { res: result })));
if (result) {
return result;
}
return {
success: false,

View File

@ -130,8 +130,8 @@ function createDbFromSchema(_a) {
* @description Update table if table exists
*/
const updateExistingTable = yield (0, updateTable_1.default)({
dbFullName: dbFullName,
tableName: tableName,
dbFullName,
tableName,
tableFields: fields,
userId,
dbSchema: database,

View File

@ -2216,6 +2216,7 @@ export type APIPathsParams<T extends {
postMiddleware?: APIPathsParamsCrudMiddleware<T>;
putMiddleware?: APIPathsParamsCrudMiddleware<T>;
deleteMiddleware?: APIPathsParamsCrudMiddleware<T>;
postResultsFn?: APIPathsPostResulstsFn<T>;
/** Runs For `POST`, `PUT`, and `DELETE` */
crudMiddleware?: APIPathsParamsCrudMiddleware<T>;
method: "GET" | "POST" | "PUT" | "DELETE";
@ -2264,6 +2265,17 @@ export type APIPathsParamsCrudMiddleware<T extends {
table: K;
targetId?: number | string;
}) => Promise<APIPathsBody<T> | undefined>;
export type APIPathsPostResulstsFn<T extends {
[k: string]: any;
} = {
[k: string]: any;
}, K extends string = string> = (params: {
body?: APIPathsBody<T>;
query?: DsqlCrudQueryObject<T>;
table: K;
targetId?: number | string;
res?: APIResponseObject;
}) => Promise<APIPathsBody<T> | undefined>;
export type APIPathsParamsAllowedTable = {
table: string;
allowedFields?: (string | RegExp)[];

View File

@ -87,15 +87,23 @@ export default async function apiCrudHandler<
}
}
let result: APIResponseObject | undefined;
switch (method) {
case "GET":
return await getResult(crudParams);
result = await getResult(crudParams);
case "POST":
return await postResult(crudParams);
result = await postResult(crudParams);
case "PUT":
return await putResult(crudParams);
result = await putResult(crudParams);
case "DELETE":
return await deleteResult(crudParams);
result = await deleteResult(crudParams);
}
await params.postResultsFn?.({ ...crudParams, res: result });
if (result) {
return result;
}
return {

View File

@ -160,8 +160,8 @@ export default async function createDbFromSchema({
* @description Update table if table exists
*/
const updateExistingTable = await updateTable({
dbFullName: dbFullName,
tableName: tableName,
dbFullName,
tableName,
tableFields: fields,
userId,
dbSchema: database,

View File

@ -2877,6 +2877,7 @@ export type APIPathsParams<
postMiddleware?: APIPathsParamsCrudMiddleware<T>;
putMiddleware?: APIPathsParamsCrudMiddleware<T>;
deleteMiddleware?: APIPathsParamsCrudMiddleware<T>;
postResultsFn?: APIPathsPostResulstsFn<T>;
/** Runs For `POST`, `PUT`, and `DELETE` */
crudMiddleware?: APIPathsParamsCrudMiddleware<T>;
method: "GET" | "POST" | "PUT" | "DELETE";
@ -2931,6 +2932,17 @@ export type APIPathsParamsCrudMiddleware<
targetId?: number | string;
}) => Promise<APIPathsBody<T> | undefined>;
export type APIPathsPostResulstsFn<
T extends { [k: string]: any } = { [k: string]: any },
K extends string = string
> = (params: {
body?: APIPathsBody<T>;
query?: DsqlCrudQueryObject<T>;
table: K;
targetId?: number | string;
res?: APIResponseObject;
}) => Promise<APIPathsBody<T> | undefined>;
export type APIPathsParamsAllowedTable = {
table: string;
allowedFields?: (string | RegExp)[];

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/datasquirel",
"version": "5.7.3",
"version": "5.7.4",
"description": "Cloud-based SQL data management tool",
"main": "dist/index.js",
"bin": {