-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathuser-splash-store.ts
105 lines (84 loc) · 2.9 KB
/
user-splash-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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import type { EventEmitter } from 'events';
import type { LogProvider, Logger } from '../logger';
import type {
IUserSplash,
IUserSplashKey,
IUserSplashStore,
} from '../types/stores/user-splash-store';
import type { Db } from './db';
const COLUMNS = ['user_id', 'splash_id', 'seen'];
const TABLE = 'user_splash';
interface IUserSplashTable {
seen?: boolean;
splash_id: string;
user_id: number;
}
const fieldToRow = (fields: IUserSplash): IUserSplashTable => ({
seen: fields.seen,
splash_id: fields.splashId,
user_id: fields.userId,
});
const rowToField = (row: IUserSplashTable): IUserSplash => ({
seen: row.seen || false,
splashId: row.splash_id,
userId: row.user_id,
});
export default class UserSplashStore implements IUserSplashStore {
private db: Db;
private logger: Logger;
constructor(db: Db, eventBus: EventEmitter, getLogger: LogProvider) {
this.db = db;
this.logger = getLogger('user-splash-store.ts');
}
async getAllUserSplashes(userId: number): Promise<IUserSplash[]> {
const userSplash = await this.db
.table<IUserSplashTable>(TABLE)
.select()
.where({ user_id: userId });
return userSplash.map(rowToField);
}
async getSplash(userId: number, splashId: string): Promise<IUserSplash> {
const userSplash = await this.db
.table<IUserSplashTable>(TABLE)
.select()
.where({ user_id: userId, splash_id: splashId })
.first();
return rowToField(userSplash);
}
async updateSplash(splash: IUserSplash): Promise<IUserSplash> {
const insertedSplash = await this.db
.table<IUserSplashTable>(TABLE)
.insert(fieldToRow(splash))
.onConflict(['user_id', 'splash_id'])
.merge()
.returning(COLUMNS);
return rowToField(insertedSplash[0] as IUserSplashTable);
}
async delete({ userId, splashId }: IUserSplashKey): Promise<void> {
await this.db(TABLE)
.where({ user_id: userId, splash_id: splashId })
.del();
}
async deleteAll(): Promise<void> {
await this.db(TABLE).del();
}
destroy(): void {}
async exists({ userId, splashId }: IUserSplashKey): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE user_id = ? AND splash_id = ?) AS present`,
[userId, splashId],
);
const { present } = result.rows[0];
return present;
}
async get({ userId, splashId }: IUserSplashKey): Promise<IUserSplash> {
return this.getSplash(userId, splashId);
}
async getAll(): Promise<IUserSplash[]> {
const userSplashs = await this.db
.table<IUserSplashTable>(TABLE)
.select();
return userSplashs.map(rowToField);
}
}
module.exports = UserSplashStore;