diff --git a/integration-tests/__tests__/import-formats.spec.js b/integration-tests/__tests__/import-formats.spec.js index ea55ebd43..5cd7871c0 100644 --- a/integration-tests/__tests__/import-formats.spec.js +++ b/integration-tests/__tests__/import-formats.spec.js @@ -17,7 +17,6 @@ const CSV_DATA = `Issue Number; Title; Github URL; Labels; State; Created At; Up `; test('csv import test', async () => { - expect(1).toEqual(1); const dbgatePluginCsv = requirePlugin('dbgate-plugin-csv'); const csvFileName = tmp.tmpNameSync(); @@ -63,3 +62,36 @@ test('csv import test', async () => { Assignee: '', }); }); + +test('JSON array import test', async () => { + const jsonFileName = tmp.tmpNameSync(); + const jsonLinesFileName = tmp.tmpNameSync(); + + fs.writeFileSync( + jsonFileName, + JSON.stringify([ + { id: 1, val: 'v1' }, + { id: 2, val: 'v2' }, + ]) + ); + + const reader = await dbgateApi.jsonReader({ + fileName: jsonFileName, + }); + + const writer = await dbgateApi.jsonLinesWriter({ + fileName: jsonLinesFileName, + }); + await dbgateApi.copyStream(reader, writer); + + const jsonData = fs.readFileSync(jsonLinesFileName, 'utf-8'); + const rows = jsonData + .split('\n') + .filter(x => x.trim() !== '') + .map(x => JSON.parse(x)); + expect(rows.length).toEqual(2); + expect(rows[0]).toEqual({ + id: 1, + val: 'v1', + }); +}); diff --git a/packages/api/src/shell/index.js b/packages/api/src/shell/index.js index 253bb053a..82513088d 100644 --- a/packages/api/src/shell/index.js +++ b/packages/api/src/shell/index.js @@ -29,6 +29,7 @@ const modifyJsonLinesReader = require('./modifyJsonLinesReader'); const dataDuplicator = require('./dataDuplicator'); const dbModelToJson = require('./dbModelToJson'); const jsonToDbModel = require('./jsonToDbModel'); +const jsonReader = require('./jsonReader'); const dbgateApi = { queryReader, @@ -61,6 +62,7 @@ const dbgateApi = { dataDuplicator, dbModelToJson, jsonToDbModel, + jsonReader, }; requirePlugin.initializeDbgateApi(dbgateApi); diff --git a/packages/api/src/shell/jsonReader.js b/packages/api/src/shell/jsonReader.js index e29187738..b54b94717 100644 --- a/packages/api/src/shell/jsonReader.js +++ b/packages/api/src/shell/jsonReader.js @@ -15,7 +15,6 @@ class ParseStream extends stream.Transform { this.rowsWritten = 0; } _transform(chunk, encoding, done) { - const obj = JSON.parse(chunk); if (!this.wasHeader) { this.push({ __isStreamHeader: true, @@ -25,7 +24,7 @@ class ParseStream extends stream.Transform { this.wasHeader = true; } if (!this.limitRows || this.rowsWritten < this.limitRows) { - this.push(obj.value); + this.push(chunk.value); this.rowsWritten += 1; } done();