Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehewitt15 committed Aug 12, 2024
1 parent a1e46f2 commit 96339b0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
Binary file added nonceDatabase.sqlite
Binary file not shown.
18 changes: 13 additions & 5 deletions src/components/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,12 +1059,20 @@ export class Database {
)
}
return (async (): Promise<Database> => {
this.ddo = await new DdoDatabase(this.config, schemas.ddoSchemas)
this.nonce = await new NonceDatabase(this.config, schemas.nonceSchemas)
this.indexer = await new IndexerDatabase(this.config, schemas.indexerSchemas)
this.logs = await new LogDatabase(this.config, schemas.logSchemas)
this.order = await new OrderDatabase(this.config, schemas.orderSchema)
this.ddoState = await new DdoStateDatabase(this.config, schemas.ddoStateSchema)
if (this.config.url && URLUtils.isValidUrl(this.config.url)) {
this.ddo = await new DdoDatabase(this.config, schemas.ddoSchemas)
console.log('Created DDO database')
this.indexer = await new IndexerDatabase(this.config, schemas.indexerSchemas)
this.logs = await new LogDatabase(this.config, schemas.logSchemas)
this.order = await new OrderDatabase(this.config, schemas.orderSchema)
this.ddoState = await new DdoStateDatabase(this.config, schemas.ddoStateSchema)
} else {
DATABASE_LOGGER.info(
'Typesense URL is not valid, falling back to SQLite for nonce database. Other DBs will not be available.'
)
}

return this
})() as unknown as Database
}
Expand Down
20 changes: 15 additions & 5 deletions src/components/database/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface DatabaseProvider {
create(address: string, nonce: number): Promise<{ id: string; nonce: number }>
retrieve(address: string): Promise<{ id: string; nonce: number | null }>
update(address: string, nonce: number): Promise<{ id: string; nonce: number }>
delete(address: string): Promise<{ id: string; nonce: null }>
delete(address: string): Promise<{ id: string; nonce: number | null }>
}

export class SQLiteProvider implements DatabaseProvider {
Expand Down Expand Up @@ -69,13 +69,23 @@ export class SQLiteProvider implements DatabaseProvider {

// eslint-disable-next-line require-await
async delete(address: string) {
const selectSQL = `
SELECT nonce FROM ${this.schema.name} WHERE id = ?
`

const deleteSQL = `
DELETE FROM ${this.schema.name} WHERE id = ?
`
return new Promise<{ id: string; nonce: null }>((resolve, reject) => {
this.db.run(deleteSQL, [address], (err) => {
if (err) reject(err)
else resolve({ id: address, nonce: null })

return new Promise<{ id: string; nonce: number | null }>((resolve, reject) => {
this.db.get(selectSQL, [address], (err, row: { nonce: number } | undefined) => {
if (err) return reject(err)
if (!row) return resolve({ id: address, nonce: null })

this.db.run(deleteSQL, [address], (err) => {
if (err) reject(err)
else resolve({ id: address, nonce: row.nonce })
})
})
})
}
Expand Down
1 change: 1 addition & 0 deletions src/test/integration/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe('NonceDatabase CRUD with SQLite', () => {

it('delete nonce', async () => {
const result = await database.nonce.delete('0x456')
console.log('Delete nonce result: ', result)
expect(result?.id).to.equal('0x456')
expect(result?.nonce).to.equal(1)
})
Expand Down

0 comments on commit 96339b0

Please sign in to comment.