File tree Expand file tree Collapse file tree 4 files changed +43
-4
lines changed Expand file tree Collapse file tree 4 files changed +43
-4
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,28 @@ connection.query(
68
68
);
69
69
```
70
70
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
+
71
93
## Connecting using custom stream:
72
94
73
95
``` js
Original file line number Diff line number Diff line change @@ -615,10 +615,15 @@ class Connection extends EventEmitter {
615
615
}
616
616
617
617
execute ( sql , values , cb ) {
618
- let options = { } ;
618
+ let options = {
619
+ infileStreamFactory : this . config . infileStreamFactory
620
+ } ;
619
621
if ( typeof sql === 'object' ) {
620
622
// execute(options, cb)
621
- options = sql ;
623
+ options = {
624
+ ...options ,
625
+ ...sql
626
+ } ;
622
627
if ( typeof values === 'function' ) {
623
628
cb = values ;
624
629
} else {
@@ -899,11 +904,15 @@ class Connection extends EventEmitter {
899
904
900
905
static createQuery ( sql , values , cb , config ) {
901
906
let options = {
902
- rowsAsArray : config . rowsAsArray
907
+ rowsAsArray : config . rowsAsArray ,
908
+ infileStreamFactory : config . infileStreamFactory
903
909
} ;
904
910
if ( typeof sql === 'object' ) {
905
911
// query(options, cb)
906
- options = sql ;
912
+ options = {
913
+ ...options ,
914
+ ...sql
915
+ } ;
907
916
if ( typeof values === 'function' ) {
908
917
cb = values ;
909
918
} else if ( values !== undefined ) {
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ const validOptions = {
30
30
flags : 1 ,
31
31
host : 1 ,
32
32
insecureAuth : 1 ,
33
+ infileStreamFactory : 1 ,
33
34
isServer : 1 ,
34
35
keepAliveInitialDelay : 1 ,
35
36
localAddress : 1 ,
@@ -108,6 +109,7 @@ class ConnectionConfig {
108
109
? 10 * 1000
109
110
: options . connectTimeout ;
110
111
this . insecureAuth = options . insecureAuth || false ;
112
+ this . infileStreamFactory = options . infileStreamFactory || undefined ;
111
113
this . supportBigNumbers = options . supportBigNumbers || false ;
112
114
this . bigNumberStrings = options . bigNumberStrings || false ;
113
115
this . decimalNumbers = options . decimalNumbers || false ;
Original file line number Diff line number Diff line change 4
4
// Modifications copyright (c) 2021, Oracle and/or its affiliates.
5
5
6
6
import { EventEmitter } from 'events' ;
7
+ import { Readable } from 'stream' ;
7
8
import { Query , QueryError } from './protocol/sequences/Query.js' ;
8
9
import { Prepare , PrepareStatementInfo } from './protocol/sequences/Prepare.js' ;
9
10
import {
@@ -165,6 +166,11 @@ export interface ConnectionOptions {
165
166
*/
166
167
insecureAuth ?: boolean ;
167
168
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
+
168
174
/**
169
175
* Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
170
176
* to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
You can’t perform that action at this time.
0 commit comments