diff --git a/packages/studiocms_core/src/sdk-utils/get/getDatabase.ts b/packages/studiocms_core/src/sdk-utils/get/getDatabase.ts index a4eb5991..98b700a6 100644 --- a/packages/studiocms_core/src/sdk-utils/get/getDatabase.ts +++ b/packages/studiocms_core/src/sdk-utils/get/getDatabase.ts @@ -6,61 +6,52 @@ import type { CombinedPageData, CombinedUserData, STUDIOCMS_SDK, SiteConfig } fr import { collectPageData, collectUserData } from '../utils'; /** - * Retrieves data from the database based on the specified table. + * Provides methods to retrieve various types of data from the database. * - * @param database - The name of the database table to retrieve data from. - * It can be one of the following values: 'users', 'pages', or 'config'. + * @type {STUDIOCMS_SDK['GET']['database']} * - * @returns A promise that resolves to the data retrieved from the specified table. - * - * - If `database` is 'users', it returns an array of `CombinedUserData` objects. - * - If `database` is 'pages', it returns an array of `CombinedPageData` objects. - * - If `database` is 'config', it returns the site configuration object. - * - * @throws Will throw an error if the specified database table is not recognized. + * @property {Function} users - Asynchronously retrieves and combines user data from the database. + * @property {Function} pages - Asynchronously retrieves and combines page data from the database. + * @property {Function} config - Asynchronously retrieves the site configuration from the database. */ -export const getDatabase: STUDIOCMS_SDK['GET']['database'] = async (database) => { - switch (database) { - case 'users': { - const combinedUserData: CombinedUserData[] = []; - - const users = await db.select().from(tsUsers); +export const getDatabase: STUDIOCMS_SDK['GET']['database'] = { + users: async () => { + const combinedUserData: CombinedUserData[] = []; - for (const user of users) { - const UserData = await collectUserData(user); + const users = await db.select().from(tsUsers); - combinedUserData.push(UserData); - } + for (const user of users) { + const UserData = await collectUserData(user); - return combinedUserData; + combinedUserData.push(UserData); } - case 'pages': { - const pages: CombinedPageData[] = []; - const pagesRaw = await db.select().from(tsPageData); + return combinedUserData; + }, + pages: async () => { + const pages: CombinedPageData[] = []; - for (const page of pagesRaw) { - const PageData = await collectPageData(page); + const pagesRaw = await db.select().from(tsPageData); - pages.push(PageData); - } + for (const page of pagesRaw) { + const PageData = await collectPageData(page); - return pages; + pages.push(PageData); } - case 'config': { - const siteConfig = await db - .select() - .from(tsSiteConfig) - .where(eq(tsSiteConfig.id, CMSSiteConfigId)) - .get(); - if (!siteConfig) return undefined; + return pages; + }, + config: async () => { + const siteConfig = await db + .select() + .from(tsSiteConfig) + .where(eq(tsSiteConfig.id, CMSSiteConfigId)) + .get(); - return siteConfig as SiteConfig; - } - default: - throw new Error(`Database table '${database}' not recognized.`); - } + if (!siteConfig) return undefined; + + return siteConfig as SiteConfig; + }, }; export default getDatabase; diff --git a/packages/studiocms_core/src/sdk-utils/get/getDatabaseEntry.ts b/packages/studiocms_core/src/sdk-utils/get/getDatabaseEntry.ts index 93446dc4..63d4a1c6 100644 --- a/packages/studiocms_core/src/sdk-utils/get/getDatabaseEntry.ts +++ b/packages/studiocms_core/src/sdk-utils/get/getDatabaseEntry.ts @@ -33,179 +33,170 @@ import { collectPageData, collectUserData } from '../utils'; * } * ``` */ -export const getDatabaseEntry: STUDIOCMS_SDK['GET']['databaseEntry'] = (database) => { - switch (database) { - case 'users': { - return { - /** - * Retrieves a database entry by its ID. - * - * @param id - The ID of the user to retrieve. - * @returns A promise that resolves to the combined user data or undefined if no user is found. - * - * The function performs the following steps: - * 1. Retrieves the user data from the database using the provided ID. - * 2. If no user is found, returns undefined. - * 3. Retrieves OAuth account data and permissions data for the user. - * 4. Returns the combined user data including OAuth data and permissions data. - * - * @example - * ```typescript - * const userData = await byId('example-id'); - * if (userData) { - * console.log(userData); - * } else { - * console.log('User not found'); - * } - * ``` - */ - async byId(id) { - const user = await db.select().from(tsUsers).where(eq(tsUsers.id, id)).get(); +export const getDatabaseEntry: STUDIOCMS_SDK['GET']['databaseEntry'] = { + users: { + /** + * Retrieves a database entry by its ID. + * + * @param id - The ID of the user to retrieve. + * @returns A promise that resolves to the combined user data or undefined if no user is found. + * + * The function performs the following steps: + * 1. Retrieves the user data from the database using the provided ID. + * 2. If no user is found, returns undefined. + * 3. Retrieves OAuth account data and permissions data for the user. + * 4. Returns the combined user data including OAuth data and permissions data. + * + * @example + * ```typescript + * const userData = await byId('example-id'); + * if (userData) { + * console.log(userData); + * } else { + * console.log('User not found'); + * } + * ``` + */ + async byId(id) { + const user = await db.select().from(tsUsers).where(eq(tsUsers.id, id)).get(); - if (!user) return undefined; + if (!user) return undefined; - return await collectUserData(user); - }, - /** - * Retrieves a database entry by its username. - * - * @param username - The username of the user to retrieve. - * @returns A promise that resolves to the combined user data or undefined if no user is found. - * - * The function performs the following steps: - * 1. Retrieves the user data from the database using the provided username. - * 2. If no user is found, returns undefined. - * 3. Retrieves OAuth account data and permissions data for the user. - * 4. Returns the combined user data including OAuth data and permissions data. - * - * @example - * ```typescript - * const userData = await byUsername('john_doe'); - * if (userData) { - * console.log(userData); - * } else { - * console.log('User not found'); - * } - * ``` - */ - async byUsername(username) { - const user = await db.select().from(tsUsers).where(eq(tsUsers.username, username)).get(); + return await collectUserData(user); + }, + /** + * Retrieves a database entry by its username. + * + * @param username - The username of the user to retrieve. + * @returns A promise that resolves to the combined user data or undefined if no user is found. + * + * The function performs the following steps: + * 1. Retrieves the user data from the database using the provided username. + * 2. If no user is found, returns undefined. + * 3. Retrieves OAuth account data and permissions data for the user. + * 4. Returns the combined user data including OAuth data and permissions data. + * + * @example + * ```typescript + * const userData = await byUsername('john_doe'); + * if (userData) { + * console.log(userData); + * } else { + * console.log('User not found'); + * } + * ``` + */ + async byUsername(username) { + const user = await db.select().from(tsUsers).where(eq(tsUsers.username, username)).get(); - if (!user) return undefined; + if (!user) return undefined; - return await collectUserData(user); - }, - /** - * Retrieves a database entry by its email. - * - * @param email - The email of the user to retrieve. - * @returns A promise that resolves to the combined user data or undefined if no user is found. - * - * The function performs the following steps: - * 1. Retrieves the user data from the database using the provided email. - * 2. If no user is found, returns undefined. - * 3. Retrieves OAuth account data and permissions data for the user. - * 4. Returns the combined user data including OAuth data and permissions data. - * - * @example - * ```typescript - * const userData = await byEmail('john@doe.xyz'); - * if (userData) { - * console.log(userData); - * } else { - * console.log('User not found'); - * } - */ - async byEmail(email) { - const user = await db.select().from(tsUsers).where(eq(tsUsers.email, email)).get(); + return await collectUserData(user); + }, + /** + * Retrieves a database entry by its email. + * + * @param email - The email of the user to retrieve. + * @returns A promise that resolves to the combined user data or undefined if no user is found. + * + * The function performs the following steps: + * 1. Retrieves the user data from the database using the provided email. + * 2. If no user is found, returns undefined. + * 3. Retrieves OAuth account data and permissions data for the user. + * 4. Returns the combined user data including OAuth data and permissions data. + * + * @example + * ```typescript + * const userData = await byEmail('john@doe.xyz'); + * if (userData) { + * console.log(userData); + * } else { + * console.log('User not found'); + * } + */ + async byEmail(email) { + const user = await db.select().from(tsUsers).where(eq(tsUsers.email, email)).get(); - if (!user) return undefined; + if (!user) return undefined; - return await collectUserData(user); - }, - }; - } - case 'pages': { - return { - /** - * Retrieves a database entry by its ID. - * - * @param id - The ID of the page to retrieve. - * @returns A promise that resolves to the combined page data or undefined if no page is found. - * - * The function performs the following steps: - * 1. Retrieves the page data from the database using the provided ID. - * 2. If no page is found, returns undefined. - * 3. Initializes arrays for categories, tags, and contributors. - * 4. Retrieves all categories, tags, and users from the database. - * 5. Maps the category IDs, tag IDs, and contributor IDs from the page data to their respective data objects. - * 6. Retrieves multi-language content data for the page. - * 7. Finds the default content data from the multi-language content data. - * 8. Returns the combined page data including categories, tags, contributors, multi-language content, and default content. - * - * @example - * ```typescript - * const pageData = await byId('example-id'); - * if (pageData) { - * console.log(pageData); - * } else { - * console.log('Page not found'); - * } - * ``` - */ - async byId(id) { - const page = await db.select().from(tsPageData).where(eq(tsPageData.id, id)).get(); + return await collectUserData(user); + }, + }, + pages: { + /** + * Retrieves a database entry by its ID. + * + * @param id - The ID of the page to retrieve. + * @returns A promise that resolves to the combined page data or undefined if no page is found. + * + * The function performs the following steps: + * 1. Retrieves the page data from the database using the provided ID. + * 2. If no page is found, returns undefined. + * 3. Initializes arrays for categories, tags, and contributors. + * 4. Retrieves all categories, tags, and users from the database. + * 5. Maps the category IDs, tag IDs, and contributor IDs from the page data to their respective data objects. + * 6. Retrieves multi-language content data for the page. + * 7. Finds the default content data from the multi-language content data. + * 8. Returns the combined page data including categories, tags, contributors, multi-language content, and default content. + * + * @example + * ```typescript + * const pageData = await byId('example-id'); + * if (pageData) { + * console.log(pageData); + * } else { + * console.log('Page not found'); + * } + * ``` + */ + async byId(id) { + const page = await db.select().from(tsPageData).where(eq(tsPageData.id, id)).get(); - if (!page) return undefined; + if (!page) return undefined; - return await collectPageData(page); - }, - /** - * Retrieves a database entry by its slug and optional package name. - * - * @param slug - The slug of the page to retrieve. - * @param pkg - Optional package name to filter the page by. Defaults to 'studiocms'. - * @returns A promise that resolves to the combined page data or undefined if no page is found. - * - * The function performs the following steps: - * 1. Retrieves the page data from the database using the provided slug and package name. - * 2. If no page is found, returns undefined. - * 3. Initializes arrays for categories, tags, and contributors. - * 4. Retrieves all categories, tags, and users from the database. - * 5. Maps the category IDs, tag IDs, and contributor IDs from the page data to their respective data objects. - * 6. Retrieves multi-language content data for the page. - * 7. Finds the default content data from the multi-language content data. - * 8. Returns the combined page data including categories, tags, contributors, multi-language content, and default content. - * - * @example - * ```typescript - * const pageData = await bySlug('example-slug'); - * if (pageData) { - * console.log(pageData); - * } else { - * console.log('Page not found'); - * } - * ``` - */ - async bySlug(slug, pkg?) { - const pkgToGet = pkg || 'studiocms'; + return await collectPageData(page); + }, + /** + * Retrieves a database entry by its slug and optional package name. + * + * @param slug - The slug of the page to retrieve. + * @param pkg - Optional package name to filter the page by. Defaults to 'studiocms'. + * @returns A promise that resolves to the combined page data or undefined if no page is found. + * + * The function performs the following steps: + * 1. Retrieves the page data from the database using the provided slug and package name. + * 2. If no page is found, returns undefined. + * 3. Initializes arrays for categories, tags, and contributors. + * 4. Retrieves all categories, tags, and users from the database. + * 5. Maps the category IDs, tag IDs, and contributor IDs from the page data to their respective data objects. + * 6. Retrieves multi-language content data for the page. + * 7. Finds the default content data from the multi-language content data. + * 8. Returns the combined page data including categories, tags, contributors, multi-language content, and default content. + * + * @example + * ```typescript + * const pageData = await bySlug('example-slug'); + * if (pageData) { + * console.log(pageData); + * } else { + * console.log('Page not found'); + * } + * ``` + */ + async bySlug(slug, pkg?) { + const pkgToGet = pkg || 'studiocms'; - const page = await db - .select() - .from(tsPageData) - .where(and(eq(tsPageData.slug, slug), eq(tsPageData.package, pkgToGet))) - .get(); + const page = await db + .select() + .from(tsPageData) + .where(and(eq(tsPageData.slug, slug), eq(tsPageData.package, pkgToGet))) + .get(); - if (!page) return undefined; + if (!page) return undefined; - return await collectPageData(page); - }, - }; - } - default: { - throw new Error(`Unknown database table: ${database}`); - } - } + return await collectPageData(page); + }, + }, }; export default getDatabaseEntry; diff --git a/packages/studiocms_core/src/sdk-utils/get/getDatabaseTable.ts b/packages/studiocms_core/src/sdk-utils/get/getDatabaseTable.ts index 59c7c346..155e713a 100644 --- a/packages/studiocms_core/src/sdk-utils/get/getDatabaseTable.ts +++ b/packages/studiocms_core/src/sdk-utils/get/getDatabaseTable.ts @@ -16,43 +16,31 @@ import { import type { STUDIOCMS_SDK } from '../types'; /** - * Retrieves raw data from the specified database table. + * Retrieves various database tables * - * @param database - The name of the database table to retrieve data from. - * @returns A promise that resolves to the data from the specified database table. - * @throws An error if the specified database table is unknown. - * - * @example - * ```typescript - * const users = await getDatabaseRaw('users'); - * console.log(users); - * ``` + * @property {Function} users - Fetches the users table. + * @property {Function} oAuthAccounts - Fetches the OAuth accounts table. + * @property {Function} sessionTable - Fetches the session table. + * @property {Function} permissions - Fetches the permissions table. + * @property {Function} pageData - Fetches the page data table. + * @property {Function} pageDataTags - Fetches the page data tags table. + * @property {Function} pageDataCategories - Fetches the page data categories table. + * @property {Function} pageContent - Fetches the page content table. + * @property {Function} siteConfig - Fetches the site configuration table with a specific site config ID. + * @property {Function} diffTracking - Fetches the diff tracking table. */ -export const getDatabaseTable: STUDIOCMS_SDK['GET']['databaseTable'] = async (database) => { - switch (database) { - case 'users': - return await db.select().from(tsUsers); - case 'oAuthAccounts': - return await db.select().from(tsOAuthAccounts); - case 'sessionTable': - return await db.select().from(tsSessionTable); - case 'permissions': - return await db.select().from(tsPermissions); - case 'pageData': - return await db.select().from(tsPageData); - case 'pageDataTags': - return await db.select().from(tsPageDataTags); - case 'pageDataCategories': - return await db.select().from(tsPageDataCategories); - case 'pageContent': - return await db.select().from(tsPageContent); - case 'siteConfig': - return await db.select().from(tsSiteConfig).where(eq(tsSiteConfig.id, CMSSiteConfigId)).get(); - case 'diffTracking': - return await db.select().from(tsDiffTracking); - default: - throw new Error(`Unknown database table: ${database}`); - } +export const getDatabaseTable: STUDIOCMS_SDK['GET']['databaseTable'] = { + users: async () => await db.select().from(tsUsers), + oAuthAccounts: async () => await db.select().from(tsOAuthAccounts), + sessionTable: async () => await db.select().from(tsSessionTable), + permissions: async () => await db.select().from(tsPermissions), + pageData: async () => await db.select().from(tsPageData), + pageDataTags: async () => await db.select().from(tsPageDataTags), + pageDataCategories: async () => await db.select().from(tsPageDataCategories), + pageContent: async () => await db.select().from(tsPageContent), + siteConfig: async () => + await db.select().from(tsSiteConfig).where(eq(tsSiteConfig.id, CMSSiteConfigId)).get(), + diffTracking: async () => await db.select().from(tsDiffTracking), }; export default getDatabaseTable; diff --git a/packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts b/packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts index eb4dad0d..43f0f7e6 100644 --- a/packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts +++ b/packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts @@ -5,82 +5,66 @@ import type { STUDIOCMS_SDK } from '../types'; import { combineRanks, verifyRank } from '../utils'; /** - * Retrieves a list of permissions based on the specified list type. + * Provides methods to retrieve lists of users with different permission levels. * - * @param list - The type of list to retrieve. Can be one of 'all', 'owners', 'admins', 'editors', or 'visitors'. - * @returns A promise that resolves to an array of permissions lists. + * @property {Function} all - Retrieves all users categorized by their permission levels. + * @property {Function} owners - Retrieves users with 'owner' permission level. + * @property {Function} admins - Retrieves users with 'admin' permission level. + * @property {Function} editors - Retrieves users with 'editor' permission level. + * @property {Function} visitors - Retrieves users with 'visitor' permission level. * - * The function performs the following actions based on the list type: - * - 'all': Retrieves all users and their permissions, then categorizes them into owners, admins, editors, and visitors. - * - 'owners': Retrieves users with 'owner' permissions. - * - 'admins': Retrieves users with 'admin' permissions. - * - 'editors': Retrieves users with 'editor' permissions. - * - 'visitors': Retrieves users with 'visitor' permissions. - * - * The function uses the following helper functions: - * - `verifyRank`: Verifies the rank of users based on the existing users and current permitted users. - * - `combineRanks`: Combines users of a specific rank into a single list. - * - * @example - * ```typescript - * const owners = await getPermissionsLists('owners'); - * console.log(owners); - * ``` + * @returns {Promise} - A promise that resolves to an array of users with the specified permission level. */ -export const getPermissionsLists: STUDIOCMS_SDK['GET']['permissionsLists'] = async (list) => { - switch (list) { - case 'all': { - const [currentPermittedUsers, existingUsers] = await db.batch([ - db.select().from(tsPermissions), - db.select().from(tsUsers), - ]); +export const getPermissionsLists: STUDIOCMS_SDK['GET']['permissionsLists'] = { + all: async () => { + const [currentPermittedUsers, existingUsers] = await db.batch([ + db.select().from(tsPermissions), + db.select().from(tsUsers), + ]); - const owners = verifyRank(existingUsers, currentPermittedUsers, 'owner'); + const owners = verifyRank(existingUsers, currentPermittedUsers, 'owner'); - const admins = verifyRank(existingUsers, currentPermittedUsers, 'admin'); + const admins = verifyRank(existingUsers, currentPermittedUsers, 'admin'); - const editors = verifyRank(existingUsers, currentPermittedUsers, 'editor'); + const editors = verifyRank(existingUsers, currentPermittedUsers, 'editor'); - const visitors = verifyRank(existingUsers, currentPermittedUsers, 'visitor'); + const visitors = verifyRank(existingUsers, currentPermittedUsers, 'visitor'); - return [ - ...combineRanks('owner', owners), - ...combineRanks('admin', admins), - ...combineRanks('editor', editors), - ...combineRanks('visitor', visitors), - ]; - } - case 'owners': { - const [currentPermittedUsers, existingUsers] = await db.batch([ - db.select().from(tsPermissions), - db.select().from(tsUsers), - ]); - return verifyRank(existingUsers, currentPermittedUsers, 'owner'); - } - case 'admins': { - const [currentPermittedUsers, existingUsers] = await db.batch([ - db.select().from(tsPermissions), - db.select().from(tsUsers), - ]); - return verifyRank(existingUsers, currentPermittedUsers, 'admin'); - } - case 'editors': { - const [currentPermittedUsers, existingUsers] = await db.batch([ - db.select().from(tsPermissions), - db.select().from(tsUsers), - ]); - return verifyRank(existingUsers, currentPermittedUsers, 'editor'); - } - case 'visitors': { - const [currentPermittedUsers, existingUsers] = await db.batch([ - db.select().from(tsPermissions), - db.select().from(tsUsers), - ]); - return verifyRank(existingUsers, currentPermittedUsers, 'visitor'); - } - default: - throw new Error(`Unknown list type: ${list}`); - } + return [ + ...combineRanks('owner', owners), + ...combineRanks('admin', admins), + ...combineRanks('editor', editors), + ...combineRanks('visitor', visitors), + ]; + }, + owners: async () => { + const [currentPermittedUsers, existingUsers] = await db.batch([ + db.select().from(tsPermissions), + db.select().from(tsUsers), + ]); + return verifyRank(existingUsers, currentPermittedUsers, 'owner'); + }, + admins: async () => { + const [currentPermittedUsers, existingUsers] = await db.batch([ + db.select().from(tsPermissions), + db.select().from(tsUsers), + ]); + return verifyRank(existingUsers, currentPermittedUsers, 'admin'); + }, + editors: async () => { + const [currentPermittedUsers, existingUsers] = await db.batch([ + db.select().from(tsPermissions), + db.select().from(tsUsers), + ]); + return verifyRank(existingUsers, currentPermittedUsers, 'editor'); + }, + visitors: async () => { + const [currentPermittedUsers, existingUsers] = await db.batch([ + db.select().from(tsPermissions), + db.select().from(tsUsers), + ]); + return verifyRank(existingUsers, currentPermittedUsers, 'visitor'); + }, }; export default getPermissionsLists; diff --git a/packages/studiocms_core/src/sdk-utils/index.ts b/packages/studiocms_core/src/sdk-utils/index.ts index c98b1cb3..90193adb 100644 --- a/packages/studiocms_core/src/sdk-utils/index.ts +++ b/packages/studiocms_core/src/sdk-utils/index.ts @@ -20,18 +20,18 @@ import type { STUDIOCMS_SDK } from './types'; * // or using the virtual module (Included by default in StudioCMS) * import StudioCMS_SDK from 'studiocms:sdk'; * - * const users = await StudioCMS_SDK.GET.database('users'); + * const users = await StudioCMS_SDK.GET.database.users(); * * console.log(users); * ``` */ export const StudioCMS_SDK: STUDIOCMS_SDK = { GET: { - database: async (database) => await getDatabase(database), - databaseEntry: (database) => getDatabaseEntry(database), - databaseTable: async (database) => await getDatabaseTable(database), + database: getDatabase, + databaseEntry: getDatabaseEntry, + databaseTable: getDatabaseTable, + permissionsLists: getPermissionsLists, packagePages: async (packageName) => await getPackagePages(packageName), - permissionsLists: async (list) => await getPermissionsLists(list), }, POST: { databaseEntry: addDatabaseEntry, diff --git a/packages/studiocms_core/src/sdk-utils/types/index.ts b/packages/studiocms_core/src/sdk-utils/types/index.ts index 5474a4d4..238c008d 100644 --- a/packages/studiocms_core/src/sdk-utils/types/index.ts +++ b/packages/studiocms_core/src/sdk-utils/types/index.ts @@ -2,8 +2,6 @@ import type { Table } from '@astrojs/db/runtime'; import type { AvailableLists, CombinedRank, - CurrentTables, - DatabaseEntryTables, DatabaseTables, PageContentReturnId, PageDataCategoriesInsertResponse, @@ -11,7 +9,6 @@ import type { PageDataStripped, PageDataTagsInsertResponse, PermissionsList, - SimplifiedTables, SingleRank, SiteConfig, } from './tableDefs'; @@ -38,8 +35,6 @@ import type { export type { AvailableLists, CombinedRank, - CurrentTables, - DatabaseEntryTables, DatabaseTables, PageContentReturnId, PageDataCategoriesInsertResponse, @@ -47,7 +42,6 @@ export type { PageDataStripped, PageDataTagsInsertResponse, PermissionsList, - SimplifiedTables, SingleRank, SiteConfig, }; @@ -188,23 +182,6 @@ interface GetDatabaseEntryPage { bySlug: (slug: string, pkg: string) => Promise; } -/** - * Represents a database entry which can be either a user or a page. - */ -export type GetDatabaseEntry = GetDatabaseEntryUser | GetDatabaseEntryPage; - -/** - * Represents the possible return types for a database query. - * - * @type {SiteConfig | CombinedUserData[] | CombinedPageData[] | undefined} - * - * @property {SiteConfig} SiteConfig - Configuration settings for the site. - * @property {CombinedUserData[]} CombinedUserData - Array of combined user data. - * @property {CombinedPageData[]} CombinedPageData - Array of combined page data. - * @property {undefined} undefined - Indicates that the database query returned no results. - */ -export type GetDatabase = SiteConfig | CombinedUserData[] | CombinedPageData[] | undefined; - /** * Interface representing the STUDIOCMS SDK. */ @@ -214,25 +191,39 @@ export interface STUDIOCMS_SDK { */ GET: { /** - * Retrieves data from the database based on the specified table. - * - * @param database - The name of the database table to retrieve data from. - * It can be one of the following values: 'users', 'pages', or 'config'. + * Provides methods to retrieve various types of data from the database. * - * @returns A promise that resolves to the data retrieved from the specified table. + * @type {STUDIOCMS_SDK['GET']['database']} * - * - If `database` is 'users', it returns an array of `CombinedUserData` objects. - * - If `database` is 'pages', it returns an array of `CombinedPageData` objects. - * - If `database` is 'config', it returns the site configuration object. - * - * @throws Will throw an error if the specified database table is not recognized. + * @property {Function} users - Asynchronously retrieves and combines user data from the database. + * @property {Function} pages - Asynchronously retrieves and combines page data from the database. + * @property {Function} config - Asynchronously retrieves the site configuration from the database. */ - database: (database: SimplifiedTables) => Promise; + database: { + /** + * Retrieves data from the Users table. + * + * @returns A promise that resolves to an array of CombinedUserData objects. + */ + users: () => Promise; + /** + * Retrieves data from the Page metadata and content tables. + * + * @returns A promise that resolves to an array of CombinedPageData objects. + */ + pages: () => Promise; + /** + * Retrieves the site configuration data. + * + * @returns A promise that resolves to the site configuration object. + */ + config: () => Promise; + }; /** * Retrieves a database entry based on the specified table. * - * @param database - The name of the database table to retrieve the entry from. + * @param table - The name of the database table to retrieve the entry from. * @returns An object containing methods to retrieve entries by different criteria. * * The function supports the following tables: @@ -258,22 +249,63 @@ export interface STUDIOCMS_SDK { * } * ``` */ - databaseEntry: (database: DatabaseEntryTables) => GetDatabaseEntry; + databaseEntry: { + /** + * Provides methods to retrieve user data by different identifiers. + */ + users: GetDatabaseEntryUser; + + /** + * Provides methods to retrieve page data by different identifiers. + */ + pages: GetDatabaseEntryPage; + }; /** - * Retrieves raw data from the specified database table. + * Retrieves various database tables * - * @param database - The name of the database table to retrieve data from. - * @returns A promise that resolves to the data from the specified database table. - * @throws An error if the specified database table is unknown. + * @property {Function} users - Fetches the users table. + * @property {Function} oAuthAccounts - Fetches the OAuth accounts table. + * @property {Function} sessionTable - Fetches the session table. + * @property {Function} permissions - Fetches the permissions table. + * @property {Function} pageData - Fetches the page data table. + * @property {Function} pageDataTags - Fetches the page data tags table. + * @property {Function} pageDataCategories - Fetches the page data categories table. + * @property {Function} pageContent - Fetches the page content table. + * @property {Function} siteConfig - Fetches the site configuration table with a specific site config ID. + * @property {Function} diffTracking - Fetches the diff tracking table. + */ + databaseTable: { + users: () => Promise; + oAuthAccounts: () => Promise; + sessionTable: () => Promise; + permissions: () => Promise; + pageData: () => Promise; + pageDataTags: () => Promise; + pageDataCategories: () => Promise; + pageContent: () => Promise; + siteConfig: () => Promise; + diffTracking: () => Promise; + }; + + /** + * Provides methods to retrieve lists of users with different permission levels. * - * @example - * ```typescript - * const users = await getDatabaseRaw('users'); - * console.log(users); - * ``` + * @property {Function} all - Retrieves all users categorized by their permission levels. + * @property {Function} owners - Retrieves users with 'owner' permission level. + * @property {Function} admins - Retrieves users with 'admin' permission level. + * @property {Function} editors - Retrieves users with 'editor' permission level. + * @property {Function} visitors - Retrieves users with 'visitor' permission level. + * + * @returns {Promise} - A promise that resolves to an array of users with the specified permission level. */ - databaseTable: (database: CurrentTables) => Promise; + permissionsLists: { + all: () => Promise; + owners: () => Promise; + admins: () => Promise; + editors: () => Promise; + visitors: () => Promise; + }; /** * Retrieves the pages associated with a given package name. @@ -282,31 +314,6 @@ export interface STUDIOCMS_SDK { * @returns A promise that resolves to an array of CombinedPageData objects. */ packagePages: (packageName: string) => Promise; - - /** - * Retrieves a list of permissions based on the specified list type. - * - * @param list - The type of list to retrieve. Can be one of 'all', 'owners', 'admins', 'editors', or 'visitors'. - * @returns A promise that resolves to an array of permissions lists. - * - * The function performs the following actions based on the list type: - * - 'all': Retrieves all users and their permissions, then categorizes them into owners, admins, editors, and visitors. - * - 'owners': Retrieves users with 'owner' permissions. - * - 'admins': Retrieves users with 'admin' permissions. - * - 'editors': Retrieves users with 'editor' permissions. - * - 'visitors': Retrieves users with 'visitor' permissions. - * - * The function uses the following helper functions: - * - `verifyRank`: Verifies the rank of users based on the existing users and current permitted users. - * - `combineRanks`: Combines users of a specific rank into a single list. - * - * @example - * ```typescript - * const owners = await getPermissionsLists('owners'); - * console.log(owners); - * ``` - */ - permissionsLists: (list: AvailableLists) => Promise; }; /** * Contains methods for adding data to the database. diff --git a/packages/studiocms_core/src/sdk-utils/types/tableDefs.ts b/packages/studiocms_core/src/sdk-utils/types/tableDefs.ts index b5d5bb1d..fc5c3a2d 100644 --- a/packages/studiocms_core/src/sdk-utils/types/tableDefs.ts +++ b/packages/studiocms_core/src/sdk-utils/types/tableDefs.ts @@ -11,55 +11,6 @@ import type { tsUsersSelect, } from './tsAlias'; -/** - * Represents the possible table names in the current database schema. - * - * @property {'users'} users - Table for user information. - * @property {'oAuthAccounts'} oAuthAccounts - Table for OAuth account details. - * @property {'sessionTable'} sessionTable - Table for session information. - * @property {'permissions'} permissions - Table for user permissions. - * @property {'pageData'} pageData - Table for page data. - * @property {'pageDataTags'} pageDataTags - Table for tags associated with page data. - * @property {'pageDataCategories'} pageDataCategories - Table for categories associated with page data. - * @property {'pageContent'} pageContent - Table for page content. - * @property {'siteConfig'} siteConfig - Table for site configuration. - * @property {'diffTracking'} diffTracking - Table for tracking differences. - */ -export type CurrentTables = - | 'users' - | 'oAuthAccounts' - | 'sessionTable' - | 'permissions' - | 'pageData' - | 'pageDataTags' - | 'pageDataCategories' - | 'pageContent' - | 'siteConfig' - | 'diffTracking'; - -/** - * Represents a simplified view of the tables in the system. - * - * The `SimplifiedTables` type is a union of string literals that correspond - * to the names of the tables in the system. This type is used to restrict - * the values to a predefined set of table names. - * - * Possible values: - * - `'users'`: Represents the users table. - * - `'pages'`: Represents the pages table. - * - `'config'`: Represents the config table. - */ -export type SimplifiedTables = 'users' | 'pages' | 'config'; - -/** - * Represents the types of tables available in the database. - * - * @example - * // Example usage: - * const table: DatabaseEntryTables = 'users'; - */ -export type DatabaseEntryTables = 'users' | 'pages'; - /** * Represents a stripped-down version of the `tsSiteConfigSelect` type, * excluding the property 'id'.