Skip to content

Commit

Permalink
Refactor getDatabaseTable function to use object literal syntax for t…
Browse files Browse the repository at this point in the history
…able selection
  • Loading branch information
Adammatthiesen committed Dec 11, 2024
1 parent 88cbaaa commit cc5422f
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 431 deletions.
73 changes: 32 additions & 41 deletions packages/studiocms_core/src/sdk-utils/get/getDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,52 @@ import type { CombinedPageData, CombinedUserData, STUDIOCMS_SDK, SiteConfig } fr
import { collectPageData, collectUserData } from '../utils';

/**
* Retrieves data from the database based on the specified table.
* Provides methods to retrieve various types of data from the database.
*
* @param database - The name of the database table to retrieve data from.
* It can be one of the following values: 'users', 'pages', or 'config'.
* @type {STUDIOCMS_SDK['GET']['database']}
*
* @returns A promise that resolves to the data retrieved from the specified table.
*
* - If `database` is 'users', it returns an array of `CombinedUserData` objects.
* - If `database` is 'pages', it returns an array of `CombinedPageData` objects.
* - If `database` is 'config', it returns the site configuration object.
*
* @throws Will throw an error if the specified database table is not recognized.
* @property {Function} users - Asynchronously retrieves and combines user data from the database.
* @property {Function} pages - Asynchronously retrieves and combines page data from the database.
* @property {Function} config - Asynchronously retrieves the site configuration from the database.
*/
export const getDatabase: STUDIOCMS_SDK['GET']['database'] = async (database) => {
switch (database) {
case 'users': {
const combinedUserData: CombinedUserData[] = [];

const users = await db.select().from(tsUsers);
export const getDatabase: STUDIOCMS_SDK['GET']['database'] = {
users: async () => {
const combinedUserData: CombinedUserData[] = [];

for (const user of users) {
const UserData = await collectUserData(user);
const users = await db.select().from(tsUsers);

combinedUserData.push(UserData);
}
for (const user of users) {
const UserData = await collectUserData(user);

return combinedUserData;
combinedUserData.push(UserData);
}
case 'pages': {
const pages: CombinedPageData[] = [];

const pagesRaw = await db.select().from(tsPageData);
return combinedUserData;
},
pages: async () => {
const pages: CombinedPageData[] = [];

for (const page of pagesRaw) {
const PageData = await collectPageData(page);
const pagesRaw = await db.select().from(tsPageData);

pages.push(PageData);
}
for (const page of pagesRaw) {
const PageData = await collectPageData(page);

return pages;
pages.push(PageData);
}
case 'config': {
const siteConfig = await db
.select()
.from(tsSiteConfig)
.where(eq(tsSiteConfig.id, CMSSiteConfigId))
.get();

if (!siteConfig) return undefined;
return pages;
},
config: async () => {
const siteConfig = await db
.select()
.from(tsSiteConfig)
.where(eq(tsSiteConfig.id, CMSSiteConfigId))
.get();

return siteConfig as SiteConfig;
}
default:
throw new Error(`Database table '${database}' not recognized.`);
}
if (!siteConfig) return undefined;

return siteConfig as SiteConfig;
},
};

export default getDatabase;
Loading

0 comments on commit cc5422f

Please sign in to comment.