diff --git a/packages/studiocms_core/src/sdk-utils/databaseUtils.ts b/packages/studiocms_core/src/sdk-utils/databaseUtils.ts new file mode 100644 index 000000000..491fba9d7 --- /dev/null +++ b/packages/studiocms_core/src/sdk-utils/databaseUtils.ts @@ -0,0 +1,164 @@ +/// +import { db, eq } from 'astro:db'; +import { CMSSiteConfigId } from '../consts'; +import { + tsOAuthAccounts, + tsPageContent, + tsPageData, + tsPageDataCategories, + tsPageDataTags, + tsPermissions, + tsSessionTable, + tsSiteConfig, + tsUsers, +} from '../db/tsTables'; +import type { + CombinedPageData, + CombinedUserData, + CurrentTables, + SimplifiedTables, + tsPageDataCategoriesSelect, + tsPageDataTagsSelect, +} from './types'; + +export async function getDatabaseRaw(database: CurrentTables) { + switch (database) { + case 'users': { + const users = await db.select().from(tsUsers); + return users; + } + case 'oAuthAccounts': { + const oAuthAccounts = await db.select().from(tsOAuthAccounts); + return oAuthAccounts; + } + case 'sessionTable': { + const sessionTable = await db.select().from(tsSessionTable); + return sessionTable; + } + case 'permissions': { + const permissions = await db.select().from(tsPermissions); + return permissions; + } + case 'pageData': { + const pageData = await db.select().from(tsPageData); + return pageData; + } + case 'pageDataTags': { + const pageDataTags = await db.select().from(tsPageDataTags); + return pageDataTags; + } + case 'pageDataCategories': { + const pageDataCategories = await db.select().from(tsPageDataCategories); + return pageDataCategories; + } + case 'pageContent': { + const pageContent = await db.select().from(tsPageContent); + return pageContent; + } + case 'siteConfig': { + const siteConfig = await db + .select() + .from(tsSiteConfig) + .where(eq(tsSiteConfig.id, CMSSiteConfigId)) + .get(); + return siteConfig; + } + } +} + +export async function getDatabase(database: SimplifiedTables) { + switch (database) { + case 'users': { + const combinedUserData: CombinedUserData[] = []; + + 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 UserData: CombinedUserData = { + ...user, + oAuthData: oAuthData, + sessionData: sessionData, + permissionsData: permissionsData, + }; + + combinedUserData.push(UserData); + } + + return combinedUserData; + } + case 'pages': { + const pages: CombinedPageData[] = []; + + const pageData = await db.select().from(tsPageData); + + for (const page of pageData) { + const categories: tsPageDataCategoriesSelect[] = []; + const tags: tsPageDataTagsSelect[] = []; + + for (const category of page.catagories as number[]) { + const categoryData = await db + .select() + .from(tsPageDataCategories) + .where(eq(tsPageDataCategories.id, category)) + .get(); + 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(); + if (tagData) { + tags.push(tagData); + } + } + + const contentData = await db + .select() + .from(tsPageContent) + .where(eq(tsPageContent.contentId, page.id)); + + const PageData: CombinedPageData = { + ...page, + categories: categories, + tags: tags, + content: contentData, + }; + + pages.push(PageData); + } + + return pages; + } + case 'config': { + const siteConfig = await db + .select() + .from(tsSiteConfig) + .where(eq(tsSiteConfig.id, CMSSiteConfigId)) + .get(); + return siteConfig; + } + } +} diff --git a/packages/studiocms_core/src/sdk-utils/types.ts b/packages/studiocms_core/src/sdk-utils/types.ts new file mode 100644 index 000000000..ede02a2b5 --- /dev/null +++ b/packages/studiocms_core/src/sdk-utils/types.ts @@ -0,0 +1,50 @@ +import type { + tsOAuthAccounts, + tsPageContent, + tsPageData, + tsPageDataCategories, + tsPageDataTags, + tsPermissions, + tsSessionTable, + tsUsers, +} from '../db/tsTables'; + +const currentTables = [ + 'users', + 'oAuthAccounts', + 'sessionTable', + 'permissions', + 'pageData', + 'pageDataTags', + 'pageDataCategories', + 'pageContent', + 'siteConfig', +] as const; + +export const simplifiedTables = ['users', 'pages', 'config'] as const; + +export type CurrentTables = (typeof currentTables)[number]; +export type SimplifiedTables = (typeof simplifiedTables)[number]; + +export type tsUsersSelect = typeof tsUsers.$inferSelect; +export type tsOAuthAccountsSelect = typeof tsOAuthAccounts.$inferSelect; +export type tsSessionTableSelect = typeof tsSessionTable.$inferSelect; +export type tsPermissionsSelect = typeof tsPermissions.$inferSelect; +export type tsPageDataSelect = typeof tsPageData.$inferSelect; +export type tsPageDataTagsSelect = typeof tsPageDataTags.$inferSelect; +export type tsPageDataCategoriesSelect = typeof tsPageDataCategories.$inferSelect; +export type tsPageContentSelect = typeof tsPageContent.$inferSelect; + +export type PageDataStripped = Omit; + +export interface CombinedUserData extends tsUsersSelect { + oAuthData: tsOAuthAccountsSelect | undefined; + sessionData: tsSessionTableSelect | undefined; + permissionsData: tsPermissionsSelect | undefined; +} + +export interface CombinedPageData extends PageDataStripped { + categories: tsPageDataCategoriesSelect[]; + tags: tsPageDataTagsSelect[]; + content: tsPageContentSelect[]; +} diff --git a/packages/studiocms_dashboard/src/routes/studiocms_api/pages/create.ts b/packages/studiocms_dashboard/src/routes/studiocms_api/pages/create.ts index ced1f6cba..180a393ff 100644 --- a/packages/studiocms_dashboard/src/routes/studiocms_api/pages/create.ts +++ b/packages/studiocms_dashboard/src/routes/studiocms_api/pages/create.ts @@ -98,7 +98,9 @@ export async function POST(context: APIContext): Promise { return simpleResponse(500, 'Error creating page'); } - await astroDb().pageContent().insert({ id: newPage.id, lang: 'default', content }); + await astroDb() + .pageContent() + .insert({ contentId: newPage.id, contentLang: 'default', content }); } catch (error) { if (error instanceof Error) { logger.error(error.message); diff --git a/packages/studiocms_dashboard/src/utils/astroDb.ts b/packages/studiocms_dashboard/src/utils/astroDb.ts index b03d9313a..0a92a1a58 100644 --- a/packages/studiocms_dashboard/src/utils/astroDb.ts +++ b/packages/studiocms_dashboard/src/utils/astroDb.ts @@ -4,72 +4,21 @@ import { db, eq } from 'astro:db'; import { CMSSiteConfigId } from '@studiocms/core/consts'; import { tsPageContent, tsPageData, tsSiteConfig } from '@studiocms/core/db/tsTables'; -type PageDataType = { - id: string; - package: string; - title: string; - description: string; - showOnNav: boolean; - publishedAt: Date; - updatedAt: Date | null; - slug: string; - contentLang: string; - heroImage: string; -}; - -type PageDataInsert = { - title: string; - description: string; - slug: string; - package: string; - showOnNav: boolean; - publishedAt: Date; - contentLang: string; - heroImage: string; -}; - -type PageDataUpdate = { - id: string; - package: string; - title: string; - description: string; - showOnNav: boolean; - updatedAt: Date | null; - slug: string; - heroImage: string; -}; - -type PageDataReturnID = { - id: string; -}; - -type PageContentInsert = { - id: string; - lang: string; - content: string; -}; - -type PageContentUpdate = { - content: string; - id: string; -}; +type PageDataInsert = typeof tsPageData.$inferInsert; +type PageDataSelect = typeof tsPageData.$inferSelect; +type PageDataReturnID = Pick; -type SiteConfigUpdate = { - title: string; - description: string; -}; +type PageContentInsert = typeof tsPageContent.$inferInsert; +type PageContentSelect = typeof tsPageContent.$inferSelect; -type SiteConfigReturn = { - id: number; - title: string; - description: string; -}; +type SiteConfigInsert = typeof tsSiteConfig.$inferInsert; +type SiteConfigSelect = typeof tsSiteConfig.$inferSelect; export const astroDb = () => { return { pageData() { return { - async getBySlug(slug: string, pkg: string): Promise { + async getBySlug(slug: string, pkg: string): Promise { const pageData = await db .select() .from(tsPageData) @@ -91,12 +40,15 @@ export const astroDb = () => { id: randomUUID(), slug: data.slug, title: data.title, - package: data.package, + package: data.package || 'studiocms', description: data.description, contentLang: contentLang, - heroImage: data.heroImage, - publishedAt: data.publishedAt, - showOnNav: data.showOnNav, + heroImage: data.heroImage || '', + publishedAt: data.publishedAt || new Date(), + showOnNav: data.showOnNav || false, + catagories: data.catagories || [], + tags: data.tags || [], + updatedAt: new Date(), }) .returning({ id: tsPageData.id }) .catch((error) => { @@ -106,7 +58,7 @@ export const astroDb = () => { return newEntry.pop(); }, - async update(data: PageDataUpdate) { + async update(data: PageDataSelect) { await db .update(tsPageData) .set({ @@ -135,15 +87,15 @@ export const astroDb = () => { .insert(tsPageContent) .values({ id: randomUUID(), - contentId: data.id, - contentLang: data.lang, - content: data.content, + contentId: data.contentId, + contentLang: data.contentLang || 'default', + content: data.content || '', }) .catch((error) => { logger.error(error); }); }, - async update(data: PageContentUpdate) { + async update(data: PageContentSelect) { await db .update(tsPageContent) .set({ content: data.content }) @@ -156,7 +108,7 @@ export const astroDb = () => { }, siteConfig() { return { - async update(data: SiteConfigUpdate): Promise { + async update(data: SiteConfigInsert): Promise { return await db .update(tsSiteConfig) .set(data)