From ac968621356c9daf1a9d0c17daf44c1039cfa495 Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Mon, 9 Dec 2024 23:00:41 -0800 Subject: [PATCH] Refactor databaseUtils to use batch queries for improved performance --- .../src/sdk-utils/databaseUtils.ts | 53 +++++-------------- .../studiocms_core/src/sdk-utils/types.ts | 3 +- 2 files changed, 13 insertions(+), 43 deletions(-) diff --git a/packages/studiocms_core/src/sdk-utils/databaseUtils.ts b/packages/studiocms_core/src/sdk-utils/databaseUtils.ts index 5b1579cebf..53c5d3bc49 100644 --- a/packages/studiocms_core/src/sdk-utils/databaseUtils.ts +++ b/packages/studiocms_core/src/sdk-utils/databaseUtils.ts @@ -74,29 +74,15 @@ export async function getDatabase(database: SimplifiedTables) { const users = await db.select().from(tsUsers); for (const user of users) { - const oAuthData = await db - .select() - .from(tsOAuthAccounts) - .where(eq(tsOAuthAccounts.userId, user.id)) - .get(); - - const sessionData = await db - .select() - .from(tsSessionTable) - .where(eq(tsSessionTable.userId, user.id)) - .get(); - - const permissionsData = await db - .select() - .from(tsPermissions) - .where(eq(tsPermissions.user, user.id)) - .get(); + const [oAuthData, permissionData] = await db.batch([ + db.select().from(tsOAuthAccounts).where(eq(tsOAuthAccounts.userId, user.id)), + db.select().from(tsPermissions).where(eq(tsPermissions.user, user.id)), + ]); const UserData: CombinedUserData = { ...user, oAuthData: oAuthData, - sessionData: sessionData, - permissionsData: permissionsData, + permissionsData: permissionData.pop(), }; combinedUserData.push(UserData); @@ -113,23 +99,20 @@ export async function getDatabase(database: SimplifiedTables) { const categories: tsPageDataCategoriesSelect[] = []; const tags: tsPageDataTagsSelect[] = []; + const [allCategories, allTags] = await db.batch([ + db.select().from(tsPageDataCategories), + db.select().from(tsPageDataTags), + ]); + for (const category of page.catagories as number[]) { - const categoryData = await db - .select() - .from(tsPageDataCategories) - .where(eq(tsPageDataCategories.id, category)) - .get(); + const categoryData = allCategories.find((cat) => cat.id === category); if (categoryData) { categories.push(categoryData); } } for (const tag of page.tags as number[]) { - const tagData = await db - .select() - .from(tsPageDataTags) - .where(eq(tsPageDataTags.id, tag)) - .get(); + const tagData = allTags.find((t) => t.id === tag); if (tagData) { tags.push(tagData); } @@ -159,18 +142,6 @@ export async function getDatabase(database: SimplifiedTables) { .where(eq(tsSiteConfig.id, CMSSiteConfigId)) .get(); - if (!siteConfig) { - return { - id: CMSSiteConfigId, - description: '', - title: 'StudioCMS', - defaultOgImage: null, - siteIcon: null, - loginPageBackground: 'studiocms-curves', - loginPageCustomImage: null, - }; - } - return siteConfig; } } diff --git a/packages/studiocms_core/src/sdk-utils/types.ts b/packages/studiocms_core/src/sdk-utils/types.ts index ede02a2b5a..7738a935e1 100644 --- a/packages/studiocms_core/src/sdk-utils/types.ts +++ b/packages/studiocms_core/src/sdk-utils/types.ts @@ -38,8 +38,7 @@ export type tsPageContentSelect = typeof tsPageContent.$inferSelect; export type PageDataStripped = Omit; export interface CombinedUserData extends tsUsersSelect { - oAuthData: tsOAuthAccountsSelect | undefined; - sessionData: tsSessionTableSelect | undefined; + oAuthData: tsOAuthAccountsSelect[] | undefined; permissionsData: tsPermissionsSelect | undefined; }