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

feat: delete by record id #983

Merged
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
Expand Up @@ -53,6 +53,10 @@ export class GenericRecordsModule {
}
}

public async deleteById(id: string): Promise<void> {
await this.genericRecordsService.deleteById(id)
}

public async update(record: GenericRecord): Promise<void> {
try {
await this.genericRecordsService.update(record)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class GenericRecordService {
}
}

public async deleteById(id: string): Promise<void> {
await this.genericRecordsRepository.deleteById(id)
}

public async update(record: GenericRecord): Promise<void> {
try {
await this.genericRecordsRepository.update(record)
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/storage/IndyStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ export class IndyStorageService<T extends BaseRecord> implements StorageService<
}
}

/** @inheritDoc */
public async deleteById(recordClass: BaseRecordConstructor<T>, id: string): Promise<void> {
try {
await this.indy.deleteWalletRecord(this.wallet.handle, recordClass.type, id)
} catch (error) {
if (isIndyError(error, 'WalletItemNotFound')) {
throw new RecordNotFoundError(`record with id ${id} not found.`, {
recordType: recordClass.type,
cause: error,
})
}

throw isIndyError(error) ? new IndySdkError(error) : error
}
}

/** @inheritDoc */
public async getById(recordClass: BaseRecordConstructor<T>, id: string): Promise<T> {
try {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/storage/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ export class Repository<T extends BaseRecord<any, any, any>> {
})
}

/**
* Delete record by id. Returns null if no record is found
* @param id the id of the record to delete
* @returns
*/
public async deleteById(id: string): Promise<void> {
await this.storageService.deleteById(this.recordClass, id)
}

/** @inheritDoc {StorageService#getById} */
public async getById(id: string): Promise<T> {
return this.storageService.getById(this.recordClass, id)
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/storage/StorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ export interface StorageService<T extends BaseRecord<any, any, any>> {
*/
delete(record: T): Promise<void>

/**
* Delete record by id.
*
* @param recordClass the record class to delete the record for
* @param id the id of the record to delete from storage
* @throws {RecordNotFoundError} if a record with this id and type does not exist
*/
deleteById(recordClass: BaseRecordConstructor<T>, id: string): Promise<void>

/**
* Get record by id.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/storage/__tests__/Repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ describe('Repository', () => {
})
})

describe('deleteById()', () => {
it('should delete the record by record id', async () => {
await repository.deleteById('test-id')

expect(storageMock.deleteById).toBeCalledWith(TestRecord, 'test-id')
})
})

describe('getById()', () => {
it('should get the record using the storage service', async () => {
const record = getRecord({ id: 'test-id' })
Expand Down
18 changes: 18 additions & 0 deletions packages/core/tests/generic-records.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { GenericRecord } from '../src/modules/generic-records/repository/GenericRecord'

import { Agent } from '../src/agent/Agent'
import { RecordNotFoundError } from '../src/error'

import { getBaseConfig } from './helpers'

Expand Down Expand Up @@ -83,6 +84,23 @@ describe('genericRecords', () => {
expect(retrievedRecord).toBeNull()
})

test('delete generic record by id', async () => {
const myId = 'test-id'
const savedRecord = await aliceAgent.genericRecords.save({ content: barString, id: myId })
expect(savedRecord).toBeDefined()

await aliceAgent.genericRecords.deleteById(savedRecord.id)

const retrievedRecord = await aliceAgent.genericRecords.findById(savedRecord.id)
expect(retrievedRecord).toBeNull()
})
test('throws an error if record not found by id ', async () => {
const deleteRecordById = async () => {
await aliceAgent.genericRecords.deleteById('test')
}
expect(deleteRecordById).rejects.toThrow(RecordNotFoundError)
})

test('update generic record', async () => {
const myId = '102'
const savedRecord1: GenericRecord = await aliceAgent.genericRecords.save({ content: barString, id: myId })
Expand Down
11 changes: 11 additions & 0 deletions tests/InMemoryStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ export class InMemoryStorageService<T extends BaseRecord = BaseRecord> implement
delete this.records[record.id]
}

/** @inheritDoc */
public async deleteById(recordClass: BaseRecordConstructor<T>, id: string): Promise<void> {
if (!this.records[id]) {
throw new RecordNotFoundError(`record with id ${id} not found.`, {
recordType: recordClass.type,
})
}

delete this.records[id]
}

/** @inheritDoc */
public async getById(recordClass: BaseRecordConstructor<T>, id: string): Promise<T> {
const record = this.records[id]
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7652,7 +7652,7 @@ node-dir@^0.1.17:
dependencies:
minimatch "^3.0.2"

node-fetch@2.6.7, node-fetch@^2.2.0, node-fetch@^2.6.0, node-fetch@^2.6.1, node-fetch@^2.6.7:
node-fetch@2.6.7, node-fetch@^2.0, node-fetch@^2.2.0, node-fetch@^2.6.0, node-fetch@^2.6.1, node-fetch@^2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
Expand Down