-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
300 lines (234 loc) · 8.39 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
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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const Excel = require('exceljs');
const { Pool } = require('pg');
const Cursor = require('pg-cursor');
const QueryStream = require('pg-query-stream');
const PORT = 3001;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const pool = new Pool({
user: 'codebaseuser',
host: '127.0.0.1',
database: 'mydb',
password: '12345678',
port: 5432,
});
// TODO
async function* getAccountsGenerator (client, done) {
// A cursor can be used to efficiently read through large result sets without loading the entire result-set into memory ahead of time.
// https://node-postgres.com/apis/cursor
const stream = client.query(new QueryStream('SELECT * FROM ACCOUNTS LIMIT 500000'));
stream.on('data', (chunk) => {
// yield chunk;
});
stream.on('end', async () => {
console.log('stream.on end');
await done();
});
}
app.get('/', (req, res) => {
return res.status(200).send({'message': 'server up and running'});
});
app.get('/query-time', async (req, res) => {
const client = await pool.connect();
try {
const queryRes = await client.query('SELECT NOW()');
return res.status(200).send({'message': queryRes});
} catch (e) {
return res.status(500).send({'message': e});
} finally {
client.release();
}
});
app.get('/download', async (req, res) => {
const client = await pool.connect();
// set batch size to 1
const batchSize = 1;
try {
// additional headers
res.status(200);
res.setHeader('Content-Disposition', 'attachment; filename=db_dump.xlsx');
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
const workbook = new Excel.stream.xlsx.WorkbookWriter({
stream: res
});
let sheet = workbook.addWorksheet('db_dump.xlsx');
sheet.columns = [
{header: 'Id', key: 'id', width: 10},
{header: 'Name', key: 'name', width: 15},
{header: 'Email', key: 'email', width: 15},
{header: 'Created At', key: 'created_at', width: 15},
]
// download 500000 datas
// estimated size is more than 10MB
// A cursor can be used to efficiently read through large result sets without loading the entire result-set into memory ahead of time.
// https://node-postgres.com/apis/cursor
const cursor = client.query(new Cursor('SELECT * FROM ACCOUNTS LIMIT 50000'));
req.on('close', async () => {
console.log('here close');
cursor.close().then(() => {
console.log('cursor closed');
console.log('finally on close');
client.release();
});
});
req.on('end', async () => {
console.log('here end');
// await cursor.close();
// console.log('cursor closed');
// console.log('finally on close');
// client.release();
});
req.on('error', async () => {
console.log('here error');
// await cursor.close();
// console.log('cursor closed');
// console.log('finally on close');
// client.release();
});
let rows = await cursor.read(batchSize);
// add initial rows
if (rows.length > 0) {
console.log(rows[0]);
sheet.addRow(rows[0]).commit();
}
while(rows.length) {
rows = await cursor.read(batchSize);
// add remaining rows
if (rows.length > 0) {
sheet.addRow(rows[0]).commit();
}
}
// commit sheet
sheet.commit();
// commit workbook
await workbook.commit();
await cursor.close();
console.log('cursor closed');
// Calling the writable.end() method signals that no more data will be written to the Writable
res.end();
} catch (e) {
console.log(e);
res.status(500);
res.end();
}
// } finally {
// console.log('finally');
// client.release();
// }
});
app.get('/download-stream', async (req, res) => {
const client = await pool.connect();
try {
// additional headers
res.status(200);
res.setHeader('Content-Disposition', 'attachment; filename=db_dump.xlsx');
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
const workbook = new Excel.stream.xlsx.WorkbookWriter({
stream: res
});
let sheet = workbook.addWorksheet('db_dump.xlsx');
sheet.columns = [
{header: 'Id', key: 'id', width: 10},
{header: 'Name', key: 'name', width: 15},
{header: 'Email', key: 'email', width: 15},
{header: 'Created At', key: 'created_at', width: 15},
]
// download 500000 datas
// estimated size is more than 10MB
// https://github.com/brianc/node-postgres/tree/master/packages/pg-query-stream
const stream = client.query(new QueryStream('SELECT * FROM ACCOUNTS LIMIT 500000'));
req.on('close', async () => {
console.log('here close');
console.log('finally on close');
stream.destroy();
console.log('stream.destroy()');
client.release();
console.log('client.release()');
console.log();
});
req.on('end', async () => {
console.log('here end');
});
req.on('error', async () => {
console.log('here error');
});
stream.on('data', (chunk) => {
sheet.addRow(chunk).commit();
});
stream.on('end', async () => {
console.log('stream.on end');
// commit sheet
sheet.commit();
// commit workbook
await workbook.commit();
console.log('workbook.commit()');
// Calling the writable.end() method signals that no more data will be written to the Writable
res.end();
});
stream.on('error', (e) => {
console.log('stream.on error ', e);
});
} catch (e) {
console.log(e);
res.status(500);
res.end();
}
});
app.get('/download-generator', async (req, res) => {
const client = await pool.connect();
try {
// additional headers
res.status(200);
res.setHeader('Content-Disposition', 'attachment; filename=db_dump.xlsx');
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
const workbook = new Excel.stream.xlsx.WorkbookWriter({
stream: res
});
let sheet = workbook.addWorksheet('db_dump.xlsx');
sheet.columns = [
{header: 'Id', key: 'id', width: 10},
{header: 'Name', key: 'name', width: 15},
{header: 'Email', key: 'email', width: 15},
{header: 'Created At', key: 'created_at', width: 15},
]
req.on('close', async () => {
console.log('here close');
console.log('finally on close');
client.release();
});
req.on('end', async () => {
console.log('here end');
});
req.on('error', async () => {
console.log('here error');
});
const accountGen = getAccountsGenerator(client, async () => {
// commit sheet
sheet.commit();
// commit workbook
await workbook.commit();
console.log('workbook.commit()');
// Calling the writable.end() method signals that no more data will be written to the Writable
res.end();
});
for await (const account of accountGen) {
sheet.addRow(account).commit();
}
} catch (e) {
console.log(e);
client.release();
res.status(500);
res.end();
}
});
const srv = app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
process.on('SIGINT', async () => {
console.log('on close');
await pool.end();
srv.close(() => {
console.log('closing http server');
});
});