-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(egh): update lesson schema and refactor Typesense lesson upsert
- Changed `published_at` field type from `number` to `string` in `updateEggheadLesson` function to store ISO date format. - Updated `writePostUpdateToDatabase` to use ISO string for `published_at`. - Introduced new `typesense-query.ts` file for managing Typesense operations, including upserting posts. - Enhanced `InstructorSchema` and `TypesensePostSchema` to include additional instructor details and ensure proper validation. - Refactored import paths for better organization.
- Loading branch information
1 parent
2d0379f
commit 214966d
Showing
4 changed files
with
128 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import Typesense from 'typesense' | ||
|
||
import { getEggheadLesson } from './egghead' | ||
import { Post, PostAction } from './posts' | ||
import { getPostTags } from './posts-query' | ||
import { InstructorSchema, TypesensePostSchema } from './typesense' | ||
|
||
export async function upsertPostToTypeSense(post: Post, action: PostAction) { | ||
let client = new Typesense.Client({ | ||
nodes: [ | ||
{ | ||
host: process.env.NEXT_PUBLIC_TYPESENSE_HOST!, | ||
port: 443, | ||
protocol: 'https', | ||
}, | ||
], | ||
apiKey: process.env.TYPESENSE_WRITE_API_KEY!, | ||
connectionTimeoutSeconds: 2, | ||
}) | ||
|
||
const shouldIndex = | ||
post.fields.state === 'published' && post.fields.visibility === 'public' | ||
|
||
if (!shouldIndex) { | ||
await client | ||
.collections(process.env.TYPESENSE_COLLECTION_NAME!) | ||
.documents(String(post.fields.eggheadLessonId)) | ||
.delete() | ||
.catch((err) => { | ||
console.error(err) | ||
}) | ||
} else { | ||
if (!post.fields.eggheadLessonId) { | ||
return | ||
} | ||
|
||
const lesson = await getEggheadLesson(post.fields.eggheadLessonId) | ||
|
||
console.log('lesson', lesson) | ||
|
||
const instructor = InstructorSchema.parse({ | ||
id: lesson.instructor?.id, | ||
name: lesson.instructor?.full_name, | ||
first_name: lesson.instructor?.first_name, | ||
last_name: lesson.instructor?.last_name, | ||
url: lesson.instructor?.url, | ||
avatar_url: lesson.instructor?.avatar_url, | ||
}) | ||
|
||
const tags = await getPostTags(post.id) | ||
|
||
const resource = TypesensePostSchema.safeParse({ | ||
id: `${post.fields.eggheadLessonId}`, | ||
externalId: post.id, | ||
title: post.fields.title, | ||
slug: post.fields.slug, | ||
summary: post.fields.description, | ||
description: post.fields.body, | ||
name: post.fields.title, | ||
path: `/${post.fields.slug}`, | ||
type: post.fields.postType, | ||
_tags: tags.map((tag) => tag.fields.name), | ||
...(lesson && { | ||
instructor_name: lesson.instructor?.full_name, | ||
instructor, | ||
image: lesson.image_480_url, | ||
published_at_timestamp: lesson.published_at | ||
? new Date(lesson.published_at).getTime() | ||
: null, | ||
}), | ||
}) | ||
|
||
if (!resource.success) { | ||
console.error(resource.error) | ||
return | ||
} | ||
|
||
console.log('resource', resource.data) | ||
|
||
await client | ||
.collections(process.env.TYPESENSE_COLLECTION_NAME!) | ||
.documents() | ||
.upsert({ | ||
...resource.data, | ||
...(action === 'publish' && { | ||
published_at_timestamp: post.updatedAt?.getTime() ?? Date.now(), | ||
}), | ||
updated_at_timestamp: post.updatedAt?.getTime() ?? Date.now(), | ||
}) | ||
.catch((err) => { | ||
console.error(err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,30 @@ | ||
import Typesense from 'typesense' | ||
import * as z from 'zod' | ||
|
||
import { getEggheadLesson } from './egghead' | ||
import { Post, PostAction } from './posts' | ||
export const InstructorSchema = z.object({ | ||
id: z.number().optional(), | ||
name: z.string().optional(), | ||
first_name: z.string().optional(), | ||
last_name: z.string().optional(), | ||
full_name: z.string().optional(), | ||
avatar_url: z.string().url().optional(), | ||
}) | ||
|
||
export async function upsertPostToTypeSense(post: Post, action: PostAction) { | ||
let client = new Typesense.Client({ | ||
nodes: [ | ||
{ | ||
host: process.env.NEXT_PUBLIC_TYPESENSE_HOST!, | ||
port: 443, | ||
protocol: 'https', | ||
}, | ||
], | ||
apiKey: process.env.TYPESENSE_WRITE_API_KEY!, | ||
connectionTimeoutSeconds: 2, | ||
}) | ||
export const TypesensePostSchema = z.object({ | ||
type: z.string().optional(), | ||
id: z.string().optional(), | ||
name: z.string().optional(), | ||
title: z.string().optional(), | ||
slug: z.string().optional(), | ||
externalId: z.string().optional(), | ||
description: z.string().optional(), | ||
summary: z.string().optional(), | ||
image: z.string().optional(), | ||
_tags: z.array(z.string()).optional(), | ||
instructor: InstructorSchema.optional(), | ||
instructor_name: z.string().optional(), | ||
instructor_url: z.string().url().optional(), | ||
path: z.string().optional(), | ||
published_at_timestamp: z.number().nullish(), | ||
}) | ||
|
||
const shouldIndex = | ||
post.fields.state === 'published' && post.fields.visibility === 'public' | ||
|
||
if (!shouldIndex) { | ||
await client | ||
.collections(process.env.TYPESENSE_COLLECTION_NAME!) | ||
.documents(String(post.fields.eggheadLessonId)) | ||
.delete() | ||
.catch((err) => { | ||
console.error(err) | ||
}) | ||
} else { | ||
if (!post.fields.eggheadLessonId) { | ||
return | ||
} | ||
const lesson = await getEggheadLesson(post.fields.eggheadLessonId) | ||
const resource = { | ||
id: `${post.fields.eggheadLessonId}`, | ||
externalId: post.id, | ||
title: post.fields.title, | ||
slug: post.fields.slug, | ||
summary: post.fields.description, | ||
description: post.fields.body, | ||
name: post.fields.title, | ||
path: `/${post.fields.slug}`, | ||
type: post.fields.postType, | ||
...(lesson && { | ||
instructor_name: lesson.instructor?.full_name, | ||
instructor: lesson.instructor, | ||
image: lesson.image_480_url, | ||
}), | ||
} | ||
await client | ||
.collections(process.env.TYPESENSE_COLLECTION_NAME!) | ||
.documents() | ||
.upsert({ | ||
...resource, | ||
...(action === 'publish' && { | ||
published_at_timestamp: post.updatedAt?.getTime() ?? Date.now(), | ||
}), | ||
updated_at_timestamp: post.updatedAt?.getTime() ?? Date.now(), | ||
}) | ||
.catch((err) => { | ||
console.error(err) | ||
}) | ||
} | ||
} | ||
export type TypesensePost = z.infer<typeof TypesensePostSchema> |