ToolJet/frontend/src/TooljetDatabase/usePostgrestQueryBuilder.jsx

106 lines
3.3 KiB
React
Raw Normal View History

import { useRef } from 'react';
import PostgrestQueryBuilder from '@/_helpers/postgrestQueryBuilder';
import { tooljetDatabaseService } from '@/_services';
import { isEmpty } from 'lodash';
import { toast } from 'react-hot-toast';
export const usePostgrestQueryBuilder = ({ organizationId, selectedTable, setSelectedTableData, setTotalRecords }) => {
const postgrestQueryBuilder = useRef({
filterQuery: new PostgrestQueryBuilder(),
sortQuery: new PostgrestQueryBuilder(),
paginationQuery: new PostgrestQueryBuilder(),
});
const handleBuildSortQuery = (filters) => {
postgrestQueryBuilder.current.sortQuery = new PostgrestQueryBuilder();
Object.keys(filters).map((key) => {
if (!isEmpty(filters[key])) {
const { column, order } = filters[key];
if (!isEmpty(column) && !isEmpty(order)) {
postgrestQueryBuilder.current.sortQuery.order(column, order);
}
}
});
updateSelectedTableData();
};
const updateSelectedTableData = async () => {
const query =
postgrestQueryBuilder.current.filterQuery.url.toString() +
'&' +
postgrestQueryBuilder.current.sortQuery.url.toString() +
'&' +
postgrestQueryBuilder.current.paginationQuery.url.toString();
Feature: Import export tjdb schema (#5752) * add ability to import export app and tjdb schema * init * feat ::global settings popover new ui * feat :: ui for version export modal * fix :: import export modal * cleanup * ui updates * header footer style fixes * closing settings modal while showing export modal * style fix header * feat :: added button to download table schema * fix :: styling for fx * add ability to import and export apps with tjdb schema * handle duplicate table in workspace * fix table rename * fix selected table on edit and delete * fix invalid toast on table delete * fix column default value * handle exports to strip '::' and quotes * make import/export backward compatible * handle page redirects based on resource import * handle import without tjdb schema * fix column delete and addition * make data migrations to be run per organizations * wip * update migration * fix credentials to be included * fix specific version export * make use of apps ability for import export resource * fix import navigation * fix lint * fix failing tests * fix lint * enable tjdb for public apps * update export error message on tjdb table blank * fix table not selected after creation * fix :: styling for imp exp modal , and functionality bug fixes after dev merge * fixes blank slate and columns selection * fix table delete * fix invalid toast on table edit * fix column information missing tjdb query manager * make ds imports to either reuse global or create * export only unique table ids * create default datasources if not present in export data * reuse existing table on imports * add timestamp to table name if name already exists * add ability to clone with tjdb * make imports work with marketplace plugin * skip dataqueries for which plugins are not installed * fix filter input width * fix failing spec * fix marketplace plugin installation in diff workspaces * fix check for plugin installed in workspace * fix export when table name is empty --------- Co-authored-by: stepinfwd <stepinfwd@gmail.com>
2023-08-28 15:53:15 +00:00
const { headers, data, error } = await tooljetDatabaseService.findOne(organizationId, selectedTable.id, query);
if (error) {
toast.error(error?.message ?? 'Something went wrong');
return;
}
const totalRecords = headers['content-range'].split('/')[1] || 0;
if (Array.isArray(data)) {
setTotalRecords(totalRecords);
setSelectedTableData(data);
}
};
const handleBuildFilterQuery = (filters) => {
postgrestQueryBuilder.current.filterQuery = new PostgrestQueryBuilder();
Object.keys(filters).map((key) => {
if (!isEmpty(filters[key])) {
const { column, operator, value } = filters[key];
if (!isEmpty(column) && !isEmpty(operator) && !isEmpty(value)) {
postgrestQueryBuilder.current.filterQuery.filter(column, operator, value);
}
}
});
updateSelectedTableData();
};
const buildPaginationQuery = (limit, offset) => {
postgrestQueryBuilder.current.paginationQuery.limit(limit);
postgrestQueryBuilder.current.paginationQuery.offset(offset);
updateSelectedTableData();
};
const resetSortQuery = () => {
postgrestQueryBuilder.current.sortQuery = new PostgrestQueryBuilder();
postgrestQueryBuilder.current.paginationQuery.limit(50);
postgrestQueryBuilder.current.paginationQuery.offset(0);
handleBuildSortQuery({});
};
const resetFilterQuery = () => {
postgrestQueryBuilder.current.filterQuery = new PostgrestQueryBuilder();
postgrestQueryBuilder.current.paginationQuery.limit(50);
postgrestQueryBuilder.current.paginationQuery.offset(0);
handleBuildFilterQuery({});
};
const resetAll = () => {
Feature: Import export tjdb schema (#5752) * add ability to import export app and tjdb schema * init * feat ::global settings popover new ui * feat :: ui for version export modal * fix :: import export modal * cleanup * ui updates * header footer style fixes * closing settings modal while showing export modal * style fix header * feat :: added button to download table schema * fix :: styling for fx * add ability to import and export apps with tjdb schema * handle duplicate table in workspace * fix table rename * fix selected table on edit and delete * fix invalid toast on table delete * fix column default value * handle exports to strip '::' and quotes * make import/export backward compatible * handle page redirects based on resource import * handle import without tjdb schema * fix column delete and addition * make data migrations to be run per organizations * wip * update migration * fix credentials to be included * fix specific version export * make use of apps ability for import export resource * fix import navigation * fix lint * fix failing tests * fix lint * enable tjdb for public apps * update export error message on tjdb table blank * fix table not selected after creation * fix :: styling for imp exp modal , and functionality bug fixes after dev merge * fixes blank slate and columns selection * fix table delete * fix invalid toast on table edit * fix column information missing tjdb query manager * make ds imports to either reuse global or create * export only unique table ids * create default datasources if not present in export data * reuse existing table on imports * add timestamp to table name if name already exists * add ability to clone with tjdb * make imports work with marketplace plugin * skip dataqueries for which plugins are not installed * fix filter input width * fix failing spec * fix marketplace plugin installation in diff workspaces * fix check for plugin installed in workspace * fix export when table name is empty --------- Co-authored-by: stepinfwd <stepinfwd@gmail.com>
2023-08-28 15:53:15 +00:00
console.log('resetAll');
postgrestQueryBuilder.current.sortQuery = new PostgrestQueryBuilder();
postgrestQueryBuilder.current.paginationQuery.limit(50);
postgrestQueryBuilder.current.paginationQuery.offset(0);
postgrestQueryBuilder.current.filterQuery = new PostgrestQueryBuilder();
handleBuildSortQuery({});
};
return {
handleBuildFilterQuery,
handleBuildSortQuery,
buildPaginationQuery,
resetSortQuery,
resetFilterQuery,
resetAll,
};
};