'use strict'; const csv = require ('csv-parse'); const fs = require ('fs-extra'); const path = require ('path'); /** * get all csv data that should be loaded * * @returns {Promise>} csv data */ async function get_csv () { const csv_list = await Promise.all (( await fs.readdir ('migrations/csv')) .map ( async (file) => { if (path.extname (file) !== '.csv') return null; const name = path.basename (file, '.csv'); const data = []; const parser = csv (await fs.readFile ( path.join ('migrations/csv', file) ), { columns: true }); for await (const entry of parser) data.push (entry); return { name, data }; } )); return csv_list.filter ((val) => val !== null); } /** * execute migration * * @param {any} knex database connection */ async function up (knex) { const data = await get_csv (); await Promise.all ( data.map ( (table) => knex (table.name) .insert (table.data) ) ); } /** * revert migration */ function down () { // noop } module.exports = { up, down };