use single row for cursor

This commit is contained in:
Hailey
2025-09-01 10:07:50 -07:00
parent 6d73e3ed3c
commit 4da6bd5b95
3 changed files with 10 additions and 8 deletions
+5 -3
View File
@@ -227,8 +227,7 @@ export class SafelinkClient {
const res = await this.db.db const res = await this.db.db
.selectFrom('safelink_cursor') .selectFrom('safelink_cursor')
.selectAll() .selectAll()
.orderBy('createdAt', 'desc') .where('id', '=', 1)
.limit(1)
.executeTakeFirst() .executeTakeFirst()
if (!res) { if (!res) {
return '' return ''
@@ -239,13 +238,16 @@ export class SafelinkClient {
} }
private async setCursor(cursor: string) { private async setCursor(cursor: string) {
const updatedAt = new Date()
try { try {
await this.db.db await this.db.db
.insertInto('safelink_cursor') .insertInto('safelink_cursor')
.values({ .values({
id: 1,
cursor, cursor,
createdAt: new Date(), updatedAt,
}) })
.onConflict(oc => oc.column('id').doUpdateSet({cursor, updatedAt}))
.execute() .execute()
this.cursor = cursor this.cursor = cursor
} catch (err) { } catch (err) {
+2 -2
View File
@@ -16,9 +16,9 @@ export async function up(db: Kysely<unknown>): Promise<void> {
await db.schema await db.schema
.createTable('safelink_cursor') .createTable('safelink_cursor')
.addColumn('id', 'bigserial', col => col.primaryKey()) .addColumn('id', 'bigserial', col => col.notNull())
.addColumn('cursor', 'varchar', col => col.notNull()) .addColumn('cursor', 'varchar', col => col.notNull())
.addColumn('createdAt', 'timestamptz', col => col.notNull()) .addColumn('updatedAt', 'timestamptz', col => col.notNull())
.execute() .execute()
await db.schema await db.schema
+3 -3
View File
@@ -1,4 +1,4 @@
import {type GeneratedAlways, type Selectable} from 'kysely' import {type Selectable} from 'kysely'
export type DbSchema = { export type DbSchema = {
link: Link link: Link
@@ -43,9 +43,9 @@ export interface SafelinkRule {
} }
export interface SafelinkCursor { export interface SafelinkCursor {
id: GeneratedAlways<number> id: number
cursor: string cursor: string
createdAt: Date updatedAt: Date
} }
export type LinkEntry = Selectable<Link> export type LinkEntry = Selectable<Link>