1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-11-10 01:19:53 +01:00
unleash.unleash/src/lib/db/setting-store.ts
Christopher Kolstad 8de801025f
task: make setting service handle conflict on insert (#9160)
Currently, in enterprise we're struggling with setting service and
transactionality; all our verfications says that the setting key is not
present, so setting-store happily tries to insert a new row with a new
PK. However, somehow at the same time, the key already exists. This
commit adds conflict handling to the insertNewRow.
2025-02-13 15:54:43 +01:00

82 lines
2.1 KiB
TypeScript

import type { Logger, LogProvider } from '../logger';
import type { ISettingStore } from '../types/stores/settings-store';
import type { Db } from './db';
const TABLE = 'settings';
export default class SettingStore implements ISettingStore {
private db: Db;
private logger: Logger;
constructor(db: Db, getLogger: LogProvider) {
this.db = db;
this.logger = getLogger('settings-store.ts');
}
async postgresVersion(): Promise<string> {
try {
const showResult = await this.db.raw('SHOW server_version');
return showResult?.rows[0]?.server_version || '';
} catch (e) {
this.logger.warn('Failed to fetch postgres version', e);
return '';
}
}
async updateRow(name: string, content: any): Promise<void> {
return this.db(TABLE)
.where('name', name)
.update({
content: JSON.stringify(content),
});
}
async exists(name: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`,
[name],
);
const { present } = result.rows[0];
return present;
}
async get<T>(name: string): Promise<T | undefined> {
const result = await this.db
.select()
.from(TABLE)
.where('name', name)
.limit(1);
if (result.length > 0) {
return result[0].content;
}
return undefined;
}
// Is actually an upsert
async insert(name: string, content: any): Promise<void> {
await this.db(TABLE)
.insert({ name, content })
.onConflict('name')
.merge();
}
async delete(name: string): Promise<void> {
await this.db(TABLE).where({ name }).del();
}
async deleteAll(): Promise<void> {
await this.db(TABLE).del();
}
destroy(): void {}
async getAll(): Promise<any[]> {
const rows = await this.db(TABLE).select();
return rows.map((r) => r.content);
}
}
module.exports = SettingStore;