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];
body?: T;
query?: string | T;
query?: DsqlCrudQueryObject<T>;
tableName: string;
addUser?: {
field: string;
@ -1307,14 +1307,19 @@ export type DsqlMethodCrudParam<T extends {
reqMethod: (typeof DataCrudRequestMethods)[number];
}) => Promise<T>;
existingData?: T;
targetId?: string | number;
};
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;
}> = ServerQueryParam & {
query: ServerQueryQueryObject<T>;
};
export type DsqlCrudParam<T extends object = {
export type DsqlCrudParam<T extends {
[key: string]: any;
} = {
[key: string]: any;
}> = {
action: (typeof DsqlCrudActions)[number];

View File

@ -16,4 +16,4 @@ export default function dsqlMethodCrud<T extends {
[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,33 +16,40 @@ exports.default = dsqlMethodCrud;
const ejson_1 = __importDefault(require("../ejson"));
const crud_1 = __importDefault(require("./crud"));
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 = {
success: false,
};
try {
let finalBody = body;
let finalQuery = query;
Object.keys(finalQuery).forEach((key) => {
const value = finalQuery[key];
if (typeof value == "string" && value.match(/^\{|^\[/)) {
finalQuery[key] = ejson_1.default.stringify(value);
}
if (value == "true") {
finalQuery[key] = true;
}
if (value == "false") {
finalQuery[key] = false;
}
});
const LIMIT = finalQuery.limit || 10;
const PAGE = finalQuery.page || 1;
const OFFSET = (PAGE - 1) * LIMIT;
let finalData = Object.assign(Object.assign({}, finalBody.data), extraData);
if (user && addUser) {
let LIMIT = 10;
let PAGE = 1;
let OFFSET = (PAGE - 1) * LIMIT;
if (finalQuery) {
Object.keys(finalQuery).forEach((key) => {
const value = finalQuery[key];
if (typeof value == "string" && value.match(/^\{|^\[/)) {
finalQuery[key] = ejson_1.default.stringify(value);
}
if (value == "true") {
finalQuery[key] = true;
}
if (value == "false") {
finalQuery[key] = false;
}
});
LIMIT = finalQuery.limit || 10;
PAGE = finalQuery.page || 1;
OFFSET = (PAGE - 1) * LIMIT;
}
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) });
}
if (transform) {
if (transform && finalData) {
finalData = yield transform({
data: finalData,
existingData: existingData,
@ -55,11 +62,13 @@ function dsqlMethodCrud(_a) {
const GET_RESULT = yield (0, crud_1.default)({
action: "get",
table: tableName,
query: Object.assign(Object.assign({}, finalQuery), { query: Object.assign(Object.assign({}, finalQuery.query), { user_id: user
? {
value: String(user.id),
}
: undefined }), limit: LIMIT, offset: OFFSET || undefined }),
query: finalQuery
? Object.assign(Object.assign({}, finalQuery), { query: Object.assign(Object.assign({}, finalQuery.query), { user_id: user
? {
value: String(user.id),
}
: undefined }), limit: LIMIT, offset: OFFSET || undefined })
: undefined,
});
result = {
success: Boolean(GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.success),
@ -86,7 +95,7 @@ function dsqlMethodCrud(_a) {
action: "update",
table: tableName,
data: finalData,
targetId: finalBody.data.id,
targetId,
});
result = {
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)({
action: "delete",
table: tableName,
targetId: finalBody.data.id,
targetId,
});
result = {
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
*/
function deserializeQuery(query) {
/** @type {Object<string,any>} */
let queryObject = typeof query == "object" ? query : Object(ejson_1.default.parse(query));
const keys = Object.keys(queryObject);
for (let i = 0; i < keys.length; i++) {

View File

@ -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;

View File

@ -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),

View File

@ -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));

View File

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