This commit is contained in:
Benjamin Toby 2025-01-25 14:45:22 +01:00
parent 483ea16d4d
commit 4fe8921bc6
8 changed files with 104 additions and 77 deletions

View File

@ -1293,7 +1293,7 @@ export type DsqlMethodCrudParam<T extends {
}> = { }> = {
method: (typeof DataCrudRequestMethods)[number]; method: (typeof DataCrudRequestMethods)[number];
body?: T; body?: T;
query?: string | T; query?: DsqlCrudQueryObject<T>;
tableName: string; tableName: string;
addUser?: { addUser?: {
field: string; field: string;
@ -1307,14 +1307,19 @@ export type DsqlMethodCrudParam<T extends {
reqMethod: (typeof DataCrudRequestMethods)[number]; reqMethod: (typeof DataCrudRequestMethods)[number];
}) => Promise<T>; }) => Promise<T>;
existingData?: T; existingData?: T;
targetId?: string | number;
}; };
export declare const DsqlCrudActions: readonly ["insert", "update", "delete", "get"]; export declare const DsqlCrudActions: readonly ["insert", "update", "delete", "get"];
export type DsqlCrudQueryObject<T extends object = { export type DsqlCrudQueryObject<T extends {
[key: string]: any;
} = {
[key: string]: any; [key: string]: any;
}> = ServerQueryParam & { }> = ServerQueryParam & {
query: ServerQueryQueryObject<T>; query: ServerQueryQueryObject<T>;
}; };
export type DsqlCrudParam<T extends object = { export type DsqlCrudParam<T extends {
[key: string]: any;
} = {
[key: string]: any; [key: string]: any;
}> = { }> = {
action: (typeof DsqlCrudActions)[number]; action: (typeof DsqlCrudActions)[number];

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, }: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>>; }>({ method, tableName, addUser, user, extraData, transform, existingData, body, query, targetId, }: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>>;

View File

@ -16,13 +16,17 @@ 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, }) { return __awaiter(this, arguments, void 0, function* ({ method, tableName, addUser, user, extraData, transform, existingData, body, query, targetId, }) {
let result = { let result = {
success: false, success: false,
}; };
try { try {
let finalBody = body; let finalBody = body;
let finalQuery = query; let finalQuery = query;
let LIMIT = 10;
let PAGE = 1;
let OFFSET = (PAGE - 1) * LIMIT;
if (finalQuery) {
Object.keys(finalQuery).forEach((key) => { Object.keys(finalQuery).forEach((key) => {
const value = finalQuery[key]; const value = finalQuery[key];
if (typeof value == "string" && value.match(/^\{|^\[/)) { if (typeof value == "string" && value.match(/^\{|^\[/)) {
@ -35,14 +39,17 @@ function dsqlMethodCrud(_a) {
finalQuery[key] = false; finalQuery[key] = false;
} }
}); });
const LIMIT = finalQuery.limit || 10; LIMIT = finalQuery.limit || 10;
const PAGE = finalQuery.page || 1; PAGE = finalQuery.page || 1;
const OFFSET = (PAGE - 1) * LIMIT; OFFSET = (PAGE - 1) * LIMIT;
let finalData = Object.assign(Object.assign({}, finalBody.data), extraData); }
if (user && addUser) { let finalData = finalBody
? Object.assign(Object.assign({}, finalBody.data), extraData)
: undefined;
if (finalData && user && 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) { if (transform && finalData) {
finalData = yield transform({ finalData = yield transform({
data: finalData, data: finalData,
existingData: existingData, existingData: existingData,
@ -55,11 +62,13 @@ function dsqlMethodCrud(_a) {
const GET_RESULT = yield (0, crud_1.default)({ const GET_RESULT = yield (0, crud_1.default)({
action: "get", action: "get",
table: tableName, table: tableName,
query: Object.assign(Object.assign({}, finalQuery), { query: Object.assign(Object.assign({}, finalQuery.query), { user_id: user query: finalQuery
? Object.assign(Object.assign({}, finalQuery), { query: Object.assign(Object.assign({}, finalQuery.query), { user_id: user
? { ? {
value: String(user.id), value: String(user.id),
} }
: undefined }), limit: LIMIT, offset: OFFSET || undefined }), : undefined }), limit: LIMIT, offset: OFFSET || undefined })
: undefined,
}); });
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),
@ -86,7 +95,7 @@ function dsqlMethodCrud(_a) {
action: "update", action: "update",
table: tableName, table: tableName,
data: finalData, data: finalData,
targetId: finalBody.data.id, targetId,
}); });
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),
@ -99,7 +108,7 @@ function dsqlMethodCrud(_a) {
const DELETE_RESULT = yield (0, crud_1.default)({ const DELETE_RESULT = yield (0, crud_1.default)({
action: "delete", action: "delete",
table: tableName, table: tableName,
targetId: finalBody.data.id, targetId,
}); });
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

@ -9,7 +9,6 @@ const ejson_1 = __importDefault(require("./ejson"));
* # Convert Serialized Query back to object * # Convert Serialized Query back to object
*/ */
function deserializeQuery(query) { function deserializeQuery(query) {
/** @type {Object<string,any>} */
let queryObject = typeof query == "object" ? query : Object(ejson_1.default.parse(query)); let queryObject = typeof query == "object" ? query : Object(ejson_1.default.parse(query));
const keys = Object.keys(queryObject); const keys = Object.keys(queryObject);
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {

View File

@ -1470,7 +1470,7 @@ export type DsqlMethodCrudParam<
> = { > = {
method: (typeof DataCrudRequestMethods)[number]; method: (typeof DataCrudRequestMethods)[number];
body?: T; body?: T;
query?: string | T; query?: DsqlCrudQueryObject<T>;
tableName: string; tableName: string;
addUser?: { addUser?: {
field: string; field: string;
@ -1488,16 +1488,20 @@ export type DsqlMethodCrudParam<
reqMethod: (typeof DataCrudRequestMethods)[number]; reqMethod: (typeof DataCrudRequestMethods)[number];
}) => Promise<T>; }) => Promise<T>;
existingData?: T; existingData?: T;
targetId?: string | number;
}; };
export const DsqlCrudActions = ["insert", "update", "delete", "get"] as const; export const DsqlCrudActions = ["insert", "update", "delete", "get"] as const;
export type DsqlCrudQueryObject<T extends object = { [key: string]: any }> = export type DsqlCrudQueryObject<
ServerQueryParam & { T extends { [key: string]: any } = { [key: string]: any }
> = ServerQueryParam & {
query: ServerQueryQueryObject<T>; query: ServerQueryQueryObject<T>;
}; };
export type DsqlCrudParam<T extends object = { [key: string]: any }> = { export type DsqlCrudParam<
T extends { [key: string]: any } = { [key: string]: any }
> = {
action: (typeof DsqlCrudActions)[number]; action: (typeof DsqlCrudActions)[number];
table: string; table: string;
data?: T; data?: T;

View File

@ -26,15 +26,21 @@ export default async function dsqlMethodCrud<
existingData, existingData,
body, body,
query, query,
targetId,
}: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>> { }: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>> {
let result: CRUDResponseObject = { let result: CRUDResponseObject = {
success: false, success: false,
}; };
try { try {
let finalBody = body as any; let finalBody = body;
let finalQuery = query as any; let finalQuery = query;
let LIMIT = 10;
let PAGE = 1;
let OFFSET = (PAGE - 1) * LIMIT;
if (finalQuery) {
Object.keys(finalQuery).forEach((key) => { Object.keys(finalQuery).forEach((key) => {
const value = finalQuery[key]; const value = finalQuery[key];
if (typeof value == "string" && value.match(/^\{|^\[/)) { if (typeof value == "string" && value.match(/^\{|^\[/)) {
@ -48,23 +54,26 @@ export default async function dsqlMethodCrud<
} }
}); });
const LIMIT = finalQuery.limit || 10; LIMIT = finalQuery.limit || 10;
const PAGE = finalQuery.page || 1; PAGE = finalQuery.page || 1;
const OFFSET = (PAGE - 1) * LIMIT; OFFSET = (PAGE - 1) * LIMIT;
}
let finalData = { let finalData = finalBody
? ({
...finalBody.data, ...finalBody.data,
...extraData, ...extraData,
} as T; } as T)
: undefined;
if (user && addUser) { if (finalData && user && addUser) {
finalData = { finalData = {
...finalData, ...finalData,
[addUser.field]: String(user.id), [addUser.field]: String(user.id),
}; };
} }
if (transform) { if (transform && finalData) {
finalData = await transform({ finalData = await transform({
data: finalData, data: finalData,
existingData: existingData, existingData: existingData,
@ -78,7 +87,8 @@ export default async function dsqlMethodCrud<
const GET_RESULT = await dsqlCrud({ const GET_RESULT = await dsqlCrud({
action: "get", action: "get",
table: tableName, table: tableName,
query: { query: finalQuery
? ({
...finalQuery, ...finalQuery,
query: { query: {
...finalQuery.query, ...finalQuery.query,
@ -90,7 +100,8 @@ export default async function dsqlMethodCrud<
}, },
limit: LIMIT, limit: LIMIT,
offset: OFFSET || undefined, offset: OFFSET || undefined,
}, } as any)
: undefined,
}); });
result = { result = {
@ -120,7 +131,7 @@ export default async function dsqlMethodCrud<
action: "update", action: "update",
table: tableName, table: tableName,
data: finalData, data: finalData,
targetId: finalBody.data.id, targetId,
}); });
result = { result = {
success: Boolean(PUT_RESULT?.success), success: Boolean(PUT_RESULT?.success),
@ -133,7 +144,7 @@ export default async function dsqlMethodCrud<
const DELETE_RESULT = await dsqlCrud({ const DELETE_RESULT = await dsqlCrud({
action: "delete", action: "delete",
table: tableName, table: tableName,
targetId: finalBody.data.id, targetId,
}); });
result = { result = {
success: Boolean(DELETE_RESULT?.success), success: Boolean(DELETE_RESULT?.success),

View File

@ -8,7 +8,6 @@ export default function deserializeQuery(
): { ): {
[s: string]: any; [s: string]: any;
} { } {
/** @type {Object<string,any>} */
let queryObject: { [s: string]: any } = let queryObject: { [s: string]: any } =
typeof query == "object" ? query : Object(EJSON.parse(query)); typeof query == "object" ? query : Object(EJSON.parse(query));

View File

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