Skip to content
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
1 change: 1 addition & 0 deletions apps/blog-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@holo-js/core": "workspace:*",
"@holo-js/db": "workspace:*",
"@holo-js/db-sqlite": "workspace:*",
"@holo-js/events": "workspace:*",
"@holo-js/flux": "workspace:*",
"@holo-js/flux-react": "workspace:*",
"@holo-js/forms": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('admins', (table) => {
table.id()
table.string('name')
Expand All @@ -12,7 +12,7 @@ export default defineMigration({
table.timestamps()
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('admins')
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('notifications', (table) => {
table.string('id').primaryKey()
table.string('type').nullable()
Expand All @@ -15,7 +15,7 @@ export default defineMigration({
table.index(['read_at'])
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('notifications')
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('users', (table) => {
table.id()
table.string('name')
Expand All @@ -12,7 +12,7 @@ export default defineMigration({
table.timestamps()
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('users')
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('sessions', (table) => {
table.string('id').primaryKey()
table.string('store').default('database')
Expand All @@ -14,7 +14,7 @@ export default defineMigration({
table.index(['expires_at'])
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('sessions')
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('auth_identities', (table) => {
table.id()
table.string('user_id')
Expand All @@ -18,7 +18,7 @@ export default defineMigration({
table.unique(['provider', 'provider_user_id'], 'auth_identities_provider_user_unique')
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('auth_identities')
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('personal_access_tokens', (table) => {
table.uuid('id').primaryKey()
table.string('provider').default('users')
Expand All @@ -16,7 +16,7 @@ export default defineMigration({
table.index(['user_id'])
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('personal_access_tokens')
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('password_reset_tokens', (table) => {
table.uuid('id').primaryKey()
table.string('provider').default('users')
Expand All @@ -14,7 +14,7 @@ export default defineMigration({
table.index(['email'])
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('password_reset_tokens')
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('email_verification_tokens', (table) => {
table.uuid('id').primaryKey()
table.string('provider').default('users')
Expand All @@ -16,7 +16,7 @@ export default defineMigration({
table.index(['email'])
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('email_verification_tokens')
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('comments', (table) => {
table.id()
table.integer('post_id')
Expand All @@ -13,7 +13,7 @@ export default defineMigration({
table.timestamps()
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('comments')
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
async up({ schema }) {
await schema.createTable('cache', (table) => {
table.string('key').primaryKey()
table.text('payload')
Expand All @@ -15,7 +15,7 @@ export default defineMigration({
table.index(['expires_at'], 'cache_locks_expires_at_index')
})
},
async down({ schema }: MigrationContext) {
async down({ schema }) {
await schema.dropTable('cache_locks')
await schema.dropTable('cache')
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }) {
await schema.createTable('failed_jobs', (table) => {
table.string('id').primaryKey()
table.string('job_id')
table.string('job')
table.string('connection')
table.string('queue')
table.text('payload')
table.text('exception')
table.bigInteger('failed_at')
table.index(['job_id'], 'failed_jobs_job_id_index')
table.index(['failed_at'], 'failed_jobs_failed_at_index')
})
},
async down({ schema }) {
await schema.dropTable('failed_jobs')
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { defineMigration } from '@holo-js/db'

export default defineMigration({
async up({ schema }) {
await schema.createTable('jobs', (table) => {
table.string('id').primaryKey()
table.string('job')
table.string('connection')
table.string('queue')
table.text('payload')
table.integer('attempts').default(0)
table.integer('max_attempts').default(1)
table.bigInteger('available_at')
table.bigInteger('reserved_at').nullable()
table.string('reservation_id').nullable()
table.bigInteger('created_at')
table.index(['queue', 'available_at'], 'jobs_queue_available_at_index')
table.index(['queue', 'reserved_at'], 'jobs_queue_reserved_at_index')
table.index(['reservation_id'], 'jobs_reservation_id_index')
})
},
async down({ schema }) {
await schema.dropTable('jobs')
},
})
13 changes: 13 additions & 0 deletions apps/blog-next/server/events/blog/post-saved.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineEvent } from '@holo-js/events'

export const BlogPostSaved = defineEvent<{
action: 'created' | 'updated' | 'deleted'
postId: number
title: string
status: string
slug: string
}>({
name: 'blog.post.saved',
})

export default BlogPostSaved
22 changes: 22 additions & 0 deletions apps/blog-next/server/jobs/blog/index-post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineJob } from '@holo-js/queue'

import Post from '../../models/Post'

export const IndexBlogPost = defineJob<{
action: 'created' | 'updated' | 'deleted'
postId: number
}>({
queue: 'default',
async handle(payload) {
const post = await Post.find(payload.postId)

return {
action: payload.action,
postId: payload.postId,
indexed: post !== undefined,
title: post?.title ?? null,
}
},
})

export default IndexBlogPost
22 changes: 22 additions & 0 deletions apps/blog-next/server/lib/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DB, uniqueSlug } from '@holo-js/db'
import { ValidationException } from '@holo-js/forms'

import { blogPostChanged } from '../broadcast/blog-post-changed'
import BlogPostSaved from '../events/blog/post-saved'
import Category from '../models/Category'
import Post from '../models/Post'
import Tag from '../models/Tag'
Expand Down Expand Up @@ -253,6 +254,13 @@ export async function createPost(input: { title: string, excerpt?: string, body:
}

await broadcast(blogPostChanged('created', post.id, post.title, post.status, post.slug))
await BlogPostSaved.dispatch({
action: 'created',
postId: post.id,
title: post.title,
status: post.status,
slug: post.slug,
})

return post
}
Expand Down Expand Up @@ -289,6 +297,13 @@ export async function updatePost(id: number, input: { title: string, excerpt?: s
}

await broadcast(blogPostChanged('updated', post.id, post.title, post.status, post.slug))
await BlogPostSaved.dispatch({
action: 'updated',
postId: post.id,
title: post.title,
status: post.status,
slug: post.slug,
})

return post
}
Expand All @@ -298,5 +313,12 @@ export async function deletePost(id: number) {
await Post.delete(id)
if (post) {
await broadcast(blogPostChanged('deleted', post.id, post.title, post.status, post.slug))
await BlogPostSaved.dispatch({
action: 'deleted',
postId: post.id,
title: post.title,
status: post.status,
slug: post.slug,
})
}
}
14 changes: 14 additions & 0 deletions apps/blog-next/server/listeners/blog/index-saved-post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineListener } from '@holo-js/events'

import BlogPostSaved from '../../events/blog/post-saved'
import IndexBlogPost from '../../jobs/blog/index-post'

export default defineListener({
listensTo: [BlogPostSaved],
async handle(event) {
await IndexBlogPost.dispatch({
action: event.payload.action,
postId: event.payload.postId,
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
},
})
9 changes: 9 additions & 0 deletions apps/blog-next/tests/blog-logic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ async function getRegisteredCacheKeys() {
return [...(await getCacheRuntimeBindings()?.dependencyIndex?.listRegisteredKeys() ?? [])]
}

async function countQueuedBlogIndexJobs() {
return await DB.table('jobs').where('job', 'blog.index-post').count()
}

function resolveDriverCacheKey(indexedKey) {
const delimiterIndex = indexedKey.indexOf('\u0000')
return delimiterIndex === -1 ? indexedKey : indexedKey.slice(delimiterIndex + 1)
Expand Down Expand Up @@ -286,6 +290,8 @@ try {
assert.ok(frameworkTag)
assert.ok(releaseTag)

const queuedJobsBeforePostLifecycle = await countQueuedBlogIndexJobs()

await createPost({
title: 'Logic Coverage Post',
excerpt: 'Logic excerpt',
Expand All @@ -294,6 +300,7 @@ try {
categoryId: String(updatedCategory.id),
tagIds: String(frameworkTag.id),
})
assert.equal(await countQueuedBlogIndexJobs(), queuedJobsBeforePostLifecycle + 1)
assert.equal(await countRegisteredCacheKeys(), 0)
assert.ok((await getPublishedPosts()).some(post => post.slug === 'logic-coverage-post'))

Expand Down Expand Up @@ -369,6 +376,7 @@ try {
categoryId: '',
tagIds: String(releaseTag.id),
})
assert.equal(await countQueuedBlogIndexJobs(), queuedJobsBeforePostLifecycle + 2)
assert.equal(await countRegisteredCacheKeys(), 0)

logicPost = await Post.with('category', 'tags').where('id', logicPost.id).first()
Expand Down Expand Up @@ -397,6 +405,7 @@ try {
assert.equal(await Category.find(updatedCategory.id), undefined)

await deletePost(logicPost.id)
assert.equal(await countQueuedBlogIndexJobs(), queuedJobsBeforePostLifecycle + 4)
await deletePost(cleanupPost.id)
if (mediaFailurePost) {
await deletePost(mediaFailurePost.id)
Expand Down
1 change: 1 addition & 0 deletions apps/blog-nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@holo-js/core": "workspace:*",
"@holo-js/db": "workspace:*",
"@holo-js/db-sqlite": "workspace:*",
"@holo-js/events": "workspace:*",
"@holo-js/flux": "workspace:*",
"@holo-js/flux-vue": "workspace:*",
"@holo-js/forms": "workspace:*",
Expand Down
Loading