Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IndexerStateRepository #7

Merged
merged 1 commit into from
Sep 13, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Logger } from '@l2beat/backend-tools'
import { expect } from 'earl'

import { setupDatabaseTestSuite } from '../../test/database'
import { IndexerStateRepository } from './IndexerStateRepository'

describe(IndexerStateRepository.name, () => {
const { database } = setupDatabaseTestSuite()
const repository = new IndexerStateRepository(database, Logger.SILENT)

before(() => repository.deleteAll())
afterEach(() => repository.deleteAll())

it('adds single record and queries it', async () => {
const record = {
id: 'id',
height: 1,
}

await repository.addOrUpdate(record)

const actual = await repository.getAll()

expect(actual).toEqual([record])
})

it('updates existing record', async () => {
const record = {
id: 'id',
height: 1,
}

await repository.addOrUpdate(record)
await repository.addOrUpdate({ ...record, height: 2 })

const actual = await repository.getAll()

expect(actual).toEqual([{ ...record, height: 2 }])
})

it('finds record by id', async () => {
const record = {
id: 'id',
height: 1,
}

const record2 = {
id: 'id2',
height: 2,
}

await repository.addOrUpdate(record)
await repository.addOrUpdate(record2)

const actual = await repository.findById('id2')

expect(actual).toEqual(record2)
})

it('delete all records', async () => {
await repository.addOrUpdate({
id: 'id',
height: 1,
})
await repository.addOrUpdate({
id: 'id2',
height: 2,
})

await repository.deleteAll()

const actual = await repository.getAll()

expect(actual).toEqual([])
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Logger } from '@l2beat/backend-tools'
import type { IndexerStateRow } from 'knex/types/tables'

import { BaseRepository, CheckConvention } from './shared/BaseRepository'
import { Database } from './shared/Database'

export interface IndexerStateRecord {
id: string // TODO: Maybe branded string?
height: number
}

export class IndexerStateRepository extends BaseRepository {
constructor(database: Database, logger: Logger) {
super(database, logger)
this.autoWrap<CheckConvention<IndexerStateRepository>>(this)
}

async addOrUpdate(record: IndexerStateRecord): Promise<string> {
const row = toRow(record)
const knex = await this.knex()
await knex('indexer_states').insert(row).onConflict('id').merge()
return record.id
}

async findById(id: string): Promise<IndexerStateRecord | undefined> {
const knex = await this.knex()
const row = await knex('indexer_states').where('id', id).first()
return row && toRecord(row)
}

async getAll(): Promise<IndexerStateRecord[]> {
const knex = await this.knex()
const rows = await knex('indexer_states').select('*')
return rows.map(toRecord)
}

async deleteAll(): Promise<number> {
const knex = await this.knex()
return knex('indexer_states').delete()
}
}

function toRow(record: IndexerStateRecord): IndexerStateRow {
return {
id: record.id,
height: record.height,
last_updated: new Date(),
}
}

function toRecord(row: IndexerStateRow): IndexerStateRecord {
return {
id: row.id,
height: row.height,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
====== IMPORTANT NOTICE ======

DO NOT EDIT OR RENAME THIS FILE

This is a migration file. Once created the file should not be renamed or edited,
because migrations are only run once on the production server.

If you find that something was incorrectly set up in the `up` function you
should create a new migration file that fixes the issue.

*/

import { Knex } from 'knex'

export async function up(knex: Knex): Promise<void> {
await knex.schema.createTable('indexer_states', (table) => {
table.string('id').primary()
table.integer('height').notNullable()
table.dateTime('last_updated', { useTz: false }).notNullable()
})
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.dropTable('indexer_states')
}
7 changes: 7 additions & 0 deletions packages/backend/src/peripherals/database/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ declare module 'knex/types/tables' {
block_number: number
}

interface IndexerStateRow {
id: string
height: number
last_updated: Date
}

interface Tables {
block_numbers: BlockNumberRow
indexer_states: IndexerStateRow
}
}