Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Page data table schemas #367

Merged
merged 14 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/clever-yaks-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@studiocms/devapps": patch
"@studiocms/core": patch
---

Expand PageData table schema and add Catagory and Tags schemas, and extend WP-importer
4 changes: 4 additions & 0 deletions packages/studiocms_core/src/db/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { defineDb } from 'astro:db';
import {
StudioCMSPageContent,
StudioCMSPageData,
StudioCMSPageDataCategories,
StudioCMSPageDataTags,
StudioCMSPermissions,
StudioCMSSessionTable,
StudioCMSSiteConfig,
Expand All @@ -14,6 +16,8 @@ export default defineDb({
tables: {
StudioCMSPageContent,
StudioCMSPageData,
StudioCMSPageDataCategories,
StudioCMSPageDataTags,
StudioCMSPermissions,
StudioCMSSessionTable,
StudioCMSSiteConfig,
Expand Down
25 changes: 25 additions & 0 deletions packages/studiocms_core/src/db/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ 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',
}),
catagories: column.json({ default: [], optional: true }),
tags: column.json({ default: [], optional: true }),
},
});

export const StudioCMSPageDataTags = defineTable({
columns: {
id: column.number({ primaryKey: true }),
description: column.text(),
name: column.text(),
slug: column.text(),
meta: column.json({ default: {} }),
},
});

export const StudioCMSPageDataCategories = defineTable({
columns: {
id: column.number({ primaryKey: true }),
parent: column.number({ optional: true }),
description: column.text(),
name: column.text(),
slug: column.text(),
meta: column.json({ default: {} }),
},
});

Expand All @@ -65,6 +88,8 @@ export const StudioCMSSiteConfig = defineTable({
id: column.number({ primaryKey: true }),
title: column.text(),
description: column.text(),
defaultOgImage: column.text({ optional: true }),
siteIcon: column.text({ optional: true }),
},
});

