Skip to content

Commit

Permalink
Merge pull request #380 from aXenDeveloper/performerce/reduce_query_t…
Browse files Browse the repository at this point in the history
…o_backend_in_middleware

perf(frontend): Reduce calls to API
  • Loading branch information
aXenDeveloper authored Jun 14, 2024
2 parents 02ef9a4 + d4ecc03 commit 19fd97c
Show file tree
Hide file tree
Showing 75 changed files with 942 additions and 838 deletions.
12 changes: 12 additions & 0 deletions backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ input HslColorInput {
s: Int!
}

type LanguagesCoreMiddleware {
code: String!
default: Boolean!
enabled: Boolean!
}

enum LayoutAdminInstallEnum {
ACCOUNT
DATABASE
Expand Down Expand Up @@ -231,6 +237,7 @@ type Query {
core_files__show(cursor: Int, first: Int, last: Int, search: String, sortBy: ShowCoreFilesSortByArgs): ShowCoreFilesObj!
core_languages__show(cursor: Int, first: Int, last: Int, search: String, sortBy: ShowCoreLanguagesSortByArgs): ShowCoreLanguagesObj!
core_members__show(cursor: Int, first: Int, last: Int, name_seo: String, search: String, sortBy: ShowCoreMembersSortByArgs): ShowCoreMembersObj!
core_middleware__show: ShowCoreMiddlewareObj!
core_nav__show(cursor: Int, first: Int, last: Int): ShowCoreNavObj!
core_plugins__show: [ShowCorePluginsObj!]!
core_sessions__authorization: AuthorizationCoreSessionsObj!
Expand Down Expand Up @@ -563,6 +570,11 @@ enum ShowCoreMembersSortingColumnEnum {
reactions
}

type ShowCoreMiddlewareObj {
languages: [LanguagesCoreMiddleware!]!
plugins: [String!]!
}

type ShowCoreNav {
children: [ShowCoreNavItem!]!
description: [TextLanguage!]!
Expand Down
5 changes: 5 additions & 0 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ export interface ConfigType {
};
sticky: boolean;
};
langs: {
code: string;
default: boolean;
enabled: boolean;
}[];
rebuild_required: {
langs: boolean;
plugins: boolean;
Expand Down
22 changes: 19 additions & 3 deletions backend/src/functions/update-object.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
export function updateObject<T>(config: T, defaultData: T): T {
export function updateObject<T extends Record<string, any>>(
config: T,
defaultData: T
): T {
const updatedConfig = config;
for (const key in defaultData) {
if (typeof defaultData[key] === "object" && defaultData[key] !== null) {
if (Array.isArray(defaultData[key])) {
// If the key corresponds to an array and it's not empty, don't edit
if (!config[key] || config[key].length === 0) {
updatedConfig[key] = [] as any;
}
} else if (
typeof defaultData[key] === "object" &&
defaultData[key] !== null
) {
// Handle nested objects
if (!config[key]) {
updatedConfig[key] = {} as T[Extract<keyof T, string>];
}
updateObject(config[key], defaultData[key]);
updatedConfig[key] = updateObject(
(config[key] || {}) as T[Extract<keyof T, string>],
defaultData[key]
);
} else {
// Handle primitive values
if (!config[key]) {
updatedConfig[key] = defaultData[key];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { UseGuards } from "@nestjs/common";

import { EditAdminMembersService } from "./edit.service";
import { EditAdminMembersArgs } from "./dto/edit.args";
import { EditAdminMembersObj } from "./dto/edit.obj";

import { AdminAuthGuards } from "@/utils/guards/admin-auth.guard";
import { EditAdminMembersObj } from "./dto/edit.obj";

@Resolver()
export class EditAdminMembersResolver {
Expand Down
5 changes: 2 additions & 3 deletions backend/src/plugins/core/admin/members/edit/edit.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Injectable } from "@nestjs/common";
import { eq } from "drizzle-orm";
import { NotFoundError, AccessDeniedError } from "@vitnode/backend";

import { EditAdminMembersArgs } from "./dto/edit.args";
import { EditAdminMembersObj } from "./dto/edit.obj";

import { core_users } from "@/plugins/core/admin/database/schema/users";
import { DatabaseService } from "@/database/database.service";
import { NotFoundError } from "@vitnode/backend";
import { AccessDeniedError } from "@vitnode/backend";
import { EditAdminMembersObj } from "./dto/edit.obj";

@Injectable()
export class EditAdminMembersService {
Expand Down
8 changes: 7 additions & 1 deletion backend/src/plugins/core/middleware/middleware.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Module } from "@nestjs/common";

import { CoreMiddlewareCron } from "./middleware.cron";
import { ShowCoreMiddlewareService } from "./show/show.service";
import { ShowCoreMiddlewareResolver } from "./show/show.resolver";

@Module({
providers: [CoreMiddlewareCron]
providers: [
CoreMiddlewareCron,
ShowCoreMiddlewareService,
ShowCoreMiddlewareResolver
]
})
export class CoreMiddlewareModule {}
22 changes: 22 additions & 0 deletions backend/src/plugins/core/middleware/show/dto/languages.obj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Field, ObjectType } from "@nestjs/graphql";

@ObjectType()
export class LanguagesCoreMiddleware {
@Field(() => String)
code: string;

@Field(() => Boolean)
enabled: boolean;

@Field(() => Boolean)
default: boolean;
}

@ObjectType()
export class ShowCoreMiddlewareObj {
@Field(() => [LanguagesCoreMiddleware])
languages: LanguagesCoreMiddleware[];

@Field(() => [String])
plugins: string[];
}
14 changes: 14 additions & 0 deletions backend/src/plugins/core/middleware/show/show.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Query, Resolver } from "@nestjs/graphql";

import { ShowCoreMiddlewareService } from "./show.service";
import { ShowCoreMiddlewareObj } from "./dto/languages.obj";

@Resolver()
export class ShowCoreMiddlewareResolver {
constructor(private readonly service: ShowCoreMiddlewareService) {}

@Query(() => ShowCoreMiddlewareObj)
async core_middleware__show(): Promise<ShowCoreMiddlewareObj> {
return this.service.languages();
}
}
24 changes: 24 additions & 0 deletions backend/src/plugins/core/middleware/show/show.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { readdir } from "fs/promises";

import { Injectable } from "@nestjs/common";

import { ShowCoreMiddlewareObj } from "./dto/languages.obj";

import { ABSOLUTE_PATHS, getConfigFile } from "@/config";

@Injectable()
export class ShowCoreMiddlewareService {
async languages(): Promise<ShowCoreMiddlewareObj> {
const config = getConfigFile();

const plugins = await readdir(ABSOLUTE_PATHS.plugins);

return {
languages: config.langs,
plugins: [
"admin",
...plugins.filter(plugin => !["plugins.module.ts"].includes(plugin))
]
};
}
}
4 changes: 1 addition & 3 deletions backend/src/utils/actions/finish-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import { db } from "@/database/client";
fs.readdir(join(process.cwd(), "src", "plugins"), async (err, plugins) => {
await Promise.all(
plugins
.filter(
plugin => !["database", "plugins.module.ts", "core"].includes(plugin)
)
.filter(plugin => !["plugins.module.ts", "core"].includes(plugin))
.map(async plugin => {
try {
await migrate({ pluginCode: plugin });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ const getData = async () => {
Admin_Blog_Categories__ShowQuery,
Admin_Blog_Categories__ShowQueryVariables
>({
query: Admin_Blog_Categories__Show,
cache: "force-cache"
query: Admin_Blog_Categories__Show
});

return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
SearchParamsPagination
} from "@/plugins/core/hooks/utils/use-pagination-api-ssr";
import { fetcher } from "@/graphql/fetcher";
import { FilesAdvancedCoreAdminView } from "@/plugins/admin/views/core/advanced/files/files-advanced-core-adminpview";
import { FilesAdvancedCoreAdminView } from "@/plugins/admin/views/core/advanced/files/files-advanced-core-admin-view";

const getData = async (variables: Admin__Core_Files__ShowQueryVariables) => {
const { data } = await fetcher<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Admin__Core_Plugins__Show__ItemQueryVariables
} from "@/graphql/hooks";
import { fetcher, ErrorType } from "@/graphql/fetcher";
import { AdminCoreApiTags } from "@/plugins/admin/api-tags";

export const getPluginDataAdmin = async (
variables: Admin__Core_Plugins__Show__ItemQueryVariables
Expand All @@ -15,11 +14,7 @@ export const getPluginDataAdmin = async (
Admin__Core_Plugins__Show__ItemQueryVariables
>({
query: Admin__Core_Plugins__Show__Item,
variables,
cache: "force-cache",
next: {
tags: [AdminCoreApiTags.Admin__Core_Plugins__Show__Item]
}
variables
});

return { data };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ const getData = async (variables: Admin__Core_Plugins__ShowQueryVariables) => {
Admin__Core_Plugins__ShowQueryVariables
>({
query: Admin__Core_Plugins__Show,
variables,
next: {
tags: ["Admin__Core_Plugins__Show"]
}
variables
});

return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const getSessionAdminData = async () => {
Admin__Sessions__AuthorizationQuery,
Admin__Sessions__AuthorizationQueryVariables
>({
query: Admin__Sessions__Authorization
query: Admin__Sessions__Authorization,
cache: "force-cache"
});

return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const getData = async () => {
Admin__Install__LayoutQuery,
Admin__Install__LayoutQueryVariables
>({
query: Admin__Install__Layout
query: Admin__Install__Layout,
cache: "force-cache"
});

return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const getData = async (variables: Core_Members__Files__ShowQueryVariables) => {
Core_Members__Files__ShowQueryVariables
>({
query: Core_Members__Files__Show,
variables
variables,
cache: "force-cache"
});

return data;
Expand Down
3 changes: 2 additions & 1 deletion frontend/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const getData = async () => {
Core_MiddlewareQuery,
Core_MiddlewareQueryVariables
>({
query: Core_Middleware
query: Core_Middleware,
cache: "force-cache"
});

return data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"use server";

import { revalidateTag } from "next/cache";
import { revalidatePath } from "next/cache";

import {
Core_Editor_Files__Upload,
Core_Editor_Files__UploadMutation,
Core_Editor_Files__UploadMutationVariables
} from "@/graphql/hooks";
import { fetcher } from "@/graphql/fetcher";
import { CoreApiTags } from "@/plugins/admin/api-tags";

export const uploadMutationApi = async (formData: FormData) => {
const file = formData.get("file") as File;
Expand All @@ -33,7 +32,7 @@ export const uploadMutationApi = async (formData: FormData) => {
]
});

revalidateTag(CoreApiTags.Core_Sessions__Authorization);
revalidatePath("/settings/files", "page");

return { data };
} catch (error) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use server";

import { revalidatePath } from "next/cache";

import {
Core_Editor_Files__Delete,
Core_Editor_Files__DeleteMutation,
Expand All @@ -19,6 +21,8 @@ export const deleteMutationApi = async (
variables
});

revalidatePath("/settings/files", "page");

return { data };
} catch (error) {
return { error };
Expand Down
4 changes: 4 additions & 0 deletions frontend/components/switchers/theme/mutation-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use server";

import { revalidatePath } from "next/cache";

import {
Core_Themes__Change,
Core_Themes__ChangeMutation,
Expand All @@ -23,6 +25,8 @@ export const mutationApi = async (
// Set cookie
setCookieFromApi({ res });

revalidatePath("/", "layout");

return { data };
} catch (error) {
return { error };
Expand Down
Loading

0 comments on commit 19fd97c

Please sign in to comment.