Skip to content

Instantly share code, notes, and snippets.

@nomoney4me
Created November 16, 2018 06:33
Show Gist options
  • Select an option

  • Save nomoney4me/7a287b4afb7440676e29a952fc5513b1 to your computer and use it in GitHub Desktop.

Select an option

Save nomoney4me/7a287b4afb7440676e29a952fc5513b1 to your computer and use it in GitHub Desktop.
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