-
-
Save nomoney4me/7a287b4afb7440676e29a952fc5513b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const generateTable = (table_name, columns, data) => { | |
| return knex.transaction((trx) => { | |
| return trx.schema.dropTableIfExists(table_name) | |
| .then(() => { | |
| return trx.schema.createTable(table_name, (tbl) => { | |
| tbl.increments('id') | |
| columns.map(key => { | |
| key === 'user_id' | |
| ? tbl.integer(key) | |
| : tbl.string(key) | |
| }) | |
| }) | |
| }) | |
| .then(() => { | |
| console.log(`${table_name} has been created with colums: [${columns}]`) | |
| // # populate the table | |
| let insert_jobs = [] | |
| data.map(row => { | |
| insert_jobs.push(trx.insert(row).into(table_name)) | |
| }) | |
| return Promise.all(insert_jobs).then(() => console.log(`${table_name} has been populated`)) | |
| }) | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment