File size: 1,917 Bytes
5e518ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { ConfigService, Database } from '@config/env.config';
import { ROOT_DIR } from '@config/path.config';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import { join } from 'path';

export type IInsert = { insertCount: number };

export interface IRepository {
  insert(data: any, instanceName: string, saveDb?: boolean): Promise<IInsert>;
  update(data: any, instanceName: string, saveDb?: boolean): Promise<IInsert>;
  find(query: any): Promise<any>;
  delete(query: any, force?: boolean): Promise<any>;

  dbSettings: Database;
  readonly storePath: string;
}

type WriteStore<U> = {
  path: string;
  fileName: string;
  data: U;
};

export abstract class Repository implements IRepository {
  constructor(configService: ConfigService) {
    this.dbSettings = configService.get<Database>('DATABASE');
  }

  dbSettings: Database;
  readonly storePath = join(ROOT_DIR, 'store');

  public writeStore = <T = any>(create: WriteStore<T>) => {
    if (!existsSync(create.path)) {
      mkdirSync(create.path, { recursive: true });
    }
    try {
      writeFileSync(join(create.path, create.fileName + '.json'), JSON.stringify({ ...create.data }), {
        encoding: 'utf-8',
      });

      return { message: 'create - success' };
    } finally {
      create.data = undefined;
    }
  };

  // eslint-disable-next-line
    public insert(data: any, instanceName: string, saveDb = false): Promise<IInsert> {
    throw new Error('Method not implemented.');
  }

  // eslint-disable-next-line
    public update(data: any, instanceName: string, saveDb = false): Promise<IInsert> {
    throw new Error('Method not implemented.');
  }

  // eslint-disable-next-line
    public find(query: any): Promise<any> {
    throw new Error('Method not implemented.');
  }

  // eslint-disable-next-line
    delete(query: any, force?: boolean): Promise<any> {
    throw new Error('Method not implemented.');
  }
}