This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
99 lines (79 loc) · 2.83 KB
/
index.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
const WebSocket = require('ws');
const schema = require('enigma.js/schemas/12.20.0.json');
const enigma = require('enigma.js');
const host = process.env.ENGINE_HOST || 'localhost';
const port = 19076;
// create a new session:
const session = enigma.create({
schema,
url: `ws://${host}:${port}/app/engineData`,
createSocket(url) {
return new WebSocket(url);
},
});
const postgresqlConnectionSettings = {
qType: 'jdbc', // the name we defined as a parameter to engine in our docker-compose.yml
qName: 'jdbc',
qConnectionString:
'CUSTOM CONNECT TO "provider=jdbc;driver=postgresql;host=postgres-database;port=5432;database=postgres"', // the connection string includes both the provider to use and parameters to it.
qUserName: 'postgres', // username and password for the postgres database, provided to the GRPC-Connector
qPassword: 'postgres',
};
const mysqlConnectionSettings = {
qType: 'jdbc', // the name we defined as a parameter to engine in our docker-compose.yml
qName: 'jdbc',
qConnectionString:
'CUSTOM CONNECT TO "provider=jdbc;driver=mysql;host=mysql-database;port=3306;database=airport"', // the connection string includes both the provider to use and parameters to it.
qUserName: 'root', // username and password for the mysql database, provided to the GRPC-Connector
qPassword: 'mysecretpassword',
};
async function loadData(app, connectionSettings) {
const startTime = Date.now();
const connectionId = await app.createConnection(connectionSettings);
const script = `
lib connect to 'jdbc';
airports:
sql SELECT * FROM airports;
`;
await app.setScript(script);
await app.doReload();
console.log(`Reload took: ${Date.now() - startTime} ms`);
await app.deleteConnection(connectionId);
await app.setScript('');
await app.doSave();
const tableData = await app.getTableData(-1, 100, true, 'airports');
const tableDataAsString = tableData
.map(row => row.qValue
.map(value => value.qText)
.reduce((left, right) => `${left}\t${right}`))
.reduce((row1, row2) => `${row1}\n${row2}`);
console.log(tableDataAsString);
}
async function load() {
const appId = 'reloadapp.qvf';
const global = await session.open();
let app;
try {
const appInfo = await global.createApp(appId);
app = await global.openDoc(appInfo.qAppId);
} catch (e) {
app = await global.openDoc(appId);
}
console.log('Loading from MySQL');
try {
await loadData(app, mysqlConnectionSettings);
} catch (e) {
console.log(`Failed to load data from MySQL with error: ${e}`);
process.exit(1);
}
console.log('Loading from PostgreSQL');
try {
await loadData(app, postgresqlConnectionSettings);
} catch (e) {
console.log(`Failed to load data from PostgreSQL with error: ${e}`);
process.exit(1);
}
await global.deleteApp(appId);
await session.close();
}
load();