UNPKG

studiocms

Version:

Astro Native CMS for AstroDB. Built from the ground up by the Astro community.

307 lines (306 loc) 12.1 kB
import { eq } from "astro:db"; import { Effect, genLogger } from "../../../effect.js"; import { FolderListMapID, FolderTreeMapID, SiteConfigMapID, StudioCMSPkgId, VersionMapID } from "../consts.js"; import { AstroDB, GetVersionFromNPM, SDKCore_FolderTree } from "../effect/index.js"; import { tsPageContent, tsPageData, tsPageDataCategories, tsPageDataTags, tsPageFolderStructure, tsPermissions } from "../tables.js"; import { _ClearUnknownError, _clearLibSQLError, CacheContext, folderListReturn, folderTreeReturn, isCacheEnabled, pageDataReturn, siteConfigReturn, versionReturn } from "../utils.js"; import { SDKCore_CLEAR } from "./clear.js"; import { SDKCore_CONFIG } from "./config.js"; import { SDKCore_GET } from "./get.js"; class SDKCore_UPDATE extends Effect.Service()( "studiocms/sdk/SDKCore/modules/update", { dependencies: [ AstroDB.Default, SDKCore_CLEAR.Default, SDKCore_FolderTree.Default, SDKCore_GET.Default, GetVersionFromNPM.Default, SDKCore_CONFIG.Default ], effect: genLogger("studiocms/sdk/SDKCore/modules/update/effect")(function* () { const [ dbService, CLEAR, GET, { siteConfig: sdkSiteConfig }, { buildFolderTree, getAvailableFolders }, { pages, FolderList, folderTree, version, siteConfig }, getVersionFromNPM ] = yield* Effect.all([ AstroDB, SDKCore_CLEAR, SDKCore_GET, SDKCore_CONFIG, SDKCore_FolderTree, CacheContext, GetVersionFromNPM ]); const UPDATE = { /** * Updates a page content in the database. * * @param data - The data to update in the page content table. * @returns A promise that resolves to the updated page content. * @throws {StudioCMS_SDK_Error} If an error occurs while updating the page content. */ pageContent: dbService.makeQuery( (ex, data) => ex( (db) => db.update(tsPageContent).set(data).where(eq(tsPageContent.id, data.id)).returning().get() ).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("UPDATE.pageContent", cause) }) ) ), /** * Updates a tag in the database. * * @param data - The data to update in the page data tags table. * @returns A promise that resolves to the updated tag. * @throws {StudioCMS_SDK_Error} If an error occurs while updating the tag. */ tags: dbService.makeQuery( (ex, data) => ex( (db) => db.update(tsPageDataTags).set(data).where(eq(tsPageDataTags.id, data.id)).returning().get() ).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("UPDATE.tags", cause) }) ) ), /** * Updates a category in the database. * * @param data - The data to update in the page data categories table. * @returns A promise that resolves to the updated category. * @throws {StudioCMS_SDK_Error} If an error occurs while updating the category. */ categories: dbService.makeQuery( (ex, data) => ex( (db) => db.update(tsPageDataCategories).set(data).where(eq(tsPageDataCategories.id, data.id)).returning().get() ).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("UPDATE.categories", cause) }) ) ), /** * Updates a permission in the database. * * @param data - The data to update in the permissions table. * @returns A promise that resolves to the updated permission. * @throws {StudioCMS_SDK_Error} If an error occurs while updating the permission. */ permissions: dbService.makeQuery( (ex, data) => ex( (db) => db.update(tsPermissions).set(data).where(eq(tsPermissions.user, data.user)).returning().get() ).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("UPDATE.permissions", cause) }) ) ), /** * Updates the folder tree structure and cache. * @returns An Effect that resolves when the folder tree is updated. * @throws {LibSQLDatabaseError} If a database error occurs during the update. */ folderTree: Effect.gen(function* () { const status = yield* isCacheEnabled; yield* CLEAR.folderTree(); const newFolderTree = yield* buildFolderTree; if (status) { folderTree.set(FolderTreeMapID, folderTreeReturn(newFolderTree)); } }).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("UPDATE.folderTree", cause) }) ), /** * Updates the folder list and cache. * @returns An Effect that resolves when the folder list is updated. * @throws {LibSQLDatabaseError} If a database error occurs during the update. */ folderList: Effect.gen(function* () { const status = yield* isCacheEnabled; yield* CLEAR.folderList(); const folderList = yield* getAvailableFolders; if (status) { FolderList.set(FolderListMapID, folderListReturn(folderList)); } }).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("UPDATE.folderList", cause) }) ), /** * Updates a folder in the database and refreshes related caches. * @param data - The folder data to update. * @returns An Effect that resolves to the updated folder data. * @throws {LibSQLDatabaseError} If a database error occurs during the update. */ folder: (data) => Effect.gen(function* () { const updated = yield* dbService.execute( (db) => db.update(tsPageFolderStructure).set(data).where(eq(tsPageFolderStructure.id, data.id)).returning().get() ); yield* UPDATE.folderList; yield* UPDATE.folderTree; return updated; }).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("UPDATE.folder", cause) }) ), /** * Updates the latest StudioCMS version in the cache. * @returns An Effect that resolves to the latest version. * @throws {UnknownException} If an error occurs while fetching the latest version. */ latestVersion: () => Effect.gen(function* () { const status = yield* isCacheEnabled; const latestVersion = yield* getVersionFromNPM.get(StudioCMSPkgId); const newVersion = versionReturn(latestVersion); if (!status) return newVersion; version.set(VersionMapID, newVersion); return newVersion; }).pipe( Effect.catchTags({ UnknownException: (cause) => _ClearUnknownError("UPDATE.latestVersion", cause) }) ), /** * Updates the site configuration in the database and cache. * @param data - The new site configuration data. * @returns An Effect that resolves to the updated site configuration. * @throws {LibSQLDatabaseError} If a database error occurs during the update. * @throws {UnknownException} If an unknown error occurs during the update. */ siteConfig: (data) => Effect.gen(function* () { const status = yield* isCacheEnabled; const newSiteConfig = yield* sdkSiteConfig.update(data); if (!newSiteConfig) return; const returnConfig = siteConfigReturn(newSiteConfig.data); if (!status) return returnConfig; siteConfig.set(SiteConfigMapID, returnConfig); return returnConfig; }), page: { /** * Updates a page by its ID, including content and data, and refreshes caches. * @param id - The ID of the page to update. * @param data - The new page data and content to update. * @returns An Effect that resolves to the updated page data, or undefined if not found. * @throws {LibSQLDatabaseError} If a database error occurs during the update. * @throws {UnknownException} If an unknown error occurs during the update. */ byId: (id, data) => { const updatePage = dbService.makeQuery( (ex, data2) => ex( (db) => db.update(tsPageData).set(data2).where(eq(tsPageData.id, data2.id)).returning().get() ) ); return Effect.gen(function* () { const status = yield* isCacheEnabled; yield* updatePage(data.pageData); yield* UPDATE.pageContent(data.pageContent); const rawUpdated = yield* GET.page.byId(id); if (!rawUpdated) { return void 0; } const returnData = pageDataReturn(rawUpdated.data); if (!status) { return returnData; } yield* CLEAR.folderList(); yield* CLEAR.folderTree(); yield* CLEAR.pages(); return returnData; }).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("UPDATE.page.byId", cause), UnknownException: (cause) => _ClearUnknownError("UPDATE.page.byId", cause) }) ); }, /** * Updates a page by its slug, including content and data, and refreshes caches. * @param slug - The slug of the page to update. * @param data - The new page data and content to update. * @returns An Effect that resolves to the updated page data, or undefined if not found. * @throws {LibSQLDatabaseError} If a database error occurs during the update. * @throws {UnknownException} If an unknown error occurs during the update. */ bySlug: (slug, data) => { const updatePage = dbService.makeQuery( (ex, data2) => ex( (db) => db.update(tsPageData).set(data2).where(eq(tsPageData.id, data2.id)).returning().get() ) ); return Effect.gen(function* () { const status = yield* isCacheEnabled; if (!status) { yield* updatePage(data.pageData); yield* UPDATE.pageContent(data.pageContent); const rawUpdated2 = yield* GET.page.byId(data.pageData.id); if (!rawUpdated2) { return void 0; } return pageDataReturn(rawUpdated2.data); } const cachedPage = Array.from(pages.values()).find((page) => page.data.slug === slug); if (!cachedPage) { return void 0; } yield* updatePage(data.pageData); yield* UPDATE.pageContent(data.pageContent); const rawUpdated = yield* GET.page.byId(data.pageData.id); if (!rawUpdated) { return void 0; } const returnData = pageDataReturn(rawUpdated.data); yield* CLEAR.folderList(); yield* CLEAR.folderTree(); yield* CLEAR.pages(); return returnData; }).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("UPDATE.page.byId", cause), UnknownException: (cause) => _ClearUnknownError("UPDATE.page.byId", cause) }) ); } } }; return UPDATE; }) } ) { } export { SDKCore_UPDATE };