mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 12:13:57 +00:00
new streams
This commit is contained in:
parent
26120969de
commit
1695fb2fd8
16
packages/api/src/shell/consoleObjectWriter.js
Normal file
16
packages/api/src/shell/consoleObjectWriter.js
Normal file
@ -0,0 +1,16 @@
|
||||
const stream = require('stream');
|
||||
|
||||
class ObjectWriterStream extends stream.Writable {
|
||||
_write(chunk, enc, next) {
|
||||
console.log(JSON.stringify(chunk));
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
async function consoleObjectWriter() {
|
||||
return new ObjectWriterStream({
|
||||
objectMode: true,
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = consoleObjectWriter;
|
15
packages/api/src/shell/csvReader.js
Normal file
15
packages/api/src/shell/csvReader.js
Normal file
@ -0,0 +1,15 @@
|
||||
const csv = require('csv');
|
||||
const fs = require('fs');
|
||||
|
||||
async function csvReader({ fileName, encoding = 'utf-8', ...options }) {
|
||||
console.log(`Reading file ${fileName}`);
|
||||
const csvStream = csv.parse({
|
||||
columns: true,
|
||||
...options,
|
||||
});
|
||||
const fileStream = fs.createReadStream(fileName, encoding);
|
||||
fileStream.pipe(csvStream);
|
||||
return csvStream;
|
||||
}
|
||||
|
||||
module.exports = csvReader;
|
@ -7,8 +7,10 @@ async function fakeObjectReader({ delay = 0 } = {}) {
|
||||
function doWrite() {
|
||||
pass.write({ id: 1, country: 'Czechia' });
|
||||
pass.write({ id: 2, country: 'Austria' });
|
||||
pass.write({ id: 3, country: 'Germany' });
|
||||
pass.write({ id: 4, country: 'Romania' });
|
||||
pass.write({ country: 'Germany', id: 3 });
|
||||
pass.write({ country: 'Romania', id: 4 });
|
||||
pass.write({ country: 'Great Britain', id: 5 });
|
||||
pass.write({ country: 'Bosna, Hecegovina', id: 6 });
|
||||
pass.end();
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,17 @@
|
||||
const queryReader = require('./queryReader');
|
||||
const csvWriter = require('./csvWriter');
|
||||
const csvReader = require('./csvReader');
|
||||
const runScript = require('./runScript');
|
||||
const copyStream = require('./copyStream');
|
||||
const fakeObjectReader = require('./fakeObjectReader');
|
||||
const consoleObjectWriter = require('./consoleObjectWriter');
|
||||
|
||||
module.exports = {
|
||||
queryReader,
|
||||
csvWriter,
|
||||
csvReader,
|
||||
runScript,
|
||||
copyStream,
|
||||
fakeObjectReader,
|
||||
consoleObjectWriter,
|
||||
};
|
||||
|
@ -17,9 +17,13 @@ async function run() {
|
||||
|
||||
const csvWriter = await dbgateApi.csvWriter({
|
||||
fileName: 'test.csv',
|
||||
header: true,
|
||||
});
|
||||
|
||||
const consoleWriter = await dbgateApi.consoleObjectWriter();
|
||||
|
||||
await dbgateApi.copyStream(queryReader, csvWriter);
|
||||
// await dbgateApi.copyStream(queryReader, consoleWriter);
|
||||
}
|
||||
|
||||
dbgateApi.runScript(run);
|
||||
|
27
test/importTable.js
Normal file
27
test/importTable.js
Normal file
@ -0,0 +1,27 @@
|
||||
const dbgateApi = require('@dbgate/api');
|
||||
|
||||
async function run() {
|
||||
const csvReader = await dbgateApi.csvReader({
|
||||
fileName: 'test.csv',
|
||||
header: true,
|
||||
});
|
||||
|
||||
|
||||
const tableWriter = await dbgateApi.tableWriter({
|
||||
connection: {
|
||||
server: 'localhost',
|
||||
engine: 'mysql',
|
||||
user: 'root',
|
||||
password: 'test',
|
||||
port: '3307',
|
||||
database: 'Chinook',
|
||||
},
|
||||
pureName: 'importedTable'
|
||||
});
|
||||
|
||||
const consoleWriter = await dbgateApi.consoleObjectWriter();
|
||||
|
||||
await dbgateApi.copyStream(csvReader, consoleWriter);
|
||||
}
|
||||
|
||||
dbgateApi.runScript(run);
|
@ -1,25 +1,7 @@
|
||||
1,Rock
|
||||
2,Jazz
|
||||
3,Metal
|
||||
4,Alternative & Punk
|
||||
5,Rock And Roll
|
||||
6,Blues
|
||||
7,Latin
|
||||
8,Reggae
|
||||
9,Pop
|
||||
10,Soundtrack
|
||||
11,Bossa Nova
|
||||
12,Easy Listening
|
||||
13,Heavy Metal
|
||||
14,R&B/Soul
|
||||
15,Electronica/Dance
|
||||
16,World
|
||||
17,Hip Hop/Rap
|
||||
18,Science Fiction
|
||||
19,TV Shows
|
||||
20,Sci Fi & Fantasy
|
||||
21,Drama
|
||||
22,Comedy
|
||||
23,Alternative
|
||||
24,Classical
|
||||
25,Opera
|
||||
id,country
|
||||
1,Czechia
|
||||
2,Austria
|
||||
3,Germany
|
||||
4,Romania
|
||||
5,Great Britain
|
||||
6,"Bosna, Hecegovina"
|
||||
|
|
Loading…
Reference in New Issue
Block a user