This commit is contained in:
Benjamin Toby 2025-01-25 15:45:27 +01:00
parent 4fe8921bc6
commit 8ecef72efc
6 changed files with 18 additions and 7 deletions

View File

@ -1308,6 +1308,7 @@ export type DsqlMethodCrudParam<T extends {
}) => Promise<T>; }) => Promise<T>;
existingData?: T; existingData?: T;
targetId?: string | number; targetId?: string | number;
sanitize?: (data?: T) => T;
}; };
export declare const DsqlCrudActions: readonly ["insert", "update", "delete", "get"]; export declare const DsqlCrudActions: readonly ["insert", "update", "delete", "get"];
export type DsqlCrudQueryObject<T extends { export type DsqlCrudQueryObject<T extends {

View File

@ -16,4 +16,4 @@ export default function dsqlMethodCrud<T extends {
[key: string]: any; [key: string]: any;
} = { } = {
[key: string]: any; [key: string]: any;
}>({ method, tableName, addUser, user, extraData, transform, existingData, body, query, targetId, }: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>>; }>({ method, tableName, addUser, user, extraData, transform, existingData, body, query, targetId, sanitize, }: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>>;

View File

@ -16,7 +16,7 @@ exports.default = dsqlMethodCrud;
const ejson_1 = __importDefault(require("../ejson")); const ejson_1 = __importDefault(require("../ejson"));
const crud_1 = __importDefault(require("./crud")); const crud_1 = __importDefault(require("./crud"));
function dsqlMethodCrud(_a) { function dsqlMethodCrud(_a) {
return __awaiter(this, arguments, void 0, function* ({ method, tableName, addUser, user, extraData, transform, existingData, body, query, targetId, }) { return __awaiter(this, arguments, void 0, function* ({ method, tableName, addUser, user, extraData, transform, existingData, body, query, targetId, sanitize, }) {
let result = { let result = {
success: false, success: false,
}; };
@ -44,9 +44,9 @@ function dsqlMethodCrud(_a) {
OFFSET = (PAGE - 1) * LIMIT; OFFSET = (PAGE - 1) * LIMIT;
} }
let finalData = finalBody let finalData = finalBody
? Object.assign(Object.assign({}, finalBody.data), extraData) ? Object.assign(Object.assign({}, finalBody), extraData)
: undefined; : undefined;
if (finalData && user && addUser) { if (finalData && (user === null || user === void 0 ? void 0 : user.id) && addUser) {
finalData = Object.assign(Object.assign({}, finalData), { [addUser.field]: String(user.id) }); finalData = Object.assign(Object.assign({}, finalData), { [addUser.field]: String(user.id) });
} }
if (transform && finalData) { if (transform && finalData) {
@ -69,6 +69,7 @@ function dsqlMethodCrud(_a) {
} }
: undefined }), limit: LIMIT, offset: OFFSET || undefined }) : undefined }), limit: LIMIT, offset: OFFSET || undefined })
: undefined, : undefined,
sanitize,
}); });
result = { result = {
success: Boolean(GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.success), success: Boolean(GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.success),
@ -82,6 +83,7 @@ function dsqlMethodCrud(_a) {
action: "insert", action: "insert",
table: tableName, table: tableName,
data: finalData, data: finalData,
sanitize,
}); });
result = { result = {
success: Boolean(POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.success), success: Boolean(POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.success),
@ -96,6 +98,7 @@ function dsqlMethodCrud(_a) {
table: tableName, table: tableName,
data: finalData, data: finalData,
targetId, targetId,
sanitize,
}); });
result = { result = {
success: Boolean(PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.success), success: Boolean(PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.success),
@ -109,6 +112,7 @@ function dsqlMethodCrud(_a) {
action: "delete", action: "delete",
table: tableName, table: tableName,
targetId, targetId,
sanitize,
}); });
result = { result = {
success: Boolean(DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.success), success: Boolean(DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.success),

View File

@ -1489,6 +1489,7 @@ export type DsqlMethodCrudParam<
}) => Promise<T>; }) => Promise<T>;
existingData?: T; existingData?: T;
targetId?: string | number; targetId?: string | number;
sanitize?: (data?: T) => T;
}; };
export const DsqlCrudActions = ["insert", "update", "delete", "get"] as const; export const DsqlCrudActions = ["insert", "update", "delete", "get"] as const;

View File

@ -27,6 +27,7 @@ export default async function dsqlMethodCrud<
body, body,
query, query,
targetId, targetId,
sanitize,
}: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>> { }: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>> {
let result: CRUDResponseObject = { let result: CRUDResponseObject = {
success: false, success: false,
@ -61,12 +62,12 @@ export default async function dsqlMethodCrud<
let finalData = finalBody let finalData = finalBody
? ({ ? ({
...finalBody.data, ...finalBody,
...extraData, ...extraData,
} as T) } as T)
: undefined; : undefined;
if (finalData && user && addUser) { if (finalData && user?.id && addUser) {
finalData = { finalData = {
...finalData, ...finalData,
[addUser.field]: String(user.id), [addUser.field]: String(user.id),
@ -102,6 +103,7 @@ export default async function dsqlMethodCrud<
offset: OFFSET || undefined, offset: OFFSET || undefined,
} as any) } as any)
: undefined, : undefined,
sanitize,
}); });
result = { result = {
@ -117,6 +119,7 @@ export default async function dsqlMethodCrud<
action: "insert", action: "insert",
table: tableName, table: tableName,
data: finalData, data: finalData,
sanitize,
}); });
result = { result = {
success: Boolean(POST_RESULT?.success), success: Boolean(POST_RESULT?.success),
@ -132,6 +135,7 @@ export default async function dsqlMethodCrud<
table: tableName, table: tableName,
data: finalData, data: finalData,
targetId, targetId,
sanitize,
}); });
result = { result = {
success: Boolean(PUT_RESULT?.success), success: Boolean(PUT_RESULT?.success),
@ -145,6 +149,7 @@ export default async function dsqlMethodCrud<
action: "delete", action: "delete",
table: tableName, table: tableName,
targetId, targetId,
sanitize,
}); });
result = { result = {
success: Boolean(DELETE_RESULT?.success), success: Boolean(DELETE_RESULT?.success),

View File

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