diff --git a/packages/api/src/shell/consoleObjectWriter.js b/packages/api/src/shell/consoleObjectWriter.js new file mode 100644 index 00000000..6623ff0b --- /dev/null +++ b/packages/api/src/shell/consoleObjectWriter.js @@ -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; diff --git a/packages/api/src/shell/csvReader.js b/packages/api/src/shell/csvReader.js new file mode 100644 index 00000000..204221e2 --- /dev/null +++ b/packages/api/src/shell/csvReader.js @@ -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; diff --git a/packages/api/src/shell/fakeObjectReader.js b/packages/api/src/shell/fakeObjectReader.js index 8b7fa4f0..73532d5c 100644 --- a/packages/api/src/shell/fakeObjectReader.js +++ b/packages/api/src/shell/fakeObjectReader.js @@ -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(); } diff --git a/packages/api/src/shell/index.js b/packages/api/src/shell/index.js index 2cb673e2..31f6c3f5 100644 --- a/packages/api/src/shell/index.js +++ b/packages/api/src/shell/index.js @@ -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, }; diff --git a/test/exportTable.js b/test/exportTable.js index cdc1bf9a..35fffdba 100644 --- a/test/exportTable.js +++ b/test/exportTable.js @@ -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); diff --git a/test/importTable.js b/test/importTable.js new file mode 100644 index 00000000..524a9677 --- /dev/null +++ b/test/importTable.js @@ -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); diff --git a/test/test.csv b/test/test.csv index 0547517b..e86e72da 100644 --- a/test/test.csv +++ b/test/test.csv @@ -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"