• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

get-set-fetch / scraper / 4403974148

pending completion
4403974148

push

github

Andrei Sabau
dependencies update

655 of 852 branches covered (76.88%)

Branch coverage included in aggregate %.

1574 of 1796 relevant lines covered (87.64%)

1266.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.22
/src/storage/ConnectionManager.ts
1
/* eslint-disable @typescript-eslint/no-var-requires */
2
/* eslint-disable max-classes-per-file */
3
import Project from './base/Project';
18✔
4
import Queue from './base/Queue';
18✔
5
import Resource from './base/Resource';
18✔
6
import Connection, { ConnectionConfig } from './base/Connection';
7
import { moduleExists } from '../plugins/file-utils';
18✔
8

9
export type PerModelConfig<T = ConnectionConfig | Connection> = {
10
  [key in 'Project'|'Queue'|'Resource'] : T;
11
}
12

13
export type PerModelConnnection = {
14
  [key in 'Project'|'Queue'|'Resource'] : Connection;
15
}
16

17
export default class ConnectionManager {
18✔
18
  Project: Connection;
19
  Queue: Connection;
20
  Resource: Connection;
21

22
  get modelKeys():string[] {
23
    return [ 'Project', 'Queue', 'Resource' ];
1,450✔
24
  }
25

26
  constructor(config: ConnectionConfig | Connection | PerModelConfig) {
27
    this.init(config);
938✔
28
  }
29

30
  isConnectionConfig(conn): conn is ConnectionConfig {
31
    return conn && Object.prototype.hasOwnProperty.call(conn, 'client');
3,629✔
32
  }
33

34
  isConnection(conn): conn is Connection {
35
    return conn && Object.prototype.hasOwnProperty.call(conn, 'config');
938✔
36
  }
37

38
  isPerModelConfig(conn): conn is PerModelConfig {
39
    return conn && Array.from(Object.keys(conn)).every(propKey => [ 'Project', 'Queue', 'Resource' ].includes(propKey));
2,691✔
40
  }
41

42
  initConnection(config: ConnectionConfig):Connection {
43
    let conn:Connection;
44
    switch (config.client) {
×
45
      case 'sqlite3':
×
46
      case 'mysql':
47
      case 'pg':
48
        // eslint-disable-next-line no-case-declarations, global-require
49
        const KnexConnection = moduleExists('knex') ? require('./knex/KnexConnection').default : null;
×
50
        if (!KnexConnection) throw new Error('knex package not installed');
×
51
        conn = new KnexConnection(config);
×
52
        break;
×
53
      default:
54
        throw new Error(`unsupported storage client ${config.client}`);
×
55
    }
56

57
    return conn;
×
58
  }
59

60
  init(config: ConnectionConfig | Connection | PerModelConfig):void {
61
    let conn: Connection;
62

63
    if (this.isConnectionConfig(config)) {
938!
64
      conn = this.initConnection(config);
×
65
    }
66
    else if (this.isConnection(config)) {
938✔
67
      conn = config;
41✔
68
    }
69

70
    // single connection config
71
    if (conn) {
938✔
72
      this.modelKeys.forEach(modelKey => {
41✔
73
        this[modelKey] = conn;
123✔
74
      });
75
    }
76
    // multiple connection configs, one for Project, Queue, Resource
77
    else if (this.isPerModelConfig(config)) {
897!
78
      this.modelKeys.forEach(modelKey => {
897✔
79
        this[modelKey] = this.isConnectionConfig(config[modelKey]) ? this.initConnection(config[modelKey]) : config[modelKey];
2,691!
80
      });
81
    }
82
    // invalid input
83
    else {
84
      throw new Error('invalid connect configuration(s)');
×
85
    }
86
  }
87

88
  async connect():Promise<void> {
89
    await Promise.all([ this.Project.open(), this.Queue.open(), this.Resource.open() ]);
515✔
90
  }
91

92
  async close():Promise<void> {
93
    await Promise.all(this.modelKeys.map(modelKey => this[modelKey].close()));
1,536✔
94
  }
95

96
  /**
97
   * Based on storage options, get custom Project class linked to custom Queue, Resource classes.
98
   */
99
  async getProject():Promise<typeof Project> {
100
    // create a unique combination of models
101
    const ExtProject:typeof Project = class extends Project {};
96✔
102
    ExtProject.storage = this.Project.getProjectStorage();
96✔
103

104
    const ExtQueue:typeof Queue = class extends Queue {};
96✔
105
    ExtQueue.storage = this.Queue.getQueueStorage();
96✔
106

107
    const ExtResource:typeof Resource = class extends Resource {};
96✔
108
    ExtResource.storage = this.Project.getResourceStorage();
96✔
109

110
    // link models
111
    ExtProject.ExtQueue = ExtQueue;
96✔
112
    ExtProject.ExtResource = ExtResource;
96✔
113
    ExtQueue.ExtResource = ExtResource;
96✔
114

115
    // init Project storage
116
    await ExtProject.storage.init();
96✔
117

118
    return ExtProject;
96✔
119
  }
120

121
  static clone(project:Project):ConnectionManager {
122
    const { config: projConnConfig, constructor: ProjConnConstructor } = project.Constructor.storage.conn;
486✔
123
    const projConn:Connection = new (<{new(config:ConnectionConfig):Connection}>ProjConnConstructor)(projConnConfig);
486✔
124

125
    const { config: queueConnConfig, constructor: QueueConnConstructor } = project.Constructor.ExtQueue.storage.conn;
486✔
126
    const queueConn:Connection = new (<{new(config:ConnectionConfig):Connection}>QueueConnConstructor)(queueConnConfig);
486✔
127

128
    const { config: resourceConnConfig, constructor: ResourceConnConstructor } = project.Constructor.ExtResource.storage.conn;
486✔
129
    const resourceConn:Connection = new (<{new(config:ConnectionConfig):Connection}>ResourceConnConstructor)(resourceConnConfig);
486✔
130

131
    return new ConnectionManager({
486✔
132
      Project: projConn,
133
      Queue: queueConn,
134
      Resource: resourceConn,
135
    });
136
  }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc