-
Notifications
You must be signed in to change notification settings - Fork 3
/
bcp.js
66 lines (65 loc) · 1.98 KB
/
bcp.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
const exec = require('child_process').spawnSync;
const fs = require('fs');
const os = require('os');
const path = require('path');
const uuid = require('uuid');
module.exports = function bcp({
table,
command,
file,
user,
password,
server,
port,
options,
database,
separator,
terminator,
firstRow = 1,
lastRow,
maxErrors = 0,
hints,
formatFile,
tempDir = os.tmpdir(),
unicode = true
}) {
if (!table) throw new Error('bcp: Missing table parameter');
if (!['in', 'out', 'queryout', 'format'].includes(command)) throw new Error('bcp: Invalid command');
if (!tempDir) throw new Error('bcp: Unknown os.tmpdir()');
const tempFileName = path.join(tempDir, uuid.v4() + '-bcp-errors.txt');
const execResult = exec('bcp', [
table,
command,
command === 'format' ? 'nul' : file,
command === 'format' && (unicode ? '-w' : '-c'),
`-e${tempFileName}`,
formatFile && `-f${formatFile}`,
maxErrors && `-m${maxErrors}`,
firstRow && `-F${firstRow}`,
lastRow && `-L${lastRow}`,
hints && `-h${hints}`,
separator && `-t${separator}`,
terminator && `-r${terminator}`,
options?.trustServerCertificate && '-u',
user && `-U${user}`,
database && `-d${database}`,
server && (port ? `-S${server},${port}` : `-S${server}`)
].filter(Boolean), {
input: password + '\n'
});
let errors = fs.existsSync(tempFileName);
let output;
try {
if (execResult.error) throw execResult.error;
output = execResult.stdout.toString();
if (execResult.status !== 0) throw new Error(output.replace(/^Password:.*\r?\n/, ''));
errors = fs.readFileSync(tempFileName).toString();
} finally {
if (errors) fs.unlinkSync(tempFileName);
}
const rows = output.match(/^(\d+) rows copied\./m);
return {
rows: rows ? parseInt(rows[1], 10) : undefined,
errors
};
};