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 updateHistoryEntry #48

Merged
merged 1 commit into from
Jun 11, 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
5 changes: 4 additions & 1 deletion src/storage/store/HistoryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IHistoryEntry, IKeyValuePair } from '@model'
import { getHistoryGroupDate } from '@util'
import type { payLnInvoice } from '@wallet'

import { type ISelectParams,StoreBase } from './StoreBase'
import { type ISelectParams, StoreBase } from './StoreBase'
import { getDb } from './utils'

/**
Expand Down Expand Up @@ -62,6 +62,9 @@ class HistoryStore extends StoreBase {
await this.#init()
return super.getObjsAll<IHistoryEntry>({ order, start, count, orderBy })
}
public updateHistoryEntry(oldEntry: IHistoryEntry, newEntry: IHistoryEntry) {
return super.updateObjByValue(oldEntry, newEntry)
}
/**
* clear history
* @returns Promise<void>
Expand Down
18 changes: 18 additions & 0 deletions src/storage/store/StoreBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ export abstract class StoreBase {
)
return result?.item?.(0)?.value
}
protected async updateByValue(oldValue: string, newValue: string) :Promise<boolean> {
if (!this._isReady) {
await this._createStore()
if (!this._isReady) { return false }
}
const result = await this._db.exec({
sql: `UPDATE ${this._name} SET value = ? WHERE KEY in (SELECT KEY FROM ${this._name} WHERE value = ? LIMIT 1)`,
args:[newValue,oldValue]
})
return !!(result && 'rowsAffected' in result && result?.rowsAffected===1)
}
protected async updateObjByValue<T extends object>(oldValue: T, newValue: T): Promise<boolean>{
if (!this._isReady) {
await this._createStore()
if (!this._isReady) { return false }
}
return this.updateByValue(toJson(oldValue), toJson(newValue))
}
protected async getObj<T extends object>(key: string): Promise<T | null | undefined> {
const strVal = await this.get(key)
if (!strVal) { return null }
Expand Down
6 changes: 6 additions & 0 deletions test/HistoryStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ describe('test HistoryStore', () => {
expect(await store.getHistoryWithKeys({ start: 1 })).toStrictEqual([
{ key: '0', value: entry },
])
// test update value by old value
expect(await store.updateHistoryEntry(entry, { ...entry, amount: 69 })).toBe(true)
expect(await store.getHistoryWithKeys()).toStrictEqual([
{ key: '1', value: entry },
{ key: '0', value: { ...entry, amount: 69 } },
])
})
})