-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathfeature-type-store.ts
88 lines (73 loc) · 2.34 KB
/
feature-type-store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import type { Logger, LogProvider } from '../logger';
import type {
IFeatureType,
IFeatureTypeStore,
} from '../types/stores/feature-type-store';
import type { Db } from './db';
const COLUMNS = ['id', 'name', 'description', 'lifetime_days'];
const TABLE = 'feature_types';
interface IFeatureTypeRow {
id: string;
name: string;
description: string;
lifetime_days: number;
}
class FeatureTypeStore implements IFeatureTypeStore {
private db: Db;
private logger: Logger;
constructor(db: Db, getLogger: LogProvider) {
this.db = db;
this.logger = getLogger('feature-type-store.ts');
}
async getAll(): Promise<IFeatureType[]> {
const rows = await this.db.select(COLUMNS).from(TABLE);
return rows.map(this.rowToFeatureType);
}
private rowToFeatureType(row: IFeatureTypeRow): IFeatureType {
return {
id: row.id,
name: row.name,
description: row.description,
lifetimeDays: row.lifetime_days,
};
}
async get(id: string): Promise<IFeatureType> {
const row = await this.db(TABLE).where({ id }).first();
return row ? this.rowToFeatureType(row) : row;
}
async getByName(name: string): Promise<IFeatureType> {
const row = await this.db(TABLE).where({ name }).first();
return this.rowToFeatureType(row);
}
async delete(key: string): Promise<void> {
await this.db(TABLE).where({ id: key }).del();
}
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 id = ?) AS present`,
[key],
);
const { present } = result.rows[0];
return present;
}
async updateLifetime(
id: string,
newLifetimeDays: number | null,
): Promise<IFeatureType | undefined> {
const [updatedType] = await this.db(TABLE)
.update({ lifetime_days: newLifetimeDays })
.where({ id })
.returning(['*']);
if (updatedType) {
return this.rowToFeatureType(updatedType);
} else {
return undefined;
}
}
}
export default FeatureTypeStore;
module.exports = FeatureTypeStore;