Skip to content

Commit

Permalink
start of new "sdk" type utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Adammatthiesen committed Dec 10, 2024
1 parent 5102b7f commit 4a676ea
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 70 deletions.
164 changes: 164 additions & 0 deletions packages/studiocms_core/src/sdk-utils/databaseUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/// <reference types="@astrojs/db" />
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;
}
}
}
50 changes: 50 additions & 0 deletions packages/studiocms_core/src/sdk-utils/types.ts
Original file line number Diff line number Diff line change
@@ -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<tsPageDataSelect, 'catagories' | 'tags'>;

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[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export async function POST(context: APIContext): Promise<Response> {
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);
Expand Down
90 changes: 21 additions & 69 deletions packages/studiocms_dashboard/src/utils/astroDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PageDataSelect, 'id'>;

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<PageDataType | undefined> {
async getBySlug(slug: string, pkg: string): Promise<PageDataSelect | undefined> {
const pageData = await db
.select()
.from(tsPageData)
Expand All @@ -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) => {
Expand All @@ -106,7 +58,7 @@ export const astroDb = () => {

return newEntry.pop();
},
async update(data: PageDataUpdate) {
async update(data: PageDataSelect) {
await db
.update(tsPageData)
.set({
Expand Down Expand Up @@ -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 })
Expand All @@ -156,7 +108,7 @@ export const astroDb = () => {
},
siteConfig() {
return {
async update(data: SiteConfigUpdate): Promise<SiteConfigReturn | undefined> {
async update(data: SiteConfigInsert): Promise<SiteConfigSelect | undefined> {
return await db
.update(tsSiteConfig)
.set(data)
Expand Down

0 comments on commit 4a676ea

Please sign in to comment.