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

fix: set updateAt on records when updating a record #1272

Merged
merged 13 commits into from
Feb 10, 2023
Merged
4 changes: 4 additions & 0 deletions packages/core/src/storage/IndyStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export class IndyStorageService<T extends BaseRecord<any, any, any>> implements
public async save(agentContext: AgentContext, record: T) {
assertIndyWallet(agentContext.wallet)

record.updatedAt = new Date()

const value = JsonTransformer.serialize(record)
const tags = this.transformFromRecordTagValues(record.getTags()) as Record<string, string>

Expand All @@ -157,6 +159,8 @@ export class IndyStorageService<T extends BaseRecord<any, any, any>> implements
public async update(agentContext: AgentContext, record: T): Promise<void> {
assertIndyWallet(agentContext.wallet)

record.updatedAt = new Date()

const value = JsonTransformer.serialize(record)
const tags = this.transformFromRecordTagValues(record.getTags()) as Record<string, string>

Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/storage/__tests__/IndyStorageService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type * as Indy from 'indy-sdk'
import { agentDependencies, getAgentConfig, getAgentContext } from '../../../tests/helpers'
import { SigningProviderRegistry } from '../../crypto/signing-provider'
import { RecordDuplicateError, RecordNotFoundError } from '../../error'
import { sleep } from '../../utils/sleep'
import { IndyWallet } from '../../wallet/IndyWallet'
import { IndyStorageService } from '../IndyStorageService'

Expand Down Expand Up @@ -113,6 +114,13 @@ describe('IndyStorageService', () => {

expect(record).toEqual(found)
})

it('After a save the record should have update the updatedAt property', async () => {
const time = new Date().getTime()
await sleep(1000)
const record = await insertRecord({ id: 'test-updatedAt' })
expect(record.updatedAt?.getTime()).toBeGreaterThan(time)
})
KolbyRKunz marked this conversation as resolved.
Show resolved Hide resolved
})

describe('getById()', () => {
Expand Down Expand Up @@ -151,6 +159,19 @@ describe('IndyStorageService', () => {
const retrievedRecord = await storageService.getById(agentContext, TestRecord, record.id)
expect(retrievedRecord).toEqual(record)
})

it('After a record has been updated it should have updated the updatedAT property', async () => {
const time = new Date()
await sleep(1000)
const record = await insertRecord({ id: 'test-id' })

record.replaceTags({ ...record.getTags(), foo: 'bar' })
record.foo = 'foobaz'
await storageService.update(agentContext, record)

const retrievedRecord = await storageService.getById(agentContext, TestRecord, record.id)
expect(retrievedRecord.createdAt.getTime()).toBeGreaterThan(time.getTime())
})
})

describe('delete()', () => {
Expand Down
2 changes: 2 additions & 0 deletions tests/InMemoryStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class InMemoryStorageService<T extends BaseRecord = BaseRecord> implement

/** @inheritDoc */
public async save(agentContext: AgentContext, record: T) {
record.updatedAt = new Date()
const value = JsonTransformer.toJSON(record)

if (this.records[record.id]) {
Expand All @@ -51,6 +52,7 @@ export class InMemoryStorageService<T extends BaseRecord = BaseRecord> implement

/** @inheritDoc */
public async update(agentContext: AgentContext, record: T): Promise<void> {
record.updatedAt = new Date()
const value = JsonTransformer.toJSON(record)
delete value._tags

Expand Down