forked from vsuaste/server-graphql-sequelize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.js
315 lines (300 loc) · 9.56 KB
/
connection.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
const { Sequelize } = require("sequelize");
const storageConfig = require("./config/data_models_storage_config.json");
const { MongoClient } = require("mongodb");
const {
S3,
waitUntilBucketExists
} = require("@aws-sdk/client-s3");
const presto = require("presto-client");
const cassandraDriver = require("cassandra-driver");
const neo4j = require("neo4j-driver");
const { queryData } = require("./utils/presto_helper");
const os = require("os");
const Op = Sequelize.Op;
storageConfig.operatorsAliases = {
$eq: Op.eq,
$and: Op.and,
$or: Op.or,
$like: Op.like,
$notLike: Op.notLike,
$between: Op.between,
$notBetween: Op.notBetween,
$in: Op.in,
$notIn: Op.notIn,
$gt: Op.gt,
$gte: Op.gte,
$lt: Op.lt,
$lte: Op.lte,
$ne: Op.ne,
$regexp: Op.regexp,
$notRegexp: Op.notRegexp,
};
/**
* setup the cassandraClient
*/
function setupCassandraDriver() {
let cassandraConfig = storageConfig["default-cassandra"];
// set up logging of requests
let requestTracker = new cassandraDriver.tracker.RequestLogger({
slowThreshold: 1000,
logNormalRequests: true,
logErroredRequests: true,
});
requestTracker.emitter.on("normal", (message) => console.log(message));
requestTracker.emitter.on("slow", (message) => console.log(message));
requestTracker.emitter.on("failure", (message) => console.log(message));
requestTracker.emitter.on("large", (message) => console.log(message));
// set up new cassandra client as configurated in 'default-cassandra'
return new cassandraDriver.Client({
contactPoints: [cassandraConfig.host],
localDataCenter: "datacenter1",
keyspace: cassandraConfig.keyspace,
protocolOptions: {
port: cassandraConfig.port,
},
requestTracker: requestTracker,
});
}
// const cassandraClient = setupCassandraDriver();
/**
* setup the MongoClient
*/
async function setupMongoDriver() {
let config = storageConfig["default-mongodb"];
const uri = `mongodb://${config.username}:${config.password}@${config.host}:${config.port}/${config.database}`;
console.log(uri);
const client = new MongoClient(uri, { useUnifiedTopology: true });
const db = await connectMongoDb(client);
return db;
}
async function connectMongoDb(client) {
try {
await client.connect();
let config = storageConfig["default-mongodb"];
console.log(`Connecting to MongoDB: ${config.database}`);
const db = await client.db(config.database);
console.log(`Connected successfully to MongoDB: ${config.database}`);
return db;
} catch (e) {
console.log("MongoDB connection error: " + e);
}
}
const setupAmazonS3 = async () => {
let config = storageConfig["default-amazonS3"];
const s3 = new S3({
region: "eu-central-1",
credentials: {
accessKeyId: config.username,
secretAccessKey: config.password
},
endpoint: `http://${config.host}:${config.port}`,
forcePathStyle: true,
signatureVersion: "v4"
});
let res = await s3.listBuckets({});
let buckets = res.Buckets.map((bucketObj) => bucketObj.Name);
if (buckets.includes(config.bucket)) {
console.log(`bucket ${config.bucket} in Minio exists`);
} else {
console.log(`create a new bucket in Minio: ${config.bucket}`);
res = await s3.createBucket({ Bucket: config.bucket });
}
return s3;
};
/**
* Stored connection instances.
*/
const addConnectionInstances = async () => {
let connectionInstances = new Map();
const user = os.userInfo().username;
for (let key of Object.keys(storageConfig)) {
let storageType = storageConfig[key].storageType;
if (
storageConfig.hasOwnProperty(key) &&
key !== "operatorsAliases" &&
storageType === "sql"
) {
const config =
storageConfig[key].dialect == "sqlite"
? {
dialect: storageConfig[key].dialect,
storage: __dirname + "/" + storageConfig[key].storage,
}
: storageConfig[key];
const connection = new Sequelize(config);
connectionInstances.set(key, {
storageType,
connection: connection,
});
} else if (
storageConfig.hasOwnProperty(key) &&
key !== "operatorsAliases" &&
storageType === "mongodb"
) {
connectionInstances.set(key, {
storageType,
connection: await setupMongoDriver(),
});
} else if (
storageConfig.hasOwnProperty(key) &&
key !== "operatorsAliases" &&
storageType === "cassandra"
) {
connectionInstances.set(key, {
storageType,
connection: setupCassandraDriver(),
});
} else if (
storageConfig.hasOwnProperty(key) &&
key !== "operatorsAliases" &&
storageType === "amazon-s3"
) {
connectionInstances.set(key, {
storageType,
connection: await setupAmazonS3(),
});
} else if (
storageConfig.hasOwnProperty(key) &&
key !== "operatorsAliases" &&
storageType === "trino"
) {
connectionInstances.set(key, {
storageType,
connection: new presto.Client({
user: `"${user}"`,
host: storageConfig[key].trino_host,
port: storageConfig[key].trino_port,
catalog: storageConfig[key].catalog,
schema: storageConfig[key].schema,
source: "nodejs-client",
engine: "trino",
}),
});
} else if (
storageConfig.hasOwnProperty(key) &&
key !== "operatorsAliases" &&
storageType === "presto"
) {
connectionInstances.set(key, {
storageType,
connection: new presto.Client({
user: `"${user}"`,
host: storageConfig[key].presto_host,
port: storageConfig[key].presto_port,
catalog: storageConfig[key].catalog,
schema: storageConfig[key].schema,
source: "nodejs-client",
engine: "presto",
}),
});
} else if (
storageConfig.hasOwnProperty(key) &&
key !== "operatorsAliases" &&
storageType === "neo4j"
) {
// note: https://stackoverflow.com/a/62816512/8132085
connectionInstances.set(key, {
storageType,
connection: neo4j.driver(
`bolt://${storageConfig[key].host}:${storageConfig[key].port}`,
neo4j.auth.basic(
storageConfig[key].username,
storageConfig[key].password
),
{ disableLosslessIntegers: true }
),
});
}
}
return connectionInstances;
};
/**
* Async verification of all connections imported from
* data_models_storage_config.json.
*/
exports.checkConnections = async () => {
const checks = [];
const connectionInstances = await addConnectionInstances();
// console.log(connectionInstances)
for (let { 0: key, 1: instance } of connectionInstances.entries()) {
try {
if (instance.storageType === "sql") {
await instance.connection.authenticate();
} else if (instance.storageType === "mongodb") {
await instance.connection.command({ ping: 1 });
} else if (instance.storageType === "cassandra") {
await instance.connection.connect();
} else if (instance.storageType === "amazon-s3") {
await waitUntilBucketExists(
{ client: instance.connection, maxWaitTime: 60},
{ Bucket: storageConfig["default-amazonS3"].bucket }
);
} else if (instance.storageType === "trino") {
try {
const trino_conf = storageConfig["default-trino"];
await queryData(
`SHOW TABLES FROM ${trino_conf.catalog}.${trino_conf.schema}`,
instance.connection
);
} catch (error) {
throw error;
}
} else if (instance.storageType === "presto") {
try {
const presto_conf = storageConfig["default-presto"];
await queryData(
`SHOW TABLES FROM ${presto_conf.catalog}.${presto_conf.schema}`,
instance.connection
);
} catch (error) {
throw error;
}
} else if (instance.storageType === "neo4j") {
await instance.connection.verifyConnectivity({
database: storageConfig["default-neo4j"].database,
});
}
checks.push({ key, valid: true });
} catch (exception) {
console.error(exception);
checks.push({ key, valid: false });
}
}
return checks;
};
/**
* Get an existing sequelize instance using the specified database.
* @param {string} key connection key as defined in the model config
* @returns A configured connection instance
*/
exports.getConnectionInstances = async () => {
const connectionInstances = await addConnectionInstances();
return connectionInstances;
};
exports.ConnectionError = class ConnectionError extends Error {
/**
* Create a new instance of a data model connection error.
* @param {object} modelDefinition model definition as a JSON object
*/
constructor({ database, model, storageType }) {
const message =
`Model "${model}" connection to its database failed. Verify that ` +
`database "${database}" and storageType "${storageType}" are ` +
`correctly set and match the config in "data_models_storage_config.json"`;
super(message);
}
};
/**
* Add connection as a static property of the data model class.
* @param {object} modelClass the data model class
* @param {object} connection the connection
* @returns The data model class with connection as a static property.
*/
exports.getAndConnectDataModelClass = (modelClass, connection) => {
return Object.defineProperty(modelClass, "storageHandler", {
value: connection,
writable: false, // cannot be changed in the future
enumerable: true,
configurable: false,
});
};