mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 14:56:01 +00:00
DBF reader plugin
This commit is contained in:
12
plugins/dbgate-plugin-dbf/src/backend/index.js
Normal file
12
plugins/dbgate-plugin-dbf/src/backend/index.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const reader = require('./reader');
|
||||
// const writer = require('./writer');
|
||||
|
||||
module.exports = {
|
||||
packageName: 'dbgate-plugin-dbf',
|
||||
shellApi: {
|
||||
reader,
|
||||
},
|
||||
initialize(dbgateEnv) {
|
||||
reader.initialize(dbgateEnv);
|
||||
},
|
||||
};
|
||||
85
plugins/dbgate-plugin-dbf/src/backend/reader.js
Normal file
85
plugins/dbgate-plugin-dbf/src/backend/reader.js
Normal file
@@ -0,0 +1,85 @@
|
||||
const _ = require('lodash');
|
||||
const csv = require('csv');
|
||||
const fs = require('fs');
|
||||
const stream = require('stream');
|
||||
const { DBFFile } = require('dbffile');
|
||||
|
||||
let dbgateApi;
|
||||
|
||||
function getFieldType(field) {
|
||||
const { type, size } = field;
|
||||
switch (type) {
|
||||
case 'C':
|
||||
return `varchar(${size})`;
|
||||
case 'N':
|
||||
return 'numeric';
|
||||
case 'F':
|
||||
return 'float';
|
||||
case 'Y':
|
||||
return 'money';
|
||||
case 'I':
|
||||
return 'int';
|
||||
case 'L':
|
||||
return 'boolean';
|
||||
case 'D':
|
||||
return 'date';
|
||||
case 'T':
|
||||
return 'datetime';
|
||||
case 'B':
|
||||
return 'duouble';
|
||||
case 'M':
|
||||
return 'varchar(max)';
|
||||
default:
|
||||
return 'string';
|
||||
}
|
||||
}
|
||||
|
||||
async function reader({ fileName, encoding = 'ISO-8859-1', includeDeletedRecords = false, limitRows = undefined }) {
|
||||
console.log(`Reading file ${fileName}`);
|
||||
const downloadedFile = await dbgateApi.download(fileName);
|
||||
|
||||
const pass = new stream.PassThrough({
|
||||
objectMode: true,
|
||||
});
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
// Open the DBF file
|
||||
const dbf = await DBFFile.open(downloadedFile, { encoding, includeDeletedRecords });
|
||||
|
||||
const columns = dbf.fields.map((field) => ({
|
||||
columnName: field.name,
|
||||
dataType: getFieldType(field),
|
||||
}));
|
||||
|
||||
pass.write({
|
||||
__isStreamHeader: true,
|
||||
columns,
|
||||
})
|
||||
|
||||
let readedRows = 0;
|
||||
// Read each record and push it into the stream
|
||||
for await (const record of dbf) {
|
||||
// Emit the record as a chunk
|
||||
pass.write(record);
|
||||
readedRows++;
|
||||
if (limitRows && readedRows >= limitRows) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pass.end();
|
||||
} catch (error) {
|
||||
// If any error occurs, destroy the stream with the error
|
||||
pass.end();
|
||||
}
|
||||
})();
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
reader.initialize = (dbgateEnv) => {
|
||||
dbgateApi = dbgateEnv.dbgateApi;
|
||||
};
|
||||
|
||||
module.exports = reader;
|
||||
25
plugins/dbgate-plugin-dbf/src/frontend/index.js
Normal file
25
plugins/dbgate-plugin-dbf/src/frontend/index.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const fileFormat = {
|
||||
packageName: 'dbgate-plugin-dbf',
|
||||
// file format identifier
|
||||
storageType: 'dbf',
|
||||
// file extension without leading dot
|
||||
extension: 'dbf',
|
||||
// human readable file format name
|
||||
name: 'DBF',
|
||||
// function name from backend, which contains reader factory, postfixed by package name
|
||||
readerFunc: 'reader@dbgate-plugin-dbf',
|
||||
|
||||
args: [
|
||||
{
|
||||
type: 'text',
|
||||
name: 'encoding',
|
||||
label: 'Encoding',
|
||||
apiName: 'encoding',
|
||||
},
|
||||
],
|
||||
|
||||
};
|
||||
|
||||
export default {
|
||||
fileFormats: [fileFormat],
|
||||
};
|
||||
Reference in New Issue
Block a user