diff --git a/packages/studiocms_core/src/db/tables.ts b/packages/studiocms_core/src/db/tables.ts index 9367c09e0..02ab7fd6d 100644 --- a/packages/studiocms_core/src/db/tables.ts +++ b/packages/studiocms_core/src/db/tables.ts @@ -57,8 +57,6 @@ export const StudioCMSPageData = defineTable({ default: 'https://images.unsplash.com/photo-1707343843982-f8275f3994c5?q=80&w=1032&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', }), - // TODO: Remove typo column name once new onboarding page is ready to use - catagories: column.json({ default: [], optional: true }), categories: column.json({ default: [], optional: true }), tags: column.json({ default: [], optional: true }), authorId: column.text({ optional: true }), diff --git a/packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts b/packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts index 6653674b1..5aae36ca5 100644 --- a/packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts +++ b/packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts @@ -30,8 +30,10 @@ import { combineRanks, verifyRank } from '../utils'; export async function getPermissionsLists(list: AvailableLists): Promise { switch (list) { case 'all': { - const currentPermittedUsers = await db.select().from(tsPermissions); - const existingUsers = await db.select().from(tsUsers); + const [currentPermittedUsers, existingUsers] = await db.batch([ + db.select().from(tsPermissions), + db.select().from(tsUsers), + ]); const owners = verifyRank(existingUsers, currentPermittedUsers, 'owner'); @@ -49,27 +51,31 @@ export async function getPermissionsLists(list: AvailableLists): Promise + any, + { + id: { + type: 'text' | 'number'; + schema: { + unique: false; + // biome-ignore lint/suspicious/noExplicitAny: + deprecated: any; + // biome-ignore lint/suspicious/noExplicitAny: + name: any; + // biome-ignore lint/suspicious/noExplicitAny: + collection: any; + primaryKey: true; + }; + }; + } +>; + /** * Represents the structure for adding a database entry for a page. * diff --git a/packages/studiocms_core/src/sdk-utils/types/tables.types.ts b/packages/studiocms_core/src/sdk-utils/types/tables.types.ts index d0b75c092..f4bbc8a8f 100644 --- a/packages/studiocms_core/src/sdk-utils/types/tables.types.ts +++ b/packages/studiocms_core/src/sdk-utils/types/tables.types.ts @@ -70,10 +70,7 @@ export type SiteConfig = Omit; * Represents a stripped-down version of the `tsPageDataSelect` type, * excluding the properties 'catagories', 'categories', 'tags', and 'contributorIds'. */ -export type PageDataStripped = Omit< - tsPageDataSelect, - 'catagories' | 'categories' | 'tags' | 'contributorIds' ->; +export type PageDataStripped = Omit; /** * Represents a type that picks the 'id' property from the tsPageContentSelect type. diff --git a/packages/studiocms_core/src/sdk-utils/utils.ts b/packages/studiocms_core/src/sdk-utils/utils.ts index 4c735155c..e94121a3c 100644 --- a/packages/studiocms_core/src/sdk-utils/utils.ts +++ b/packages/studiocms_core/src/sdk-utils/utils.ts @@ -12,6 +12,7 @@ import type { CombinedPageData, CombinedRank, CombinedUserData, + GenericTable, SingleRank, tsPageDataCategoriesSelect, tsPageDataSelect, @@ -39,6 +40,35 @@ export async function collectUserData(user: tsUsersSelect): Promise} ids - An array of IDs to query. + * @param {GenericTable} table - The table to query against. + * @returns {Promise} A promise that resolves to an array of results of type `returnType`. + */ +export async function runDbBatchQuery( + ids: Array, + table: GenericTable +): Promise { + const batchQueries = []; + + for (const id of ids) { + batchQueries.push(db.select().from(table).where(eq(table.id, id))); + } + + const [head, ...tail] = batchQueries; + + if (head) { + const queryResults = await db.batch([head, ...tail]); + // Flatten and filter the results + return queryResults.flat().filter(Boolean) as returnType[]; + } + + return [] as returnType[]; +} + /** * Collects and combines page data including categories, tags, contributors, and multilingual content. * @@ -54,36 +84,14 @@ export async function collectUserData(user: tsUsersSelect): Promise { - const categories: tsPageDataCategoriesSelect[] = []; - const tags: tsPageDataTagsSelect[] = []; - const contributors: string[] = []; - - const [allCategories, allTags, allUsers] = await db.batch([ - db.select().from(tsPageDataCategories), - db.select().from(tsPageDataTags), - db.select().from(tsUsers), - ]); + const categories = await runDbBatchQuery( + page.categories as number[], + tsPageDataCategories + ); - for (const category of page.categories as number[]) { - const categoryData = allCategories.find((cat) => cat.id === category); - if (categoryData) { - categories.push(categoryData); - } - } + const tags = await runDbBatchQuery(page.tags as number[], tsPageDataTags); - for (const tag of page.tags as number[]) { - const tagData = allTags.find((t) => t.id === tag); - if (tagData) { - tags.push(tagData); - } - } - - for (const contributor of page.contributorIds as string[]) { - const contributorData = allUsers.find((user) => user.id === contributor); - if (contributorData) { - contributors.push(contributorData.id); - } - } + const contributors = await runDbBatchQuery(page.contributorIds as string[], tsUsers); const multiLangContentData = await db .select()