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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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