simplify db schema + pagination (#94)

This commit is contained in:
Daniel Holmgren 2024-07-08 12:59:26 -05:00 committed by GitHub
parent 12073636c7
commit 3e3f846a9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 3 additions and 17 deletions

View File

@ -1,4 +1,3 @@
import { InvalidRequestError } from '@atproto/xrpc-server'
import { QueryParams } from '../lexicon/types/app/bsky/feed/getFeedSkeleton'
import { AppContext } from '../config'
@ -14,15 +13,8 @@ export const handler = async (ctx: AppContext, params: QueryParams) => {
.limit(params.limit)
if (params.cursor) {
const [indexedAt, cid] = params.cursor.split('::')
if (!indexedAt || !cid) {
throw new InvalidRequestError('malformed cursor')
}
const timeStr = new Date(parseInt(indexedAt, 10)).toISOString()
builder = builder
.where('post.indexedAt', '<', timeStr)
.orWhere((qb) => qb.where('post.indexedAt', '=', timeStr))
.where('post.cid', '<', cid)
const timeStr = new Date(parseInt(params.cursor, 10)).toISOString()
builder = builder.where('post.indexedAt', '<', timeStr)
}
const res = await builder.execute()
@ -33,7 +25,7 @@ export const handler = async (ctx: AppContext, params: QueryParams) => {
let cursor: string | undefined
const last = res.at(-1)
if (last) {
cursor = `${new Date(last.indexedAt).getTime()}::${last.cid}`
cursor = new Date(last.indexedAt).getTime().toString(10)
}
return {

View File

@ -14,8 +14,6 @@ migrations['001'] = {
.createTable('post')
.addColumn('uri', 'varchar', (col) => col.primaryKey())
.addColumn('cid', 'varchar', (col) => col.notNull())
.addColumn('replyParent', 'varchar')
.addColumn('replyRoot', 'varchar')
.addColumn('indexedAt', 'varchar', (col) => col.notNull())
.execute()
await db.schema

View File

@ -6,8 +6,6 @@ export type DatabaseSchema = {
export type Post = {
uri: string
cid: string
replyParent: string | null
replyRoot: string | null
indexedAt: string
}

View File

@ -27,8 +27,6 @@ export class FirehoseSubscription extends FirehoseSubscriptionBase {
return {
uri: create.uri,
cid: create.cid,
replyParent: create.record?.reply?.parent.uri ?? null,
replyRoot: create.record?.reply?.root.uri ?? null,
indexedAt: new Date().toISOString(),
}
})