Expand Down
17 changes: 17 additions & 0 deletions packages/studiocms_core/src/db/tsTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { asDrizzleTable } from '@astrojs/db/utils';
import {
StudioCMSPageContent,
StudioCMSPageData,
StudioCMSPageDataCategories,
StudioCMSPageDataTags,
StudioCMSPermissions,
StudioCMSSessionTable,
StudioCMSSiteConfig,
Expand All @@ -20,6 +22,21 @@ export const tsPageContent = asDrizzleTable('StudioCMSPageContent', StudioCMSPag
*/
export const tsPageData = asDrizzleTable('StudioCMSPageData', StudioCMSPageData);

/**
* # StudioCMS - Page Data Categories Table
* @description Exported TypeSafe Table definition for use in StudioCMS Integrations
*/
export const tsPageDataCategories = asDrizzleTable(
'StudioCMSPageDataCategories',
StudioCMSPageDataCategories
);

/**
* # StudioCMS - Page Data Tags Table
* @description Exported TypeSafe Table definition for use in StudioCMS Integrations
*/
export const tsPageDataTags = asDrizzleTable('StudioCMSPageDataTags', StudioCMSPageDataTags);

/**
* # StudioCMS - Permissions Table
* @description Exported TypeSafe Table definition for use in StudioCMS Integrations
Expand Down
1 change: 1 addition & 0 deletions packages/studiocms_devapps/src/apps/wp-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export default defineToolbarApp({
<select id="importer-type" name="importer-type" required>
<option value="pages" selected>Pages</option>
<option value="posts">Posts</option>
<option value="settings">Site Settings (This option will override your StudioCMS Site Config)</option>
</select>
</div>

Expand Down
9 changes: 8 additions & 1 deletion packages/studiocms_devapps/src/routes/wp-api-importer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { APIContext, APIRoute } from 'astro';
import { importPagesFromWPAPI, importPostsFromWPAPI } from '../utils/wp-api';
import {
importPagesFromWPAPI,
importPostsFromWPAPI,
importSettingsFromWPAPI,
} from '../utils/wp-api';

export const POST: APIRoute = async ({ request }: APIContext) => {
const data = await request.formData();
Expand Down Expand Up @@ -41,6 +45,9 @@ export const POST: APIRoute = async ({ request }: APIContext) => {
case 'posts':
await importPostsFromWPAPI(url, useBlogPluginValue);
break;
case 'settings':
await importSettingsFromWPAPI(url);
break;
default:
throw new Error('Invalid import type');
}
Expand Down
30 changes: 30 additions & 0 deletions packages/studiocms_devapps/src/schema/wp-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,35 @@ export const PostSchema = PageSchema.extend({
tags: z.array(z.number()),
});

export const TagSchema = z.object({
id: z.number(),
count: z.number(),
description: z.string(),
link: z.string().url(),
name: z.string(),
slug: z.string(),
taxonomy: z.string(),
meta: z.array(z.any()).or(z.record(z.any())),
});

export const CategorySchema = TagSchema.extend({
parent: z.number(),
});

export const SiteSettingsSchema = z.object({
name: z.string(),
description: z.string(),
url: z.string(),
home: z.string(),
gmt_offset: z.coerce.number(),
timezone_string: z.string(),
site_logo: z.number().optional(),
site_icon: z.number().optional(),
site_icon_url: z.string().optional(),
});

export type Page = typeof PageSchema._output;
export type Post = typeof PostSchema._output;
export type Tag = typeof TagSchema._output;
export type Category = typeof CategorySchema._output;
export type SiteSettings = typeof SiteSettingsSchema._output;
109 changes: 104 additions & 5 deletions packages/studiocms_devapps/src/utils/wp-api/converters.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from 'node:path';
import { db, eq } from 'astro:db';
import Config from 'virtual:studiocms-devapps/wp-api/configPath';
import { tsPageDataCategories, tsPageDataTags } from '@studiocms/core/db/tsTables';
import { decode } from 'html-entities';
import TurndownService from 'turndown';
import type { Page, Post } from '../../schema/wp-api';
import type { Category, Page, Post, Tag } from '../../schema/wp-api';
import type { PageContent, PageData } from './index';
import {
apiEndpoint,
Expand All @@ -17,11 +19,11 @@ const WPImportFolder = path.resolve(ASTROPUBLICFOLDER, 'wp-import');
const pagesImagesFolder = path.resolve(WPImportFolder, 'pages');
const postsImagesFolder = path.resolve(WPImportFolder, 'posts');

export const ConvertToPageData = async (page: unknown): Promise<PageData> => {
export const ConvertToPageData = async (page: unknown, endpoint: string): Promise<PageData> => {
const data = page as Page;

const titleImageId = data.featured_media;
const titleImageURL = apiEndpoint(`${titleImageId}`, 'media');
const titleImageURL = apiEndpoint(endpoint, 'media', `${titleImageId}`);
const titleImageResponse = await fetch(titleImageURL);
const titleImageJson = await titleImageResponse.json();
const titleImage = await downloadPostImage(titleImageJson.source_url, pagesImagesFolder);
Expand Down Expand Up @@ -76,17 +78,112 @@ export const ConvertToPageContent = async (
return pageContent;
};

export const ConvertToPostData = async (post: unknown, useBlogPkg: boolean): Promise<PageData> => {
export const generateCatagories = async (categories: number[], endpoint: string) => {
const newCatagories: Category[] = [];

for (const catagoryId of categories) {
// Check if catagory already exists in the database
const catagoryExists = await db
.select()
.from(tsPageDataCategories)
.where(eq(tsPageDataCategories.id, catagoryId))
.get();

if (catagoryExists) {
console.log(`Catagory with id ${catagoryId} already exists in the database`);
Adammatthiesen marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

const catagoryURL = apiEndpoint(endpoint, 'catagories', `${catagoryId}`);
const response = await fetch(catagoryURL);
const json = await response.json();
newCatagories.push(json);
}

if (newCatagories.length > 0) {
const catagoryData = newCatagories.map((catagory) => {
const data: typeof tsPageDataCategories.$inferInsert = {
id: catagory.id,
name: catagory.name,
slug: catagory.slug,
description: catagory.description,
meta: JSON.stringify(catagory.meta),
};

if (catagory.parent) {
data.parent = catagory.parent;
}

return data;
});

for (const catagory of catagoryData) {
console.log(`Inserting catagory with id ${catagory.id} into the database`);
await db.insert(tsPageDataCategories).values(catagory);
}
}
};

export const generateTags = async (tags: number[], endpoint: string) => {
const newTags: Tag[] = [];

for (const tagId of tags) {
// Check if tag already exists in the database
const tagExists = await db
.select()
.from(tsPageDataTags)
.where(eq(tsPageDataTags.id, tagId))
.get();

if (tagExists) {
console.log(`Tag with id ${tagId} already exists in the database`);
continue;
}

const tagURL = apiEndpoint(endpoint, 'tags', `${tagId}`);
const response = await fetch(tagURL);
const json = await response.json();
newTags.push(json);
}

if (newTags.length > 0) {
const tagData = newTags.map((tag) => {
const data: typeof tsPageDataTags.$inferInsert = {
id: tag.id,
name: tag.name,
slug: tag.slug,
description: tag.description,
meta: JSON.stringify(tag.meta),
};

return data;
});

for (const tag of tagData) {
console.log(`Inserting tag with id ${tag.id} into the database`);
await db.insert(tsPageDataTags).values(tag);
}
}
};

export const ConvertToPostData = async (
post: unknown,
useBlogPkg: boolean,
endpoint: string
): Promise<PageData> => {
const data = post as Post;

const titleImageId = data.featured_media;
const titleImageURL = apiEndpoint(`${titleImageId}`, 'media');
const titleImageURL = apiEndpoint(endpoint, 'media', `${titleImageId}`);
const titleImageResponse = await fetch(titleImageURL);
const titleImageJson = await titleImageResponse.json();
const titleImage = await downloadPostImage(titleImageJson.source_url, pagesImagesFolder);

const pkg = useBlogPkg ? '@studiocms/blog' : 'studiocms';

await generateCatagories(data.categories, endpoint);
await generateTags(data.tags, endpoint);

const pageData: PageData = {
id: crypto.randomUUID(),
title: data.title.rendered,
Expand All @@ -97,6 +194,8 @@ export const ConvertToPostData = async (post: unknown, useBlogPkg: boolean): Pro
showOnNav: false,
contentLang: 'default',
package: pkg,
catagories: JSON.stringify(data.categories),
tags: JSON.stringify(data.tags),
};

if (titleImage) {
Expand Down
Loading