Skip to content

Commit

Permalink
Refactor tsAlias.types.ts and tables.types.ts to improve type definit…
Browse files Browse the repository at this point in the history
…ions and simplify table selection
  • Loading branch information
Adammatthiesen committed Dec 11, 2024
1 parent 3049365 commit df34c8d
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 250 deletions.
1 change: 1 addition & 0 deletions packages/studiocms_core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"./sdk-utils/post": "./src/sdk-utils/post/index.ts",
"./sdk-utils/get": "./src/sdk-utils/get/index.ts",
"./sdk-utils/types": "./src/sdk-utils/types/index.ts",
"./sdk-utils/tables": "./src/sdk-utils/tables.ts",
"./sdk-utils/utils": "./src/sdk-utils/utils.ts"
},
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/studiocms_core/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const PLUGIN_INTEGRATION_ICON: string =
export const CMSSiteConfigId: number = 1;

/**
* StudioCMS Site Config Table Entry ID
* StudioCMS Social Links Type
*/
export type StudioCMSSocials = {
github: string;
Expand Down
14 changes: 5 additions & 9 deletions packages/studiocms_core/src/sdk-utils/get/getDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
/// <reference types="@astrojs/db" />
import { db, eq } from 'astro:db';
import { CMSSiteConfigId } from '../../consts';
import { tsPageData, tsSiteConfig, tsUsers } from '../../db/tsTables';
import type {
CombinedPageData,
CombinedUserData,
GetDatabase,
SimplifiedTables,
SiteConfig,
} from '../types';
import { tsPageData, tsSiteConfig, tsUsers } from '../tables';
import type { CombinedPageData, CombinedUserData, STUDIOCMS_SDK, SiteConfig } from '../types';
import { collectPageData, collectUserData } from '../utils';

/**
Expand All @@ -25,7 +19,9 @@ import { collectPageData, collectUserData } from '../utils';
*
* @throws Will throw an error if the specified database table is not recognized.
*/
export async function getDatabase(database: SimplifiedTables): Promise<GetDatabase> {
export async function getDatabase(
database: Parameters<STUDIOCMS_SDK['GET']['database']>[0]
): ReturnType<STUDIOCMS_SDK['GET']['database']> {
switch (database) {
case 'users': {
const combinedUserData: CombinedUserData[] = [];
Expand Down
23 changes: 10 additions & 13 deletions packages/studiocms_core/src/sdk-utils/get/getDatabaseEntry.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/// <reference types="@astrojs/db" />
import { and, db, eq } from 'astro:db';
import { tsPageData, tsUsers } from '../../db/tsTables';
import type {
CombinedPageData,
CombinedUserData,
DatabaseEntryTables,
GetDatabaseEntry,
} from '../types';
import { tsPageData, tsUsers } from '../tables';
import type { STUDIOCMS_SDK } from '../types';
import { collectPageData, collectUserData } from '../utils';

/**
Expand Down Expand Up @@ -38,7 +33,9 @@ import { collectPageData, collectUserData } from '../utils';
* }
* ```
*/
export function getDatabaseEntry(database: DatabaseEntryTables): GetDatabaseEntry {
export function getDatabaseEntry(
database: Parameters<STUDIOCMS_SDK['GET']['databaseEntry']>[0]
): ReturnType<STUDIOCMS_SDK['GET']['databaseEntry']> {
switch (database) {
case 'users': {
return {
Expand All @@ -64,7 +61,7 @@ export function getDatabaseEntry(database: DatabaseEntryTables): GetDatabaseEntr
* }
* ```
*/
async byId(id: string): Promise<CombinedUserData | undefined> {
async byId(id) {
const user = await db.select().from(tsUsers).where(eq(tsUsers.id, id)).get();

if (!user) return undefined;
Expand Down Expand Up @@ -93,7 +90,7 @@ export function getDatabaseEntry(database: DatabaseEntryTables): GetDatabaseEntr
* }
* ```
*/
async byUsername(username: string): Promise<CombinedUserData | undefined> {
async byUsername(username) {
const user = await db.select().from(tsUsers).where(eq(tsUsers.username, username)).get();

if (!user) return undefined;
Expand Down Expand Up @@ -121,7 +118,7 @@ export function getDatabaseEntry(database: DatabaseEntryTables): GetDatabaseEntr
* console.log('User not found');
* }
*/
async byEmail(email: string): Promise<CombinedUserData | undefined> {
async byEmail(email) {
const user = await db.select().from(tsUsers).where(eq(tsUsers.email, email)).get();

if (!user) return undefined;
Expand Down Expand Up @@ -158,7 +155,7 @@ export function getDatabaseEntry(database: DatabaseEntryTables): GetDatabaseEntr
* }
* ```
*/
async byId(id: string): Promise<CombinedPageData | undefined> {
async byId(id) {
const page = await db.select().from(tsPageData).where(eq(tsPageData.id, id)).get();

if (!page) return undefined;
Expand Down Expand Up @@ -192,7 +189,7 @@ export function getDatabaseEntry(database: DatabaseEntryTables): GetDatabaseEntr
* }
* ```
*/
async bySlug(slug: string, pkg?: string): Promise<CombinedPageData | undefined> {
async bySlug(slug, pkg?) {
const pkgToGet = pkg || 'studiocms';

const page = await db
Expand Down
8 changes: 5 additions & 3 deletions packages/studiocms_core/src/sdk-utils/get/getDatabaseTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
tsSessionTable,
tsSiteConfig,
tsUsers,
} from '../../db/tsTables';
import type { CurrentTables, DatabaseTables } from '../types';
} from '../tables';
import type { STUDIOCMS_SDK } from '../types';

/**
* Retrieves raw data from the specified database table.
Expand All @@ -28,7 +28,9 @@ import type { CurrentTables, DatabaseTables } from '../types';
* console.log(users);
* ```
*/
export async function getDatabaseTable(database: CurrentTables): Promise<DatabaseTables> {
export async function getDatabaseTable(
database: Parameters<STUDIOCMS_SDK['GET']['databaseTable']>[0]
): ReturnType<STUDIOCMS_SDK['GET']['databaseTable']> {
switch (database) {
case 'users':
return await db.select().from(tsUsers);
Expand Down
8 changes: 5 additions & 3 deletions packages/studiocms_core/src/sdk-utils/get/getPackagePages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="@astrojs/db" />
import { db, eq } from 'astro:db';
import { tsPageData } from '../../db/tsTables';
import type { CombinedPageData } from '../types';
import { tsPageData } from '../tables';
import type { CombinedPageData, STUDIOCMS_SDK } from '../types';
import { collectPageData } from '../utils';

/**
Expand All @@ -10,7 +10,9 @@ import { collectPageData } from '../utils';
* @param packageName - The name of the package for which to retrieve pages.
* @returns A promise that resolves to an array of CombinedPageData objects.
*/
export async function getPackagePages(packageName: string): Promise<CombinedPageData[]> {
export async function getPackagePages(
packageName: Parameters<STUDIOCMS_SDK['GET']['packagePages']>[0]
): ReturnType<STUDIOCMS_SDK['GET']['packagePages']> {
const pages: CombinedPageData[] = [];

const pagesRaw = await db.select().from(tsPageData).where(eq(tsPageData.package, packageName));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="@astrojs/db" />
import { db } from 'astro:db';
import { tsPermissions, tsUsers } from '../../db/tsTables';
import type { AvailableLists, PermissionsList } from '../types';
import { tsPermissions, tsUsers } from '../tables';
import type { STUDIOCMS_SDK } from '../types';
import { combineRanks, verifyRank } from '../utils';

/**
Expand All @@ -27,7 +27,9 @@ import { combineRanks, verifyRank } from '../utils';
* console.log(owners);
* ```
*/
export async function getPermissionsLists(list: AvailableLists): Promise<PermissionsList[]> {
export async function getPermissionsLists(
list: Parameters<STUDIOCMS_SDK['GET']['permissionsLists']>[0]
): ReturnType<STUDIOCMS_SDK['GET']['permissionsLists']> {
switch (list) {
case 'all': {
const [currentPermittedUsers, existingUsers] = await db.batch([
Expand Down
Loading

0 comments on commit df34c8d

Please sign in to comment.