ndjson direct support

This commit is contained in:
Jan Prochazka 2022-03-27 20:35:30 +02:00
parent a20a34680d
commit 7c0f33383f
5 changed files with 34 additions and 7 deletions

View File

@ -6,6 +6,10 @@ function getJslFileName(jslid) {
if (archiveMatch) { if (archiveMatch) {
return path.join(resolveArchiveFolder(archiveMatch[1]), `${archiveMatch[2]}.jsonl`); return path.join(resolveArchiveFolder(archiveMatch[1]), `${archiveMatch[2]}.jsonl`);
} }
const fileMatch = jslid.match(/^file:\/\/(.*)$/);
if (fileMatch) {
return fileMatch[1];
}
return path.join(jsldir(), `${jslid}.jsonl`); return path.join(jsldir(), `${jslid}.jsonl`);
} }

View File

@ -3,7 +3,8 @@ import { FileFormatDefinition, QuickExportDefinition } from 'dbgate-types';
const jsonlFormat = { const jsonlFormat = {
storageType: 'jsonl', storageType: 'jsonl',
extension: 'jsonl', extension: 'jsonl',
name: 'JSON lines', extensions: ['jsonl', 'ndjson'],
name: 'JSON lines/NDJSON',
readerFunc: 'jsonLinesReader', readerFunc: 'jsonLinesReader',
writerFunc: 'jsonLinesWriter', writerFunc: 'jsonLinesWriter',
}; };
@ -23,7 +24,7 @@ const sqlFormat = {
}; };
const jsonlQuickExport = { const jsonlQuickExport = {
label: 'JSON lines', label: 'JSON lines/NDJSON',
extension: 'jsonl', extension: 'jsonl',
createWriter: fileName => ({ createWriter: fileName => ({
functionName: 'jsonLinesWriter', functionName: 'jsonLinesWriter',

View File

@ -1,5 +1,5 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
export const matchingProps = ['archiveFile', 'archiveFolder']; export const matchingProps = ['archiveFile', 'archiveFolder', 'jslid'];
</script> </script>
<script lang="ts"> <script lang="ts">

View File

@ -167,8 +167,8 @@ export const copyRowsFormatDefs = {
formatter: clipboardJsonFormatter(), formatter: clipboardJsonFormatter(),
}, },
jsonLines: { jsonLines: {
label: 'Copy as JSON lines', label: 'Copy as JSON lines/NDJSON',
name: 'JSON lines', name: 'JSON lines/NDJSON',
formatter: clipboardJsonLinesFormatter(), formatter: clipboardJsonLinesFormatter(),
}, },
yaml: { yaml: {

View File

@ -7,6 +7,8 @@ import { currentDatabase, extensions } from '../stores';
import { getUploadListener } from './uploadFiles'; import { getUploadListener } from './uploadFiles';
import { getDatabaseFileLabel } from './getConnectionLabel'; import { getDatabaseFileLabel } from './getConnectionLabel';
import { apiCall } from './api'; import { apiCall } from './api';
import openNewTab from './openNewTab';
import _ from 'lodash';
export function canOpenByElectron(file, extensions) { export function canOpenByElectron(file, extensions) {
if (!file) return false; if (!file) return false;
@ -15,6 +17,7 @@ export function canOpenByElectron(file, extensions) {
if (nameLower.endsWith('.db') || nameLower.endsWith('.sqlite') || nameLower.endsWith('.sqlite3')) return true; if (nameLower.endsWith('.db') || nameLower.endsWith('.sqlite') || nameLower.endsWith('.sqlite3')) return true;
for (const format of extensions.fileFormats) { for (const format of extensions.fileFormats) {
if (nameLower.endsWith(`.${format.extension}`)) return true; if (nameLower.endsWith(`.${format.extension}`)) return true;
if (format.extensions?.find(ext => nameLower.endsWith(`.${ext}`))) return true;
} }
return false; return false;
} }
@ -50,6 +53,18 @@ function getFileEncoding(filePath, fs) {
return e; return e;
} }
function openElectronJsonLinesFile(filePath, parsed) {
openNewTab({
title: parsed.name,
tooltip: filePath,
icon: 'img sql-file',
tabComponent: 'ArchiveFileTab',
props: {
jslid: `file://${filePath}`,
},
});
}
export function openElectronFileCore(filePath, extensions) { export function openElectronFileCore(filePath, extensions) {
const nameLower = filePath.toLowerCase(); const nameLower = filePath.toLowerCase();
const path = window.require('path'); const path = window.require('path');
@ -74,6 +89,10 @@ export function openElectronFileCore(filePath, extensions) {
openSqliteFile(filePath); openSqliteFile(filePath);
return; return;
} }
if (nameLower.endsWith('.jsonl') || nameLower.endsWith('.ndjson')) {
openElectronJsonLinesFile(filePath, parsed);
return;
}
for (const format of extensions.fileFormats) { for (const format of extensions.fileFormats) {
if (nameLower.endsWith(`.${format.extension}`)) { if (nameLower.endsWith(`.${format.extension}`)) {
if (uploadListener) { if (uploadListener) {
@ -100,16 +119,19 @@ export function openElectronFileCore(filePath, extensions) {
} }
function getFileFormatFilters(extensions) { function getFileFormatFilters(extensions) {
return extensions.fileFormats.filter(x => x.readerFunc).map(x => ({ name: x.name, extensions: [x.extension] })); return extensions.fileFormats
.filter(x => x.readerFunc)
.map(x => ({ name: x.name, extensions: x.extensions || [x.extension] }));
} }
function getFileFormatExtensions(extensions) { function getFileFormatExtensions(extensions) {
return extensions.fileFormats.filter(x => x.readerFunc).map(x => x.extension); return _.flatten(extensions.fileFormats.filter(x => x.readerFunc).map(x => x.extensions || [x.extension]));
} }
export async function openElectronFile() { export async function openElectronFile() {
const electron = getElectron(); const electron = getElectron();
const ext = get(extensions); const ext = get(extensions);
const filePaths = await electron.showOpenDialog({ const filePaths = await electron.showOpenDialog({
filters: [ filters: [
{ name: `All supported files`, extensions: ['sql', 'sqlite', 'db', 'sqlite3', ...getFileFormatExtensions(ext)] }, { name: `All supported files`, extensions: ['sql', 'sqlite', 'db', 'sqlite3', ...getFileFormatExtensions(ext)] },