-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBQPlasmaJs.js
More file actions
153 lines (138 loc) · 4.96 KB
/
Copy pathBQPlasmaJs.js
File metadata and controls
153 lines (138 loc) · 4.96 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import PlasmaJsGeneric from './PlasmaJsGeneric';
import {BigQuery} from '@google-cloud/bigquery';
class BQPlasmaJs extends PlasmaJsGeneric{
constructor() {
super();
}
query(query, parameters, callback) {
// For all options, see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query
const options = {
query: query,
// Location must match that of the dataset(s) referenced in the query.
location: this.location,
params: parameters,
};
if(callback === undefined){
return new Promise(async (resolve, reject) => {
// Run the query as a job
this.bigquery.createQueryJob(options).then(([job]) => {
console.log(`Job ${job.id} started.`);
// Wait for the query to finish
job.getQueryResults().then(([rows]) => {
resolve([rows]);
}).catch((err2) => {
console.error(err2);
reject(err2);
});
}).catch((err) => {
console.error(err);
reject(err);
});
});
}else{
this.bigquery.createQueryJob(options, (err, res) => {
if(callback !== undefined){
callback(err, res.getQueryResults());
}
});
}
}
fetch(entity, query, parameters, callback) {
console.log(query);
console.log("parameters ",parameters);
let _this = this;
if(callback === undefined) {
return new Promise((resolve, reject) => {
_this.query(query, parameters).then(([res]) => {
console.log('Rows:');
res.forEach(row => console.log(row));
let objects = [];
if (res !== undefined && res.length > 0) {
res.forEach(function (object, index) {
let tmp = new entity();
tmp.populateObject(object);
objects.push(tmp);
});
}
resolve(objects);
}).catch((err) => {
reject(err);
});
});
}else{
throw new Error("Callbacks have been deprecated please use promises");
}
}
getByUUID(entity, uuid, callback) {
let _this = this;
if(callback === undefined){
return new Promise((resolve, reject) => {
_this.fetch(entity, "SELECT * FROM " + entity.getEntity() + " WHERE uuid = ? LIMIT 1", [uuid]).then((res) => {
console.log("getbyuid res ", res);
let object = undefined;
if (res !== undefined && res.length > 0) {
object = res[0];
}
resolve(object);
}).catch((err) => {
reject(err);
});
});
}else {
throw new Error("Callbacks have been deprecated please use promises");
}
}
fetchPartial(entity, query, parameters, callback) {
if(callback === undefined){
return super.fetchPartial(entity, query, parameters, callback);
}else{
throw new Error("Callbacks have been deprecated please use promises");
}
}
list(entity, callback) {
if(callback === undefined){
return super.list(entity, callback);
}else{
throw new Error("Callbacks have been deprecated please use promises");
}
}
exists(entity, uuid, callback){
let _this = this;
const query = "SELECT uuid FROM " + entity.getEntity() + " WHERE uuid = ? LIMIT 1;";
if(callback === undefined){
return new Promise((resolve, reject) => {
_this.query(query, [uuid], (err, res)=>{
if(res !== undefined && res.rows !== undefined && res.rows.length > 0){
resolve(true);
}else{
resolve(false);
}
});
});
}else{
throw new Error("Callbacks have been deprecated please use promises");
}
}
connect(config) {
configIsset(config, 'location');
this.bigquery = new BigQuery();
this.location = config.location;
PlasmaJsGeneric.setConnection = this;
}
closeConnection(){
this.bigquery = null;
}
static get getConnection(){
return this.connection;
}
static set setConnection(connection){
this.connection = connection;
}
}
export default BQPlasmaJs;
/* PRIVATE */
function configIsset(object, key){
if(object[key] === undefined){
throw new Error("Missing required parameter '" + key + "'");
}
}