-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.ts
423 lines (336 loc) · 11.2 KB
/
test.ts
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import * as fs from 'fs';
import * as mysql from 'mysql';
import * as stream from 'stream';
// Connections
let connection = mysql.createConnection({
host: 'localhost',
user: 'me',
password: 'secret'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (err: mysql.QueryError, rows: mysql.RowDataPacket[], fields: mysql.FieldPacket) {
if (err) {
throw err;
}
console.log('The solution is: ', rows[0]['solution']);
});
// multipleStatements
connection.query('SELECT 1 AS x; SELECT 1 AS x; SELECT 1 AS x', function (err: mysql.QueryError, rows: mysql.RowDataPacket[][], fields: mysql.FieldPacket) {
if (err) {
throw err;
}
console.log(rows[0][0]['x']);
console.log(rows[1][0]['x']);
console.log(rows[2][0]['x']);
});
connection.end();
connection = mysql.createConnection({
host: 'example.org',
user: 'bob',
password: 'secret'
});
connection.connect(function (err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
connection.query('SELECT 1', function (err, rows) {
// connected! (unless `err` is set)
});
connection = mysql.createConnection({
host: 'localhost',
ssl: {
ca: ''
}
});
connection = mysql.createConnection({
host: 'localhost',
ssl: {
// DO NOT DO THIS
// set up your ca correctly to trust the connection
rejectUnauthorized: false
}
});
connection.end(function (err) {
// The connection is terminated now
});
connection.destroy();
connection.changeUser({ user: 'john' }, function (err) {
if (err) {
throw err;
}
});
let userId = 'some user provided value';
let sql = 'SELECT * FROM users WHERE id = ' + connection.escape(userId);
connection.query(sql, function (err, results) {
// ...
});
connection.query('SELECT * FROM users WHERE id = ?', [userId], function (err, results) {
// ...
});
let post = { id: 1, title: 'Hello MySQL' };
let query = connection.query('INSERT INTO posts SET ?', post, function (err, result) {
// Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
let queryStr = 'SELECT * FROM posts WHERE title=' + mysql.escape('Hello MySQL');
console.log(queryStr); // SELECT * FROM posts WHERE title='Hello MySQL'
let sorter = 'date';
sql = 'SELECT * FROM posts ORDER BY ' + connection.escapeId(sorter);
connection.query(sql, function (err, results) {
// ...
});
sql = 'SELECT * FROM posts ORDER BY ' + connection.escapeId('posts.' + sorter);
connection.query(sql, function (err, results) {
// ...
});
let userIdNum = 1;
let columns = ['username', 'email'];
query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userIdNum], function (err, results) {
// ...
});
console.log(query.sql); // SELECT `username`, `email` FROM `users` WHERE id = 1
sql = 'SELECT * FROM ?? WHERE ?? = ?';
let inserts = ['users', 'id', userId];
sql = mysql.format(sql, inserts);
sql = 'INSERT INTO posts SET ?';
post = { id: 1, title: 'Hello MySQL' };
sql = mysql.format(sql, post);
connection.config.queryFormat = function (query, values) {
if (!values) {
return query;
}
return query.replace(/\:(\w+)/g, function (txt: string, key: string) {
if (values.hasOwnProperty(key)) {
return this.escape(values[key]);
}
return txt;
}.bind(this));
};
connection.query('UPDATE posts SET title = :title', { title: 'Hello MySQL' });
let s: stream.Readable = connection.query('UPDATE posts SET title = :title', { title: 'Hello MySQL' }).stream({ highWaterMark: 5 });
connection.query('INSERT INTO posts SET ?', { title: 'test' }, function (err: mysql.QueryError, result: mysql.OkPacket) {
console.log(result.insertId);
});
connection.query('DELETE FROM posts WHERE title = "wrong"', function (err: mysql.QueryError, result: mysql.OkPacket) {
console.log('deleted ' + result.affectedRows + ' rows');
});
connection.query('UPDATE posts SET ...', function (err: mysql.QueryError, result: mysql.OkPacket) {
console.log('changed ' + result.changedRows + ' rows');
});
connection.connect(function (err) {
console.log('connected as id ' + connection.threadId);
});
/// Pools
let poolConfig = {
connectionLimit: 10,
host: 'example.org',
user: 'bob',
password: 'secret'
};
let pool = mysql.createPool(poolConfig);
pool.query('SELECT 1 + 1 AS solution', function (err: mysql.QueryError, rows: mysql.RowDataPacket[], fields: mysql.FieldPacket) {
if (err) {
throw err;
}
console.log('The solution is: ', rows[0]['solution']);
});
pool = mysql.createPool({
host: 'example.org',
user: 'bob',
password: 'secret'
});
pool.getConnection(function (err, connection) {
// connected! (unless `err` is set)
});
pool.on('connection', function (connection) {
connection.query('SET SESSION auto_increment_increment=1');
});
pool.getConnection(function (err, connection) {
// Use the connection
connection.query('SELECT something FROM sometable', function (err, rows) {
// And done with the connection.
connection.release();
// Don't use the connection here, it has been returned to the pool.
});
});
/// PoolClusters
// create
let poolCluster = mysql.createPoolCluster();
let poolClusterConfig = {
canRetry: true,
removeNodeErrorCount: 2,
restoreNodeTimeout: 3,
defaultSelector: 'RANDOM'
};
poolCluster.add(poolClusterConfig); // anonymous group
poolCluster.add('MASTER', poolClusterConfig);
poolCluster.add('SLAVE1', poolClusterConfig);
poolCluster.add('SLAVE2', poolClusterConfig);
// Target Group : ALL(anonymous, MASTER, SLAVE1-2), Selector : round-robin(default)
poolCluster.getConnection(function (err, connection) { });
// Target Group : MASTER, Selector : round-robin
poolCluster.getConnection('MASTER', function (err, connection) { });
// Target Group : SLAVE1-2, Selector : order
// If can't connect to SLAVE1, return SLAVE2. (remove SLAVE1 in the cluster)
poolCluster.on('remove', function (nodeId) {
console.log('REMOVED NODE : ' + nodeId); // nodeId = SLAVE1
});
poolCluster.getConnection('SLAVE*', 'ORDER', function (err, connection) { });
// of namespace : of(pattern, selector)
poolCluster.of('*').getConnection(function (err, connection) { });
const cluster = poolCluster.of('SLAVE*', 'RANDOM');
pool.getConnection(function (err, connection) { });
pool.getConnection(function (err, connection) { });
let poolClusterWithOptions = mysql.createPoolCluster({
canRetry: true,
removeNodeErrorCount: 3,
restoreNodeTimeout: 1000,
defaultSelector: 'RR'
});
// destroy
poolCluster.end();
// Queries
query = connection.query('SELECT * FROM posts');
query
.on('error', function (err: NodeJS.ErrnoException) {
// Handle error, an 'end' event will be emitted after this as well
})
.on('fields', function (fields: any) {
// the field packets for the rows to follow
})
.on('result', function (row: any) {
// Pausing the connnection is useful if your processing involves I/O
connection.pause();
let processRow = (row: any, cb: () => void) => {
cb();
};
processRow(row, function () {
connection.resume();
});
})
.on('end', function () {
// all rows have been received
});
let writable = fs.createWriteStream('file.txt');
connection.query('SELECT * FROM posts')
.stream({ highWaterMark: 5 })
.pipe(writable);
connection = mysql.createConnection({ multipleStatements: true });
connection.query('SELECT 1; SELECT 2', function (err: mysql.QueryError, results: mysql.RowDataPacket) {
// `results` is an array with one element for every statement in the query:
console.log(results[0]); // [{1: 1}]
console.log(results[1]); // [{2: 2}]
});
query = connection.query('SELECT 1; SELECT 2');
query
.on('fields', function (fields, index) {
// the fields for the result rows that follow
})
.on('result', function (row, index) {
// index refers to the statement this result belongs to (starts at 0)
});
let options = { sql: '...', nestTables: true };
connection.query(options, function (err, results) {
/* results will be an array like this now:
[{
table1: {
fieldA: '...',
fieldB: '...',
},
table2: {
fieldA: '...',
fieldB: '...',
},
}, ...]
*/
});
connection.beginTransaction(function (err) {
let title = 'title';
if (err) { throw err; }
connection.query('INSERT INTO posts SET title=?', title, function (err: mysql.QueryError, result: mysql.OkPacket) {
if (err) {
connection.rollback(function () {
throw err;
});
}
let log = 'Post ' + result.insertId + ' added';
connection.query('INSERT INTO log SET data=?', log, function (err, result) {
if (err) {
connection.rollback(function () {
throw err;
});
}
connection.commit(function (err) {
if (err) {
connection.rollback(function () {
throw err;
});
}
console.log('success!');
});
});
});
});
// Kill query after 60s
connection.query({ sql: 'SELECT COUNT(*) AS count FROM big_table', timeout: 60000 }, function (err: mysql.QueryError, rows: mysql.RowDataPacket[]) {
if (err && err.code === 'PROTOCOL_SEQUENCE_TIMEOUT') {
throw new Error('too long to count table rows!');
}
if (err) {
throw err;
}
console.log(rows[0]['count'] + ' rows');
});
connection = mysql.createConnection({
port: 84943 // WRONG PORT
});
connection.connect(function (err) {
if (err) {
console.log(err.code); // 'ECONNREFUSED'
console.log(err.fatal); // true
}
});
connection.query('SELECT 1', function (err) {
if (err) {
console.log(err.code); // 'ECONNREFUSED'
console.log(err.fatal); // true
}
});
connection.query('USE name_of_db_that_does_not_exist', function (err, rows) {
if (err) {
console.log(err.code); // 'ER_BAD_DB_ERROR'
}
});
connection.query('SELECT 1', function (err: mysql.QueryError | null, rows: mysql.RowDataPacket[]) {
console.log(err); // null
console.log(rows.length); // 1
});
connection.on('error', function (err: NodeJS.ErrnoException | null) {
if (err) {
console.log(err.code); // 'ER_BAD_DB_ERROR'
}
});
connection.query('USE name_of_db_that_does_not_exist');
// I am Chuck Norris:
connection.on('error', function () {
// ignore
});
connection = mysql.createConnection({ typeCast: false });
query = connection.query({ sql: '...', typeCast: false }, function (err, results) {
//
});
connection.query({
sql: '...',
typeCast: function (field: any, next: Function) {
if (field.type === 'TINY' && field.length === 1) {
return (field.string() === '1'); // 1 = true, 0 = false
}
return next();
}
});
connection = mysql.createConnection('mysql://localhost/test?flags=-FOUND_ROWS');
connection = mysql.createConnection({ debug: true });
connection = mysql.createConnection({ debug: ['ComQueryPacket', 'RowDataPacket'] });