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

View File

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

View File

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

View File

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