mantine-entity
Version:
A library combining Mantine, TanStack Query, and Mantine React Table for efficient entity management
122 lines (121 loc) • 4.14 kB
JavaScript
// Function to encode a CollectionQuery object to a custom URL query string
export function encodeCollectionQuery(query = {}) {
const queryParams = [];
if (query.select) {
queryParams.push(`s=${query.select.join(",")}`);
}
if (query.where) {
queryParams.push(`w=${encodeWhere(query.where)}`);
}
if (query.t !== undefined) {
queryParams.push(`t=${query.t}`);
}
if (query.sk !== undefined) {
queryParams.push(`sk=${query.sk}`);
}
if (query.orderBy && Array.isArray(query.orderBy)) {
queryParams.push(`o=${encodeOrderBy(query.orderBy)}`);
}
if (query.includes && Array.isArray(query.includes)) {
queryParams.push(`i=${query.includes.join(",")}`);
}
if (query.groupBy && Array.isArray(query.groupBy)) {
queryParams.push(`g=${query.groupBy.join(",")}`);
}
if (query.having && Array.isArray(query.having)) {
queryParams.push(`h=${encodeWhere(query.having)}`);
}
if (query.count !== undefined) {
queryParams.push(`c=${query.count}`);
}
return queryParams.join("&");
}
// Function to decode a custom URL query string into a CollectionQuery object
export function decodeCollectionQuery(queryString) {
const query = {};
const queryParams = new URLSearchParams(queryString);
if (queryParams.has("s")) {
query.select = queryParams.get("s").split(",");
}
if (queryParams.has("w")) {
query.where = decodeWhere(queryParams.get("w"));
}
if (queryParams.has("t")) {
query.t = parseInt(queryParams.get("t"), 10);
}
if (queryParams.has("sk")) {
query.sk = parseInt(queryParams.get("sk"), 10);
}
if (queryParams.has("o")) {
query.orderBy = decodeOrderBy(queryParams.get("o"));
}
if (queryParams.has("i")) {
query.includes = queryParams.get("i").split(",");
}
if (queryParams.has("g")) {
query.groupBy = queryParams.get("g").split(",");
}
if (queryParams.has("h")) {
query.having = decodeWhere(queryParams.get("h"));
}
if (queryParams.has("c")) {
query.count = queryParams.get("c") === "true";
}
return query;
}
// Helper function to encode a Where array to a compact string
function encodeWhere(where) {
return where.map(encodeWhereGroup).join("|");
}
// Helper function to decode a compact string to a Where array
function decodeWhere(encoded) {
return encoded.split("|").map(decodeWhereGroup);
}
// Helper function to encode a Where group to a compact string
function encodeWhereGroup(group) {
return group.map(encodeWhereItem).filter(Boolean).join("|");
}
// Helper function to decode a compact string to a Where group
function decodeWhereGroup(encoded) {
return encoded.split(",").map(decodeWhereItem);
}
// Helper function to encode a Where item to a compact string
function encodeWhereItem(item) {
if (item.operator === "IS" || item.operator === "IS DISTINCT FROM") {
// Special handling for null checks
return `${item.column}:${item.operator}:null`;
}
return item.value ? `${item.column}:${item.operator}:${item.value}` : "";
}
// Helper function to decode a compact string to a Where item
function decodeWhereItem(encoded) {
const [column, operator, value] = encoded.split(":");
return {
column,
operator: operator,
value,
};
}
// Helper function to encode an Order array to a compact string
function encodeOrderBy(orderBy) {
return orderBy.map(encodeOrderItem).join(",");
}
// Helper function to decode a compact string to an Order array
function decodeOrderBy(encoded) {
return encoded.split(",").map(decodeOrderItem);
}
// Helper function to encode an Order item to a compact string
function encodeOrderItem(item) {
return `${item.column}:${item.direction ?? "ASC"}`;
}
// Helper function to decode a compact string to an Order item
function decodeOrderItem(encoded) {
const [column, direction] = encoded.split(":");
return {
column,
direction: direction,
};
}
export const buildQuery = (query = {}) => {
return query;
};