Updates
This commit is contained in:
@@ -1470,7 +1470,7 @@ export type DsqlMethodCrudParam<
|
||||
> = {
|
||||
method: (typeof DataCrudRequestMethods)[number];
|
||||
body?: T;
|
||||
query?: string | T;
|
||||
query?: DsqlCrudQueryObject<T>;
|
||||
tableName: string;
|
||||
addUser?: {
|
||||
field: string;
|
||||
@@ -1488,16 +1488,20 @@ export type DsqlMethodCrudParam<
|
||||
reqMethod: (typeof DataCrudRequestMethods)[number];
|
||||
}) => Promise<T>;
|
||||
existingData?: T;
|
||||
targetId?: string | number;
|
||||
};
|
||||
|
||||
export const DsqlCrudActions = ["insert", "update", "delete", "get"] as const;
|
||||
|
||||
export type DsqlCrudQueryObject<T extends object = { [key: string]: any }> =
|
||||
ServerQueryParam & {
|
||||
query: ServerQueryQueryObject<T>;
|
||||
};
|
||||
export type DsqlCrudQueryObject<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
> = ServerQueryParam & {
|
||||
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];
|
||||
table: string;
|
||||
data?: T;
|
||||
|
||||
@@ -26,45 +26,54 @@ export default async function dsqlMethodCrud<
|
||||
existingData,
|
||||
body,
|
||||
query,
|
||||
targetId,
|
||||
}: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>> {
|
||||
let result: CRUDResponseObject = {
|
||||
success: false,
|
||||
};
|
||||
|
||||
try {
|
||||
let finalBody = body as any;
|
||||
let finalQuery = query as any;
|
||||
let finalBody = body;
|
||||
let finalQuery = query;
|
||||
|
||||
Object.keys(finalQuery).forEach((key) => {
|
||||
const value = finalQuery[key];
|
||||
if (typeof value == "string" && value.match(/^\{|^\[/)) {
|
||||
finalQuery[key] = EJSON.stringify(value);
|
||||
}
|
||||
if (value == "true") {
|
||||
finalQuery[key] = true;
|
||||
}
|
||||
if (value == "false") {
|
||||
finalQuery[key] = false;
|
||||
}
|
||||
});
|
||||
let LIMIT = 10;
|
||||
let PAGE = 1;
|
||||
let OFFSET = (PAGE - 1) * LIMIT;
|
||||
|
||||
const LIMIT = finalQuery.limit || 10;
|
||||
const PAGE = finalQuery.page || 1;
|
||||
const OFFSET = (PAGE - 1) * LIMIT;
|
||||
if (finalQuery) {
|
||||
Object.keys(finalQuery).forEach((key) => {
|
||||
const value = finalQuery[key];
|
||||
if (typeof value == "string" && value.match(/^\{|^\[/)) {
|
||||
finalQuery[key] = EJSON.stringify(value);
|
||||
}
|
||||
if (value == "true") {
|
||||
finalQuery[key] = true;
|
||||
}
|
||||
if (value == "false") {
|
||||
finalQuery[key] = false;
|
||||
}
|
||||
});
|
||||
|
||||
let finalData = {
|
||||
...finalBody.data,
|
||||
...extraData,
|
||||
} as T;
|
||||
LIMIT = finalQuery.limit || 10;
|
||||
PAGE = finalQuery.page || 1;
|
||||
OFFSET = (PAGE - 1) * LIMIT;
|
||||
}
|
||||
|
||||
if (user && addUser) {
|
||||
let finalData = finalBody
|
||||
? ({
|
||||
...finalBody.data,
|
||||
...extraData,
|
||||
} as T)
|
||||
: undefined;
|
||||
|
||||
if (finalData && user && addUser) {
|
||||
finalData = {
|
||||
...finalData,
|
||||
[addUser.field]: String(user.id),
|
||||
};
|
||||
}
|
||||
|
||||
if (transform) {
|
||||
if (transform && finalData) {
|
||||
finalData = await transform({
|
||||
data: finalData,
|
||||
existingData: existingData,
|
||||
@@ -78,19 +87,21 @@ export default async function dsqlMethodCrud<
|
||||
const GET_RESULT = await dsqlCrud({
|
||||
action: "get",
|
||||
table: tableName,
|
||||
query: {
|
||||
...finalQuery,
|
||||
query: {
|
||||
...finalQuery.query,
|
||||
user_id: user
|
||||
? {
|
||||
value: String(user.id),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
limit: LIMIT,
|
||||
offset: OFFSET || undefined,
|
||||
},
|
||||
query: finalQuery
|
||||
? ({
|
||||
...finalQuery,
|
||||
query: {
|
||||
...finalQuery.query,
|
||||
user_id: user
|
||||
? {
|
||||
value: String(user.id),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
limit: LIMIT,
|
||||
offset: OFFSET || undefined,
|
||||
} as any)
|
||||
: undefined,
|
||||
});
|
||||
|
||||
result = {
|
||||
@@ -120,7 +131,7 @@ export default async function dsqlMethodCrud<
|
||||
action: "update",
|
||||
table: tableName,
|
||||
data: finalData,
|
||||
targetId: finalBody.data.id,
|
||||
targetId,
|
||||
});
|
||||
result = {
|
||||
success: Boolean(PUT_RESULT?.success),
|
||||
@@ -133,7 +144,7 @@ export default async function dsqlMethodCrud<
|
||||
const DELETE_RESULT = await dsqlCrud({
|
||||
action: "delete",
|
||||
table: tableName,
|
||||
targetId: finalBody.data.id,
|
||||
targetId,
|
||||
});
|
||||
result = {
|
||||
success: Boolean(DELETE_RESULT?.success),
|
||||
|
||||
@@ -8,7 +8,6 @@ export default function deserializeQuery(
|
||||
): {
|
||||
[s: string]: any;
|
||||
} {
|
||||
/** @type {Object<string,any>} */
|
||||
let queryObject: { [s: string]: any } =
|
||||
typeof query == "object" ? query : Object(EJSON.parse(query));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user