studiocms
Version:
Astro Native CMS for AstroDB. Built from the ground up by the Astro community.
148 lines (147 loc) • 5.35 kB
JavaScript
import { eq, inArray } from "astro:db";
import { Effect } from "../../../effect.js";
import { SDKCoreError, StudioCMS_SDK_Error } from "../errors.js";
import {
tsOAuthAccounts,
tsPageContent,
tsPageDataCategories,
tsPageDataTags,
tsPermissions,
tsUsers
} from "../tables.js";
import { AstroDB } from "./db.js";
import { SDKCore_FolderTree } from "./foldertree.js";
import { SDKCore_Parsers } from "./parsers.js";
class SDKCore_Collectors extends Effect.Service()("SDKCore_Collectors", {
effect: Effect.gen(function* () {
const dbService = yield* AstroDB;
const folderTreeService = yield* SDKCore_FolderTree;
const parseService = yield* SDKCore_Parsers;
const collectCategories = (categoryIds) => Effect.gen(function* () {
const categories = [];
if (categoryIds.length > 0) {
const resItem = yield* dbService.execute(
(db) => db.select().from(tsPageDataCategories).where(inArray(tsPageDataCategories.id, categoryIds))
);
if (resItem) categories.push(...resItem);
}
return categories;
});
const collectTags = (tagIds) => Effect.gen(function* () {
const tags = [];
if (tagIds.length > 0) {
const resItem = yield* dbService.execute(
(db) => db.select().from(tsPageDataTags).where(inArray(tsPageDataTags.id, tagIds))
);
if (resItem) tags.push(...resItem);
}
return tags;
});
const transformPageDataToMetaOnly = (data) => Effect.try(() => {
if (Array.isArray(data)) {
return data.map(
({ defaultContent, multiLangContent, ...rest2 }) => rest2
);
}
const {
defaultContent: _dump1,
multiLangContent: _dump2,
...rest
} = data;
return rest;
});
function collectPageData(page, tree, metaOnly = false) {
return Effect.gen(function* () {
const categoryIds = yield* parseService.parseIdNumberArray(page.categories || []);
const tagIds = yield* parseService.parseIdNumberArray(page.tags || []);
const contributorIds = yield* parseService.parseIdStringArray(page.contributorIds || []);
const categories = yield* collectCategories(categoryIds);
const tags = yield* collectTags(tagIds);
const authorData = yield* dbService.execute(
(db) => db.select().from(tsUsers).where(eq(tsUsers.id, page.authorId || "")).get()
);
let contributorsData;
if (contributorIds.length) {
contributorsData = yield* dbService.execute(
(db) => db.select().from(tsUsers).where(inArray(tsUsers.id, contributorIds))
);
} else {
contributorsData = [];
}
let multiLanguageContentData;
if (!metaOnly) {
multiLanguageContentData = yield* dbService.execute(
(db) => db.select().from(tsPageContent).where(eq(tsPageContent.contentId, page.id))
);
}
const defaultLanguageContentData = multiLanguageContentData?.find(
(content) => content.contentLang === page.contentLang
);
const safeSlug = page.slug === "index" ? "/" : `/${page.slug}`;
let urlRoute = safeSlug;
if (page.parentFolder) {
const urlParts = yield* folderTreeService.findNodesAlongPathToId(tree, page.parentFolder);
urlRoute = `/${urlParts.map((part) => part.name).join("/")}${safeSlug}`;
}
const returnData = {
...page,
urlRoute,
categories,
tags,
contributorIds,
authorData,
contributorsData,
multiLangContent: multiLanguageContentData,
defaultContent: defaultLanguageContentData
};
if (metaOnly) {
return yield* transformPageDataToMetaOnly(returnData);
}
return returnData;
}).pipe(
Effect.catchTags({
LibSQLClientError: (cause) => Effect.fail(
new SDKCoreError({
type: "LibSQLDatabaseError",
cause: new StudioCMS_SDK_Error(`Error collecting page data: ${cause}`)
})
),
UnknownException: (cause) => Effect.fail(
new SDKCoreError({
type: "UNKNOWN",
cause: new StudioCMS_SDK_Error(`Error collecting page data: ${cause}`)
})
)
})
);
}
const collectUserData = (user) => Effect.gen(function* () {
const oAuthData = yield* dbService.execute(
(db) => db.select().from(tsOAuthAccounts).where(eq(tsOAuthAccounts.userId, user.id))
);
const permissionsData = yield* dbService.execute(
(db) => db.select().from(tsPermissions).where(eq(tsPermissions.user, user.id)).get()
);
return {
...user,
oAuthData,
permissionsData
};
}).pipe(
Effect.catchTags({
LibSQLClientError: (cause) => Effect.fail(
new SDKCoreError({
type: "LibSQLDatabaseError",
cause: new StudioCMS_SDK_Error(`Error collecting page data: ${cause}`)
})
)
})
);
return { collectCategories, collectTags, collectPageData, collectUserData };
}),
dependencies: [AstroDB.Default, SDKCore_FolderTree.Default, SDKCore_Parsers.Default]
}) {
}
export {
SDKCore_Collectors
};