Skip to content

Commit

Permalink
Refactor databaseUtils to use batch queries for improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Adammatthiesen committed Dec 10, 2024
1 parent 65486de commit ac96862
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 43 deletions.
53 changes: 12 additions & 41 deletions packages/studiocms_core/src/sdk-utils/databaseUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/studiocms_core/src/sdk-utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export type tsPageContentSelect = typeof tsPageContent.$inferSelect;
export type PageDataStripped = Omit<tsPageDataSelect, 'catagories' | 'tags'>;

export interface CombinedUserData extends tsUsersSelect {
oAuthData: tsOAuthAccountsSelect | undefined;
sessionData: tsSessionTableSelect | undefined;
oAuthData: tsOAuthAccountsSelect[] | undefined;
permissionsData: tsPermissionsSelect | undefined;
}

Expand Down

0 comments on commit ac96862

Please sign in to comment.