-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconnectionHelper.js
More file actions
48 lines (41 loc) · 1.17 KB
/
connectionHelper.js
File metadata and controls
48 lines (41 loc) · 1.17 KB
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
const { BigQuery, credentials } = require('@google-cloud/bigquery');
const fsExtra = require('fs-extra');
const path = require('path');
let client = null;
function getAuthInfo({ authType, connectionInfo }) {
if (authType === 'app_default_credentials') {
// In this authType case projectId is mandatory
return {
projectId: connectionInfo.projectId,
credentialsFilePath: connectionInfo.credentialsFilePath,
};
}
// service account case and backward compatible value
return {
projectId: connectionInfo.projectId,
credentialsFilePath: connectionInfo.keyFilename,
};
}
const connect = async connectionInfo => {
if (client) {
return client;
}
const authType = connectionInfo.authType;
const { projectId, credentialsFilePath } = getAuthInfo({ authType, connectionInfo });
const location = connectionInfo.location;
const credentials = await fsExtra.readJson(path.resolve(credentialsFilePath));
client = new BigQuery({
credentials,
location,
projectId,
scopes: ['https://www.googleapis.com/auth/bigquery', 'https://www.googleapis.com/auth/drive'],
});
return client;
};
const disconnect = () => {
client = null;
};
module.exports = {
connect,
disconnect,
};