127.0.0.1:8000 budget / master server / utils / import_and_export.js
master

Tree @master (Download .tar.gz)

import_and_export.js @masterraw · history · blame

const utils = require('.');
const {
    stringify
} = require('csv-stringify');

module.exports = {
    import_data: async function() {
        // TODO:
    },
    export_data: async function() {
        const tables = await utils.query("SHOW TABLES");
        const csv_rows = await Promise.all(tables.filter(function(table) {
            return !["sessions"].includes(table)
        }).map(async function(table) {
            console.log(table);
            const rows = await utils.query("SELECT * FROM " + table["Tables_in_budget"]);
            if (!rows.length) {
                return;
            }
            let [first_row, ...rest_rows] = [rows.unshift(), ...rows];
            delete first_row.id;
            return [Object.keys(first_row), ...[first_row, ...rest_rows].map(function(row) {
                delete row.id;
                return Object.values(row);
            })];
        }));
        return stringify(csv_rows, {
            header: true
        });
    }
};