write WIP of logging: works using the old logging logic

This commit is contained in:
ShootingStar91
2024-08-25 18:50:33 +03:00
parent d58512e540
commit 418e0a3256
3 changed files with 25 additions and 7 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ export const writeLocality = async (locality: EditDataType<LocalityDetailsType>)
await writeHandler.upsertList('now_ss', locality.now_ss, ['lid', 'sed_struct'])
await writeHandler.upsertList('now_coll_meth', locality.now_coll_meth, ['lid', 'coll_meth'])
await writeHandler.upsertList('now_syn_loc', locality.now_syn_loc, ['lid', 'syn_id'])
await writeHandler.logUpdates()
await writeHandler.end()
return locality.lid
}
+1 -1
View File
@@ -120,7 +120,7 @@ export const logAllUpdates = async (
await writeUpdateEntries(updateEntries, writeContext.connection, userInitials, comment)
await writeReferences(updateEntries, writeContext.connection, references)
console.log({ writeList })
// The write has value and oldValue swapped for 'delete' type logRows due to reasons.
// It is simpler to fix it here than changing how write works, because values are used to find id's above.
const fixDeleteRows = (logRow: LogRow) => {
@@ -1,5 +1,6 @@
import { RowState } from '../../../../frontend/src/backendTypes'
import { AllowedTables, DbValue, PrimaryTables, WriteItem } from '../write/writeUtils'
import { logger } from '../../utils/logger'
import { AllowedTables, DbValue, Item, logAllUpdates, PrimaryTables, WriteItem } from '../write/writeUtils'
import { DatabaseHandler, DbWriteItem } from './databaseHandler'
import { fixBoolean, getItemList, valueIsDifferent } from './utils'
@@ -35,20 +36,22 @@ export class WriteHandler extends DatabaseHandler {
ids: Column & value pairs that define which item to write to. Empty if new item. */
async updateRow<T extends Record<string, DbValue>>(table: AllowedTables, items: DbWriteItem[], ids: DbWriteItem[]) {
const oldObject = await super.select<T>(table, [], ids)
const fieldsToWrite: DbWriteItem[] = []
const fieldsToWrite: Item[] = []
// TODO put oldValue here too for logging
for (const { column, value } of items) {
const oldValue = oldObject[column as keyof T]
const fixedValue = fixBoolean(value)
if (!valueIsDifferent(fixedValue, oldValue)) continue
fieldsToWrite.push({ column, value: fixedValue })
console.log({ value: fixedValue, oldValue, column })
fieldsToWrite.push({ column, value: fixedValue, oldValue, table })
}
if (items.find(item => !this.allowedColumns.includes(item.column))) {
throw new Error('Unallowed column in SQL')
}
this.writeList.push({ table, type: 'update', items: items.map(item => ({ ...item, table })) })
this.writeList.push({ table, type: 'update', items: fieldsToWrite.map(item => ({ ...item, table })) })
if (fieldsToWrite.length === 0) return
return await super.update(table, fieldsToWrite, ids)
}
@@ -88,4 +91,19 @@ export class WriteHandler extends DatabaseHandler {
}
}
}
async logUpdates() {
if (this.writeList.length === 0) {
logger.info(`No changes detected, skipping logging.`)
return
}
await logAllUpdates(
{ connection: this.connection!, username: 'ArK', writeList: this.writeList, type: 'add' },
this.table,
'ArK',
'comment',
this.idValue!,
[]
)
}
}