new streams

This commit is contained in:
Jan Prochazka 2020-06-10 21:35:25 +02:00
parent 26120969de
commit 1695fb2fd8
7 changed files with 77 additions and 27 deletions

View 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;

View 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;

View File

@ -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();
}

View File

@ -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,
};

View File

@ -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
View 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);

View File

@ -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"

1 1 id Rock country
2 2 1 Jazz Czechia
3 3 2 Metal Austria
4 4 3 Alternative & Punk Germany
5 5 4 Rock And Roll Romania
6 6 5 Blues Great Britain
7 7 6 Latin Bosna, Hecegovina
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