Skip to content

Commit 5bed0f8

Browse files
authored
feat: add conn-level infileStreamFactory option (#2159)
1 parent 5e5ecb2 commit 5bed0f8

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

documentation/en/Extras.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ connection.query(
6868
);
6969
```
7070

71+
The `infileStreamFactory` option may also be set at a connection-level:
72+
73+
```js
74+
const fs = require("fs");
75+
const mysql = require('mysql2');
76+
77+
const connection = mysql.createConnection({
78+
user: 'test',
79+
database: 'test',
80+
infileStreamFactory: path => {
81+
// Validate file path
82+
const validPaths = ['/tmp/data.csv'];
83+
if (!validPaths.includes(path)) {
84+
throw new Error(`invalid file path: ${path}: expected to be one of ${validPaths.join(',')}`);
85+
}
86+
return fs.createReadStream(path);
87+
}
88+
});
89+
90+
connection.query('LOAD DATA LOCAL INFILE "/tmp/data.csv" INTO TABLE test', onInserted);
91+
```
92+
7193
## Connecting using custom stream:
7294

7395
```js

lib/connection.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,15 @@ class Connection extends EventEmitter {
615615
}
616616

617617
execute(sql, values, cb) {
618-
let options = {};
618+
let options = {
619+
infileStreamFactory: this.config.infileStreamFactory
620+
};
619621
if (typeof sql === 'object') {
620622
// execute(options, cb)
621-
options = sql;
623+
options = {
624+
...options,
625+
...sql
626+
};
622627
if (typeof values === 'function') {
623628
cb = values;
624629
} else {
@@ -899,11 +904,15 @@ class Connection extends EventEmitter {
899904

900905
static createQuery(sql, values, cb, config) {
901906
let options = {
902-
rowsAsArray: config.rowsAsArray
907+
rowsAsArray: config.rowsAsArray,
908+
infileStreamFactory: config.infileStreamFactory
903909
};
904910
if (typeof sql === 'object') {
905911
// query(options, cb)
906-
options = sql;
912+
options = {
913+
...options,
914+
...sql
915+
};
907916
if (typeof values === 'function') {
908917
cb = values;
909918
} else if (values !== undefined) {

lib/connection_config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const validOptions = {
3030
flags: 1,
3131
host: 1,
3232
insecureAuth: 1,
33+
infileStreamFactory: 1,
3334
isServer: 1,
3435
keepAliveInitialDelay: 1,
3536
localAddress: 1,
@@ -108,6 +109,7 @@ class ConnectionConfig {
108109
? 10 * 1000
109110
: options.connectTimeout;
110111
this.insecureAuth = options.insecureAuth || false;
112+
this.infileStreamFactory = options.infileStreamFactory || undefined;
111113
this.supportBigNumbers = options.supportBigNumbers || false;
112114
this.bigNumberStrings = options.bigNumberStrings || false;
113115
this.decimalNumbers = options.decimalNumbers || false;

typings/mysql/lib/Connection.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Modifications copyright (c) 2021, Oracle and/or its affiliates.
55

66
import { EventEmitter } from 'events';
7+
import { Readable } from 'stream';
78
import { Query, QueryError } from './protocol/sequences/Query.js';
89
import { Prepare, PrepareStatementInfo } from './protocol/sequences/Prepare.js';
910
import {
@@ -165,6 +166,11 @@ export interface ConnectionOptions {
165166
*/
166167
insecureAuth?: boolean;
167168

169+
/**
170+
* By specifying a function that returns a readable stream, an arbitrary stream can be sent when sending a local fs file.
171+
*/
172+
infileStreamFactory?: (path: string) => Readable;
173+
168174
/**
169175
* Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
170176
* to disable type casting, but you can currently do so on either the connection or query level. (Default: true)

0 commit comments

Comments
 (0)