diff --git a/packages/studiocms_core/src/sdk-utils/add/addDatabaseEntry.ts b/packages/studiocms_core/src/sdk-utils/add/addDatabaseEntry.ts index 05cf71a1..28f050a5 100644 --- a/packages/studiocms_core/src/sdk-utils/add/addDatabaseEntry.ts +++ b/packages/studiocms_core/src/sdk-utils/add/addDatabaseEntry.ts @@ -1,115 +1,118 @@ /// import { db } from 'astro:db'; import { tsPageContent, tsPageData } from '../../db/tsTables'; -import type { - addDatabaseEntryDatabase, - addDatabaseEntryInsertPage, - tsPageContentInsert, - tsPageDataInsert, -} from '../types'; +import type { addDatabaseEntryInsertPage, tsPageContentInsert, tsPageDataInsert } from '../types'; -/** - * Adds a new entry to the specified database table. - */ -export function addDatabaseEntry(database: addDatabaseEntryDatabase) { - switch (database) { - case 'pages': { - return { - /** - * Adds a new page entry to the database. - * - * @param pageData - An object containing the page metadata. - * @param pageContent - An object containing the page content. - * - * @returns A promise that resolves to an object containing the newly inserted page data and content. - * - * @throws Will throw an error if there is an error during the insertion process. - */ - insert: async ( - pageData: tsPageDataInsert, - pageContent: tsPageContentInsert - ): Promise => { - const newContentID = crypto.randomUUID().toString(); +interface AddDatabaseEntry { + pages: { + insert: ( + pageData: tsPageDataInsert, + pageContent: tsPageContentInsert + ) => Promise; + }; + pageContent: { + insert: () => void; + }; + tags: { + insert: () => void; + }; + categories: { + insert: () => void; + }; + permissions: { + insert: () => void; + }; +} - const { - title, - slug, - description, - authorId = null, - package: packageName = 'studiocms', - contentLang = 'default', - heroImage = '', - showOnNav = false, - showAuthor = false, - showContributors = false, - } = pageData; +export const addDatabaseEntry: AddDatabaseEntry = { + pages: { + /** + * Adds a new page entry to the database. + * + * @param pageData - An object containing the page metadata. + * @param pageContent - An object containing the page content. + * + * @returns A promise that resolves to an object containing the newly inserted page data and content. + * + * @throws Will throw an error if there is an error during the insertion process. + */ + insert: async ( + pageData: tsPageDataInsert, + pageContent: tsPageContentInsert + ): Promise => { + const newContentID = crypto.randomUUID().toString(); - const stringified = { - categories: JSON.stringify(pageData.categories), - tags: JSON.stringify(pageData.tags), - contributorIds: JSON.stringify(pageData.contributorIds), - }; + const { + title, + slug, + description, + authorId = null, + package: packageName = 'studiocms', + contentLang = 'default', + heroImage = '', + showOnNav = false, + showAuthor = false, + showContributors = false, + } = pageData; - const contentData = { - id: crypto.randomUUID().toString(), - contentId: newContentID, - contentLang: pageContent.contentLang || 'default', - content: pageContent.content || '', - }; + const stringified = { + categories: JSON.stringify(pageData.categories), + tags: JSON.stringify(pageData.tags), + contributorIds: JSON.stringify(pageData.contributorIds), + }; - const NOW = new Date(); + const contentData = { + id: crypto.randomUUID().toString(), + contentId: newContentID, + contentLang: pageContent.contentLang || 'default', + content: pageContent.content || '', + }; - const [newPageData, newPageContent] = await db - .batch([ - db - .insert(tsPageData) - .values({ - id: newContentID, - title, - slug, - description, - authorId, - contentLang, - heroImage, - showAuthor, - showContributors, - showOnNav, - package: packageName, - publishedAt: NOW, - updatedAt: NOW, - ...stringified, - }) - .returning({ id: tsPageData.id }), - db.insert(tsPageContent).values(contentData).returning({ id: tsPageContent.id }), - ]) - .catch((error) => { - throw new Error(error); - }); + const NOW = new Date(); + + const [newPageData, newPageContent] = await db + .batch([ + db + .insert(tsPageData) + .values({ + id: newContentID, + title, + slug, + description, + authorId, + contentLang, + heroImage, + showAuthor, + showContributors, + showOnNav, + package: packageName, + publishedAt: NOW, + updatedAt: NOW, + ...stringified, + }) + .returning({ id: tsPageData.id }), + db.insert(tsPageContent).values(contentData).returning({ id: tsPageContent.id }), + ]) + .catch((error) => { + throw new Error(error); + }); - return { - pageData: newPageData, - pageContent: newPageContent, - }; - }, - }; - } - case 'tags': { - return { - insert: async () => {}, - }; - } - case 'categories': { - return { - insert: async () => {}, - }; - } - case 'permissions': { return { - insert: async () => {}, + pageData: newPageData, + pageContent: newPageContent, }; - } - default: { - throw new Error('Invalid database entry'); - } - } -} + }, + }, + pageContent: { + insert: async () => {}, + }, + tags: { + insert: async () => {}, + }, + categories: { + insert: async () => {}, + }, + permissions: { + insert: async () => {}, + }, +}; diff --git a/packages/studiocms_core/src/sdk-utils/types.ts b/packages/studiocms_core/src/sdk-utils/types.ts index 7d077063..3b51301b 100644 --- a/packages/studiocms_core/src/sdk-utils/types.ts +++ b/packages/studiocms_core/src/sdk-utils/types.ts @@ -226,22 +226,6 @@ export type addDatabaseEntryInsertPage = { pageContent: PageContentReturnId[]; }; -/** - * Represents the possible database entries that can be added. - * - * @property {'pages'} pages - Represents the pages database entry. - * @property {'pageContent'} pageContent - Represents the page content database entry. - * @property {'tags'} tags - Represents the tags database entry. - * @property {'categories'} categories - Represents the categories database entry. - * @property {'permissions'} permissions - Represents the permissions database entry. - */ -export type addDatabaseEntryDatabase = - | 'pages' - | 'pageContent' - | 'tags' - | 'categories' - | 'permissions'; - /** * Interface for retrieving user data from the database. * Provides methods to fetch user data by different identifiers. diff --git a/packages/studiocms_core/src/sdk-utils/utils.ts b/packages/studiocms_core/src/sdk-utils/utils.ts index 99218379..4c735155 100644 --- a/packages/studiocms_core/src/sdk-utils/utils.ts +++ b/packages/studiocms_core/src/sdk-utils/utils.ts @@ -1,3 +1,4 @@ +/// import { db, eq } from 'astro:db'; import { tsOAuthAccounts,