Skip to content

Commit

Permalink
feat: skip unpublished posts
Browse files Browse the repository at this point in the history
  • Loading branch information
cptchloroplast committed Nov 6, 2024
1 parent 6222b58 commit d1f9a08
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions migrations/0001_add_posts_and_tags_tables.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE `posts` (
`content` text,
`content` text NOT NULL,
`description` text,
`published` text NOT NULL,
`published` text,
`slug` text PRIMARY KEY NOT NULL,
`title` text NOT NULL,
`type` text NOT NULL,
Expand Down
6 changes: 3 additions & 3 deletions migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "6",
"dialect": "sqlite",
"id": "33e6eabe-c792-4d57-bd4d-906a2df54df2",
"id": "95b0d99d-96d7-4a70-8e3d-63de6dc09478",
"prevId": "d0532e55-b97a-4c4f-b350-50a3301e31bb",
"tables": {
"components": {
Expand Down Expand Up @@ -164,7 +164,7 @@
"name": "content",
"type": "text",
"primaryKey": false,
"notNull": false,
"notNull": true,
"autoincrement": false
},
"description": {
Expand All @@ -178,7 +178,7 @@
"name": "published",
"type": "text",
"primaryKey": false,
"notNull": true,
"notNull": false,
"autoincrement": false
},
"slug": {
Expand Down
2 changes: 1 addition & 1 deletion migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{
"idx": 1,
"version": "6",
"when": 1723171014511,
"when": 1730863213827,
"tag": "0001_add_posts_and_tags_tables",
"breakpoints": true
}
Expand Down
4 changes: 2 additions & 2 deletions src/schemas/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { z } from "zod"
export const PostSchema = z.object({
content: z.string(),
description: z.string().optional(),
published: z.date(),
published: z.date().nullable().optional(),
slug: z.string(),
title: z.string(),
tags: z.array(z.string()),
Expand All @@ -15,7 +15,7 @@ export type Post = z.infer<typeof PostSchema>
export const PostsTable = sqliteTable("posts", {
content: text("content").notNull(),
description: text("description"),
published: text("published").notNull(),
published: text("published"),
slug: text("slug").primaryKey(),
title: text("title").notNull(),
type: text("type", { enum: ["post", "photo"] }).notNull(),
Expand Down
17 changes: 9 additions & 8 deletions src/services/PostService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { D1Database } from "@cloudflare/workers-types"
import { type Post, PostsTable, schema, TagsTable } from "@schemas"
import { conflictUpdateAllExcept } from "@utils"
import { asc, count, desc, eq, inArray, type InferSelectModel } from "drizzle-orm"
import { asc, count, desc, eq, inArray, type InferSelectModel, and, isNotNull } from "drizzle-orm"
import { drizzle } from "drizzle-orm/d1"

type PostService = {
Expand All @@ -16,12 +16,13 @@ type PostService = {
type PostRecord = InferSelectModel<typeof PostsTable> & { tags: InferSelectModel<typeof TagsTable>[] }
const tags = { orderBy: TagsTable.name }
const orderBy = [desc(PostsTable.published)]
const where = isNotNull(PostsTable.published)
function transformPost(post?: PostRecord): Post | undefined {
if (!post) return undefined
return Object.entries({
...post,
description: post.description ? post.description : undefined,
published: new Date(post.published),
published: post.published ? new Date(post.published) : undefined,
tags: post.tags.map(function(tag) { return tag.name }),
updated: post.updated ? new Date(post.updated) : undefined,
}).reduce(function(prev, [key, value]) {
Expand All @@ -40,28 +41,28 @@ export function PostService(d1: D1Database): PostService {
const db = drizzle(d1, { schema })
return {
async getBySlug(slug) {
return db.query.PostsTable.findFirst({ with: { tags }, where: eq(PostsTable.slug, slug) }).then(transformPost)
return db.query.PostsTable.findFirst({ with: { tags }, where: and(where, eq(PostsTable.slug, slug)) }).then(transformPost)
},
async getEarliest() {
return db.query.PostsTable.findFirst({ with: { tags }, orderBy: [asc(PostsTable.published)] }).then(transformPost)
return db.query.PostsTable.findFirst({ with: { tags }, orderBy: [asc(PostsTable.published)], where }).then(transformPost)
},
async getLatest() {
return db.query.PostsTable.findFirst({ with: { tags }, orderBy }).then(transformPost)
return db.query.PostsTable.findFirst({ with: { tags }, orderBy, where }).then(transformPost)
},
async list() {
return db.query.PostsTable.findMany({ with: { tags }, orderBy }).then(transformPosts)
return db.query.PostsTable.findMany({ with: { tags }, orderBy, where }).then(transformPosts)
},
async listByTag(tag) {
const query = db.select({ slug: TagsTable.post_slug }).from(TagsTable).where(eq(TagsTable.name, tag))
return db.query.PostsTable.findMany({ with: { tags }, orderBy, where: inArray(PostsTable.slug, query) }).then(transformPosts)
return db.query.PostsTable.findMany({ with: { tags }, orderBy, where: and(where, inArray(PostsTable.slug, query)) }).then(transformPosts)
},
async listTags() {
return db.select({ name: TagsTable.name, count: count(TagsTable.name)}).from(TagsTable).groupBy(TagsTable.name).orderBy(desc(count(TagsTable.name)), TagsTable.name)
},
async upsert(value) {
const record = {
...value,
published: value.published.toISOString(),
published: value.published?.toISOString(),
updated: value.updated?.toISOString(),
}
let [post] = await db.insert(PostsTable).values(record)
Expand Down
6 changes: 6 additions & 0 deletions test/services/PostService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ beforeEach(async function () {
title: "Post3",
type: "post",
},
{
content: "Post4 content",
slug: "post-four",
title: "Post4",
type: "post",
},
])
await db.insert(TagsTable).values([
{
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ bucket_name = "strava"
[[d1_databases]]
binding = "DB"
database_name = "blog"
database_id = "eb7f2bb4-2a43-4c8b-8f47-33e4a0205d12"
database_id = "690af449-57fa-41ac-9adf-e4e7ababf788"

0 comments on commit d1f9a08

Please sign in to comment.