File size: 2,000 Bytes
4327358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Knex } from 'knex';

import { App } from '../dto/app.dto';
import { AppDB } from './types';

export class AppRepository {
  static tableName = 'apps';

  constructor(private readonly knex: Knex) {}

  get tableName() {
    return AppRepository.tableName;
  }

  /**
   * Saves an app to the database
   */
  async save(app: Omit<App, 'pk'>): Promise<AppDB> {
    const appToSave = { ...app };
    if (appToSave.config && typeof appToSave.config === 'object') {
      appToSave.config = JSON.stringify(appToSave.config);
    }
    const [pk] = await this.knex(this.tableName)
      .insert(appToSave)
      .returning('pk');
    return { ...app, pk: pk };
  }

  private deserialize(app: AppDB): AppDB {
    const parsedConfig =
      typeof app.config === 'string' ? JSON.parse(app.config) : app.config;
    return { ...app, config: parsedConfig };
  }

  private serialize<T extends Partial<App>>(app: T): T {
    const appCopy = { ...app };
    if (appCopy.config && typeof appCopy.config === 'object') {
      appCopy.config = JSON.stringify(appCopy.config);
    }
    return appCopy;
  }

  /**
   * Gets an app by its id
   */
  async getById(id: string): Promise<AppDB | null> {
    const app = await this.knex(this.tableName).where('id', id).first();
    if (!app) {
      return null;
    }
    return this.deserialize(app);
  }

  /**
   * Gets all apps
   */
  async getAllBySession(session: string): Promise<AppDB[]> {
    return this.knex(this.tableName)
      .select('*')
      .where('session', session)
      .orderBy('id', 'asc')
      .then((apps) => apps.map((app) => this.deserialize(app)));
  }

  /**
   * Updates an app
   */
  async update(id: string, app: Partial<Omit<App, 'id'>>): Promise<void> {
    const appToUpdate = this.serialize(app);
    await this.knex(this.tableName).where('id', id).update(appToUpdate);
  }

  /**
   * Deletes an app
   */
  async delete(id: string): Promise<void> {
    await this.knex(this.tableName).where('id', id).delete();
  }
}