This commit is contained in:
Benjamin Toby
2025-01-25 14:20:25 +01:00
parent 1db9c3a2e7
commit e82bcd0824
91 changed files with 936 additions and 324 deletions
+25
View File
@@ -0,0 +1,25 @@
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
import { PostReturn, ServerQueryParam, ServerQueryQueryObject } from "../../types";
export declare const DsqlCrudActions: readonly ["insert", "update", "delete", "get"];
export type CrudQueryObject<T extends object = {
[key: string]: any;
}> = ServerQueryParam & {
query: ServerQueryQueryObject<T>;
};
export type CrudParam<T extends object = {
[key: string]: any;
}> = {
action: (typeof DsqlCrudActions)[number];
table: string;
data?: T;
targetId?: string | number;
query?: CrudQueryObject<T>;
sanitize?: (data?: T) => T;
};
export default function dsqlCrud<T extends {
[key: string]: any;
} = {
[key: string]: any;
}>({ action, data, table, targetId, query, sanitize, }: CrudParam<T>): Promise<(PostReturn & {
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
}) | null>;
+69
View File
@@ -0,0 +1,69 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DsqlCrudActions = void 0;
exports.default = dsqlCrud;
const get_1 = __importDefault(require("../../actions/get"));
const post_1 = __importDefault(require("../../actions/post"));
const sql_generator_1 = __importDefault(require("../../functions/dsql/sql/sql-generator"));
exports.DsqlCrudActions = ["insert", "update", "delete", "get"];
function dsqlCrud(_a) {
return __awaiter(this, arguments, void 0, function* ({ action, data, table, targetId, query, sanitize, }) {
const finalData = sanitize ? sanitize(data) : data;
const finalId = targetId;
let queryObject;
switch (action) {
case "get":
queryObject = (0, sql_generator_1.default)({
tableName: table,
genObject: query,
});
const GET_RES = yield (0, get_1.default)({
query: (queryObject === null || queryObject === void 0 ? void 0 : queryObject.string) || "",
queryValues: (queryObject === null || queryObject === void 0 ? void 0 : queryObject.values) || [],
});
return Object.assign(Object.assign({}, GET_RES), { queryObject });
case "insert":
return yield (0, post_1.default)({
query: {
action: "insert",
table,
data: finalData,
},
});
case "update":
data === null || data === void 0 ? true : delete data.id;
return yield (0, post_1.default)({
query: {
action: "update",
table,
identifierColumnName: "id",
identifierValue: String(finalId),
data: finalData,
},
});
case "delete":
return yield (0, post_1.default)({
query: {
action: "delete",
table,
identifierColumnName: "id",
identifierValue: String(finalId),
},
});
default:
return null;
}
});
}
@@ -0,0 +1,42 @@
import { DATASQUIREL_LoggedInUser, ServerQueryParam } from "../../types";
export declare const DataCrudRequestMethods: readonly ["GET", "POST", "PUT", "DELETE"];
export type APIDataCrudQuery = ServerQueryParam & {
page?: number;
};
export type CRUDResponseObject<P extends any = any> = {
success: boolean;
payload?: P;
msg?: string;
error?: string;
};
export type ApiDataCrudParam<T extends {
[key: string]: any;
} = {
[key: string]: any;
}> = {
method: (typeof DataCrudRequestMethods)[number];
body?: T;
query?: string | T;
tableName: string;
addUser?: {
field: string;
};
user?: DATASQUIREL_LoggedInUser;
extraData?: T;
transform?: ({ data, existingData, user, }: {
user?: DATASQUIREL_LoggedInUser;
data: T;
existingData?: T;
reqMethod: (typeof DataCrudRequestMethods)[number];
}) => Promise<T>;
existingData?: T;
};
export default function dsqlMethodCrud<T extends {
[key: string]: any;
} = {
[key: string]: any;
}, P extends {
[key: string]: any;
} = {
[key: string]: any;
}>({ method, tableName, addUser, user, extraData, transform, existingData, body, query, }: ApiDataCrudParam<T>): Promise<CRUDResponseObject<P>>;
+122
View File
@@ -0,0 +1,122 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataCrudRequestMethods = void 0;
exports.default = dsqlMethodCrud;
const ejson_1 = __importDefault(require("../ejson"));
const crud_1 = __importDefault(require("./crud"));
exports.DataCrudRequestMethods = ["GET", "POST", "PUT", "DELETE"];
function dsqlMethodCrud(_a) {
return __awaiter(this, arguments, void 0, function* ({ method, tableName, addUser, user, extraData, transform, existingData, body, query, }) {
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) {
finalData = Object.assign(Object.assign({}, finalData), { [addUser.field]: String(user.id) });
}
if (transform) {
finalData = yield transform({
data: finalData,
existingData: existingData,
user,
reqMethod: method,
});
}
switch (method) {
case "GET":
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 }),
});
result = {
success: Boolean(GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.success),
payload: GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.payload,
msg: GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.msg,
error: GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.error,
};
break;
case "POST":
const POST_RESULT = yield (0, crud_1.default)({
action: "insert",
table: tableName,
data: finalData,
});
result = {
success: Boolean(POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.success),
payload: POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.payload,
msg: POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.msg,
error: POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.error,
};
break;
case "PUT":
const PUT_RESULT = yield (0, crud_1.default)({
action: "update",
table: tableName,
data: finalData,
targetId: finalBody.data.id,
});
result = {
success: Boolean(PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.success),
payload: PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.payload,
msg: PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.msg,
error: PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.error,
};
break;
case "DELETE":
const DELETE_RESULT = yield (0, crud_1.default)({
action: "delete",
table: tableName,
targetId: finalBody.data.id,
});
result = {
success: Boolean(DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.success),
payload: DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.payload,
msg: DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.msg,
error: DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.error,
};
break;
default:
break;
}
return result;
}
catch (error) {
return result;
}
});
}