Skip to content

Commit

Permalink
♻️ refactor(data): improve data CRUD types
Browse files Browse the repository at this point in the history
  • Loading branch information
thrownullexception committed Dec 17, 2023
1 parent 29454bd commit 70fc7c9
Show file tree
Hide file tree
Showing 25 changed files with 120 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/backend/data/data.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class DataApiController {
queryFilters: QueryFilterSchema
): Promise<{ count: number }> {
return {
count: await this._dataApiService.count(entity, queryFilters),
count: await this._dataApiService.countData(entity, queryFilters),
};
}

Expand Down
50 changes: 30 additions & 20 deletions src/backend/data/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
EntitiesApiService,
entitiesApiService,
} from "../entities/entities.service";
import { PortalDataHooksService } from "./portal";
import { PortalDataHooksService, PortalQueryImplementation } from "./portal";
import { makeTableData } from "./utils";

const DEFAULT_LIST_LIMIT = 50;
Expand All @@ -46,7 +46,7 @@ export class DataApiService implements IDataApiService {
return this._rDBMSApiDataService;
}

async list(
async fetchData(
entity: string,
select: string[],
queryFilter: QueryFilterSchema,
Expand All @@ -55,12 +55,22 @@ export class DataApiService implements IDataApiService {
return await this.getDataAccessInstance().list(
entity,
select,
queryFilter, //
await PortalQueryImplementation.query(queryFilter, entity),
paginationFilters
);
}

async read<T>(
async countData(
entity: string,
queryFilter: QueryFilterSchema
): Promise<number> {
return await this.getDataAccessInstance().count(
entity,
await PortalQueryImplementation.query(queryFilter, entity)
);
}

async readData<T>(
entity: string,
select: string[],
query: Record<string, unknown>
Expand All @@ -78,7 +88,7 @@ export class DataApiService implements IDataApiService {
this._entitiesApiService.getEntityPrimaryField(entity),
]);

const data = await this.read<Record<string, unknown>>(
const data = await this.readData<Record<string, unknown>>(
entity,
relationshipSettings.fields,
{
Expand All @@ -98,12 +108,11 @@ export class DataApiService implements IDataApiService {
this._entitiesApiService.getAllowedCrudsFieldsToShow(entity, "details"),
column || this._entitiesApiService.getEntityPrimaryField(entity),
]);
const data = await this.read<Record<string, unknown>>(
const data = await this.readData<Record<string, unknown>>(
entity,
fieldsToShow,
{
[columnField]: id,
//
}
);
if (!data) {
Expand Down Expand Up @@ -166,7 +175,7 @@ export class DataApiService implements IDataApiService {
this._entitiesApiService.getEntityPrimaryField(entity),
]);

const data = await this.getDataAccessInstance().list(
const data = await this.fetchData(
entity,
[...relationshipSettings.fields, primaryField],
{
Expand All @@ -179,7 +188,6 @@ export class DataApiService implements IDataApiService {
},
})),
},
//
{
take: DEFAULT_LIST_LIMIT,
page: 1,
Expand All @@ -188,7 +196,7 @@ export class DataApiService implements IDataApiService {

return data.map((datum: Record<string, unknown>) => {
return {
value: datum[primaryField],
value: datum[primaryField] as string,
label: compileTemplateString(relationshipSettings.format, datum),
};
});
Expand All @@ -201,16 +209,16 @@ export class DataApiService implements IDataApiService {
): Promise<PaginatedData<Record<string, unknown>>> {
return makeTableData(
await Promise.all([
this.getDataAccessInstance().list(
this.fetchData(
entity,
await this._entitiesApiService.getAllowedCrudsFieldsToShow(
entity,
"table"
),
queryFilters, //
queryFilters,
paginationFilters
),
this.getDataAccessInstance().count(entity, queryFilters),
this.countData(entity, queryFilters),
]),
paginationFilters
);
Expand Down Expand Up @@ -282,9 +290,15 @@ export class DataApiService implements IDataApiService {
dataId: id,
});

await this.getDataAccessInstance().delete(entity, {
[await this._entitiesApiService.getEntityPrimaryField(entity)]: id,
}); //
await PortalQueryImplementation.delete({
entity,
id,
implementation: async () => {
await this.getDataAccessInstance().delete(entity, {
[await this._entitiesApiService.getEntityPrimaryField(entity)]: id,
});
},
});

await PortalDataHooksService.afterDelete({
beforeData,
Expand All @@ -294,10 +308,6 @@ export class DataApiService implements IDataApiService {
});
}

async count(entity: string, queryFilter: QueryFilterSchema): Promise<number> {
return await this.getDataAccessInstance().count(entity, queryFilter); //
}

private async getRelationshipSettings(entity: string): Promise<{
format: string;
fields: string[];
Expand Down
2 changes: 1 addition & 1 deletion src/backend/data/portal/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { PortalDataHooksService } from "./main";
export { PortalDataHooksService, PortalQueryImplementation } from "./main";
19 changes: 19 additions & 0 deletions src/backend/data/portal/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable max-classes-per-file */
import { noop } from "shared/lib/noop";
import { QueryFilterSchema } from "shared/types/data";
import { IDataApiService } from "../types";

export class PortalDataHooksService {
Expand Down Expand Up @@ -86,3 +87,21 @@ export class PortalDataHooksService {
noop(dataApiService, entity, dataId, beforeData);
}
}

export class PortalQueryImplementation {
static async delete(params: {
id: string;
entity: string;
implementation: () => Promise<void>;
}) {
noop(params);
}

static async query(
queryFilter: QueryFilterSchema,
entity: string
): Promise<QueryFilterSchema> {
noop(entity);
return queryFilter;
}
}
6 changes: 3 additions & 3 deletions src/backend/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ export interface IPaginationFilters {
export const FOR_CODE_COV = 1;

export interface IDataApiService extends IApplicationService {
list(
fetchData(
entity: string,
select: string[],
queryFilter: QueryFilterSchema,
dataFetchingModifiers: IPaginationFilters
): Promise<Record<string, unknown>[]>;

read(
readData(
entity: string,
select: string[],
query: Record<string, unknown>
): Promise<Record<string, unknown>>;

count(entity: string, queryFilter: QueryFilterSchema): Promise<number>;
countData(entity: string, queryFilter: QueryFilterSchema): Promise<number>;

create(
entity: string,
Expand Down
7 changes: 6 additions & 1 deletion src/backend/user-preferences/user-preferences.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
USER_PREFERENCES_CONFIG,
UserPreferencesKeys,
UserPreferencesValueType,
} from "shared/user-preferences/constants";
import { BadRequestError } from "backend/lib/errors";
import {
Expand All @@ -20,7 +21,11 @@ export class UserPreferenceApiController {
};
}

async upsert(username: string, key: string, value: unknown) {
async upsert<T extends UserPreferencesKeys>(
username: string,
key: T,
value: UserPreferencesValueType<T>
) {
await this._userPreferencesApiService.upsert(
username,
this.validateUserPreferencesKeys(key),
Expand Down
4 changes: 2 additions & 2 deletions src/backend/user-preferences/user-preferences.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export class UserPreferencesApiService implements IApplicationService {
);

if (value) {
return value as T;
return value as UserPreferencesValueType<T>;
}

return USER_PREFERENCES_CONFIG[key].defaultValue as T;
return USER_PREFERENCES_CONFIG[key].defaultValue;
}

async upsert<T extends UserPreferencesKeys>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function ActionButtons({
action={actionButton.action}
label={actionButton.label}
justIcon={justIcons}
isMakingActionRequest={actionButton.isMakingActionRequest}
icon={actionButton.icon}
/>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type IActionButton =
_type: "normal";
action: string | (() => void);
label: string;
isMakingActionRequest?: boolean;
icon: ButtonIconTypes;
order?: number;
}
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/hooks/auth/preferences.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function useUserPreference<T extends UserPreferencesKeys>(key: T) {
enabled: isAuthenticated === true,
returnUndefinedOnError: true,
errorMessage: MAKE_USER_PREFERENCE_CRUD_CONFIG(key).TEXT_LANG.NOT_FOUND,
defaultData: USER_PREFERENCES_CONFIG[key].defaultValue as T,
defaultData: USER_PREFERENCES_CONFIG[key].defaultValue,
selector: (data) => data.data,
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/hooks/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const ENTITY_REFERENCE_PATH = (entity: string, id: string) =>

export const ENTITY_LIST_PATH = (entity: string) => `/api/data/${entity}/list`;

export const CREATE_DATA_ENDPOINT_TO_CLEAR = (entity: string) => [
export const DATA_MUTATION_ENDPOINTS_TO_CLEAR = (entity: string) => [
ENTITY_TABLE_PATH(entity),
ENTITY_COUNT_PATH(entity),
ENTITY_LIST_PATH(entity),
Expand Down
18 changes: 4 additions & 14 deletions src/frontend/hooks/data/data.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ import { DataStates } from "frontend/lib/data/types";
import { useEntityCrudConfig } from "../entity/entity.config";
import { useMultipleEntityReferenceFields } from "../entity/entity.store";
import { isRouterParamEnabled } from "..";
import { DATA_MUTATION_QUERY_ENDPOINTS } from "./portal";
import {
CREATE_DATA_ENDPOINT_TO_CLEAR,
DATA_MUTATION_ENDPOINTS_TO_CLEAR,
ENTITY_COUNT_PATH,
ENTITY_DETAILS_PATH,
ENTITY_LIST_PATH,
ENTITY_REFERENCE_PATH,
ENTITY_TABLE_PATH,
} from "./constants";

export const useEntityDataDetails = ({
Expand Down Expand Up @@ -142,7 +139,7 @@ export function useEntityDataCreationMutation(entity: string) {
const apiMutateOptions = useWaitForResponseMutationOptions<
Record<string, string>
>({
endpoints: CREATE_DATA_ENDPOINT_TO_CLEAR(entity),
endpoints: DATA_MUTATION_ENDPOINTS_TO_CLEAR(entity),
smartSuccessMessage: ({ id }) => ({
message: entityCrudConfig.MUTATION_LANG.CREATE,
action: {
Expand All @@ -165,10 +162,8 @@ export function useEntityDataUpdationMutation(entity: string, id: string) {
Record<string, string>
>({
endpoints: [
ENTITY_TABLE_PATH(entity),
ENTITY_DETAILS_PATH(entity, id),
ENTITY_LIST_PATH(entity),
...DATA_MUTATION_QUERY_ENDPOINTS(entity),
...DATA_MUTATION_ENDPOINTS_TO_CLEAR(entity),
],
successMessage: entityCrudConfig.MUTATION_LANG.EDIT,
});
Expand All @@ -189,12 +184,7 @@ export function useEntityDataDeletionMutation(
const apiMutateOptions = useWaitForResponseMutationOptions<
Record<string, string>
>({
endpoints: [
ENTITY_TABLE_PATH(entity),
ENTITY_COUNT_PATH(entity),
ENTITY_LIST_PATH(entity),
...DATA_MUTATION_QUERY_ENDPOINTS(entity),
],
endpoints: DATA_MUTATION_ENDPOINTS_TO_CLEAR(entity),
onSuccessActionWithFormData: () => {
if (redirectTo) {
router.replace(redirectTo);
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/views/account/Preferences/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
UPDATE_USER_PREFERENCES_FORM_SCHEMA,
} from "./constants";
import { IUserPreferences } from "./types";
import { PortalUserPreferences } from "./portal";

export function UserPreferences() {
const userPreferences = useUserPreference("theme");
Expand Down Expand Up @@ -68,6 +69,7 @@ export function UserPreferences() {
/>
</ViewStateMachine>
</SectionBox>
<PortalUserPreferences />
</BaseAccountLayout>
);
}
1 change: 1 addition & 0 deletions src/frontend/views/account/Preferences/portal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PortalUserPreferences } from "./main";
3 changes: 3 additions & 0 deletions src/frontend/views/account/Preferences/portal/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function PortalUserPreferences() {
return null;
}
6 changes: 3 additions & 3 deletions src/frontend/views/data/Details/DetailsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { filterOutHiddenScalarColumns } from "../utils";
import { useEntityViewStateMachine } from "../useEntityViewStateMachine";
import { viewSpecialDataTypes } from "../viewSpecialDataTypes";
import { evalutePresentationScript } from "../evaluatePresentationScript";
import { PreDataDetails } from "./portal";

const ContentText = styled(Typo.SM)`
overflow-wrap: anywhere;
Expand All @@ -34,14 +35,12 @@ export function EntityDetailsView({
id,
entity,
displayFrom,
column,
}: {
id: string;
entity: string;
displayFrom: "details" | "canvas";
column?: string;
}) {
const dataDetails = useEntityDataDetails({ entity, entityId: id, column });
const dataDetails = useEntityDataDetails({ entity, entityId: id });
const entityFields = useEntityFields(entity);
const entityFieldTypes = useProcessedEntityFieldTypes(entity);
const hiddenDetailsColumns = useHiddenEntityColumns("details", entity);
Expand Down Expand Up @@ -96,6 +95,7 @@ export function EntityDetailsView({
</>
}
>
<PreDataDetails entity={entity} entityId={id} />
<div aria-label="Details Section">
{filterOutHiddenScalarColumns(
entityFields.data,
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/views/data/Details/RelationsDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ export function EntityRelationDetails() {
>
<EntityDetailsView
displayFrom="details"
id={viewEntityId}
id={idData}
entity={childEntity}
column={detailsColumn}
/>
</SectionBox>
)}
Expand Down
Loading

0 comments on commit 70fc7c9

Please sign in to comment.