2021-04-20 12:32:02 +02:00
|
|
|
import { Knex } from 'knex';
|
|
|
|
import { Logger, LogProvider } from '../logger';
|
2021-08-12 15:04:37 +02:00
|
|
|
import {
|
|
|
|
IContextField,
|
2021-10-14 08:30:36 +02:00
|
|
|
IContextFieldDto,
|
2021-08-12 15:04:37 +02:00
|
|
|
IContextFieldStore,
|
|
|
|
} from '../types/stores/context-field-store';
|
2021-04-20 12:32:02 +02:00
|
|
|
|
2020-02-27 21:26:18 +01:00
|
|
|
const COLUMNS = [
|
|
|
|
'name',
|
|
|
|
'description',
|
2021-02-11 17:59:16 +01:00
|
|
|
'stickiness',
|
2020-02-27 21:26:18 +01:00
|
|
|
'sort_order',
|
|
|
|
'legal_values',
|
|
|
|
'created_at',
|
|
|
|
];
|
|
|
|
const TABLE = 'context_fields';
|
|
|
|
|
2022-04-19 08:40:07 +02:00
|
|
|
const mapRow: (object) => IContextField = (row) => ({
|
2020-02-27 21:26:18 +01:00
|
|
|
name: row.name,
|
|
|
|
description: row.description,
|
2021-02-11 17:59:16 +01:00
|
|
|
stickiness: row.stickiness,
|
2020-02-27 21:26:18 +01:00
|
|
|
sortOrder: row.sort_order,
|
2022-04-19 08:40:07 +02:00
|
|
|
legalValues: row.legal_values || [],
|
2020-02-27 21:26:18 +01:00
|
|
|
createdAt: row.created_at,
|
|
|
|
});
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
interface ICreateContextField {
|
2021-04-20 12:32:02 +02:00
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
stickiness: boolean;
|
|
|
|
sort_order: number;
|
2021-10-14 08:30:36 +02:00
|
|
|
legal_values?: string;
|
2021-04-20 12:32:02 +02:00
|
|
|
updated_at: Date;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
class ContextFieldStore implements IContextFieldStore {
|
2021-04-20 12:32:02 +02:00
|
|
|
private db: Knex;
|
|
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
constructor(db: Knex, getLogger: LogProvider) {
|
2020-02-27 21:26:18 +01:00
|
|
|
this.db = db;
|
2021-08-12 15:04:37 +02:00
|
|
|
this.logger = getLogger('context-field-store.ts');
|
2020-02-27 21:26:18 +01:00
|
|
|
}
|
|
|
|
|
2021-10-14 08:30:36 +02:00
|
|
|
fieldToRow(
|
|
|
|
data: IContextFieldDto,
|
|
|
|
): Omit<ICreateContextField, 'updated_at'> {
|
2020-02-27 21:26:18 +01:00
|
|
|
return {
|
|
|
|
name: data.name,
|
|
|
|
description: data.description,
|
2021-02-11 17:59:16 +01:00
|
|
|
stickiness: data.stickiness,
|
2020-02-27 21:26:18 +01:00
|
|
|
sort_order: data.sortOrder, // eslint-disable-line
|
2022-04-19 08:40:07 +02:00
|
|
|
legal_values: JSON.stringify(data.legalValues || []),
|
2020-02-27 21:26:18 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
async getAll(): Promise<IContextField[]> {
|
2020-09-18 09:05:09 +02:00
|
|
|
const rows = await this.db
|
2020-02-27 21:26:18 +01:00
|
|
|
.select(COLUMNS)
|
|
|
|
.from(TABLE)
|
2020-09-18 09:05:09 +02:00
|
|
|
.orderBy('name', 'asc');
|
|
|
|
|
|
|
|
return rows.map(mapRow);
|
2020-02-27 21:26:18 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async get(key: string): Promise<IContextField> {
|
2020-02-27 21:26:18 +01:00
|
|
|
return this.db
|
|
|
|
.first(COLUMNS)
|
|
|
|
.from(TABLE)
|
2021-08-12 15:04:37 +02:00
|
|
|
.where({ name: key })
|
2020-02-27 21:26:18 +01:00
|
|
|
.then(mapRow);
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async deleteAll(): Promise<void> {
|
|
|
|
await this.db(TABLE).del();
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(): void {}
|
|
|
|
|
|
|
|
async exists(key: string): Promise<boolean> {
|
|
|
|
const result = await this.db.raw(
|
|
|
|
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`,
|
|
|
|
[key],
|
|
|
|
);
|
|
|
|
const { present } = result.rows[0];
|
|
|
|
return present;
|
|
|
|
}
|
|
|
|
|
2021-10-14 08:30:36 +02:00
|
|
|
async create(contextField: IContextFieldDto): Promise<IContextField> {
|
2021-08-12 15:04:37 +02:00
|
|
|
const row = await this.db(TABLE)
|
|
|
|
.insert(this.fieldToRow(contextField))
|
|
|
|
.returning('*');
|
|
|
|
return mapRow(row);
|
2020-02-27 21:26:18 +01:00
|
|
|
}
|
|
|
|
|
2021-10-14 08:30:36 +02:00
|
|
|
async update(data: IContextFieldDto): Promise<IContextField> {
|
|
|
|
const row = await this.db(TABLE)
|
2020-02-27 21:26:18 +01:00
|
|
|
.where({ name: data.name })
|
2021-08-12 15:04:37 +02:00
|
|
|
.update(this.fieldToRow(data))
|
|
|
|
.returning('*');
|
2021-10-14 08:30:36 +02:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
return mapRow(row);
|
2020-02-27 21:26:18 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async delete(name: string): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
return this.db(TABLE).where({ name }).del();
|
2020-02-27 21:26:18 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-20 12:32:02 +02:00
|
|
|
export default ContextFieldStore;
|