Skip to content

Commit

Permalink
feat: add conn-level infileStreamFactory option (#2159)
Browse files Browse the repository at this point in the history
  • Loading branch information
loozhengyuan authored Aug 4, 2023
1 parent 5e5ecb2 commit 5bed0f8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
22 changes: 22 additions & 0 deletions documentation/en/Extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ connection.query(
);
```

The `infileStreamFactory` option may also be set at a connection-level:

```js
const fs = require("fs");
const mysql = require('mysql2');

const connection = mysql.createConnection({
user: 'test',
database: 'test',
infileStreamFactory: path => {
// Validate file path
const validPaths = ['/tmp/data.csv'];
if (!validPaths.includes(path)) {
throw new Error(`invalid file path: ${path}: expected to be one of ${validPaths.join(',')}`);
}
return fs.createReadStream(path);
}
});

connection.query('LOAD DATA LOCAL INFILE "/tmp/data.csv" INTO TABLE test', onInserted);
```

## Connecting using custom stream:

```js
Expand Down
17 changes: 13 additions & 4 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,15 @@ class Connection extends EventEmitter {
}

execute(sql, values, cb) {
let options = {};
let options = {
infileStreamFactory: this.config.infileStreamFactory
};
if (typeof sql === 'object') {
// execute(options, cb)
options = sql;
options = {
...options,
...sql
};
if (typeof values === 'function') {
cb = values;
} else {
Expand Down Expand Up @@ -899,11 +904,15 @@ class Connection extends EventEmitter {

static createQuery(sql, values, cb, config) {
let options = {
rowsAsArray: config.rowsAsArray
rowsAsArray: config.rowsAsArray,
infileStreamFactory: config.infileStreamFactory
};
if (typeof sql === 'object') {
// query(options, cb)
options = sql;
options = {
...options,
...sql
};
if (typeof values === 'function') {
cb = values;
} else if (values !== undefined) {
Expand Down
2 changes: 2 additions & 0 deletions lib/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const validOptions = {
flags: 1,
host: 1,
insecureAuth: 1,
infileStreamFactory: 1,
isServer: 1,
keepAliveInitialDelay: 1,
localAddress: 1,
Expand Down Expand Up @@ -108,6 +109,7 @@ class ConnectionConfig {
? 10 * 1000
: options.connectTimeout;
this.insecureAuth = options.insecureAuth || false;
this.infileStreamFactory = options.infileStreamFactory || undefined;
this.supportBigNumbers = options.supportBigNumbers || false;
this.bigNumberStrings = options.bigNumberStrings || false;
this.decimalNumbers = options.decimalNumbers || false;
Expand Down
6 changes: 6 additions & 0 deletions typings/mysql/lib/Connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Modifications copyright (c) 2021, Oracle and/or its affiliates.

import { EventEmitter } from 'events';
import { Readable } from 'stream';
import { Query, QueryError } from './protocol/sequences/Query.js';
import { Prepare, PrepareStatementInfo } from './protocol/sequences/Prepare.js';
import {
Expand Down Expand Up @@ -165,6 +166,11 @@ export interface ConnectionOptions {
*/
insecureAuth?: boolean;

/**
* By specifying a function that returns a readable stream, an arbitrary stream can be sent when sending a local fs file.
*/
infileStreamFactory?: (path: string) => Readable;

/**
* Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
* to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
Expand Down

0 comments on commit 5bed0f8

Please # to comment.