Skip to content

Instantly share code, notes, and snippets.

@zmts
Created March 22, 2019 10:33
Show Gist options
  • Select an option

  • Save zmts/c288d0fd0a6fe2995a07cd242cf41951 to your computer and use it in GitHub Desktop.

Select an option

Save zmts/c288d0fd0a6fe2995a07cd242cf41951 to your computer and use it in GitHub Desktop.
Knex: create table if not exists

Knex: Create table if not exists

const Knex = require('knex')
const config = require('./config')
const knex = Knex(config.knex)

async function createSchema () {
  const hasTable = await knex.schema.hasTable('videoRequests')
  if (!hasTable) {
    return knex.migrate.latest()
      .then(() => {
        __logger.info(`Server was successfully creates initial table in '${config.knex.connection.database}' database`)
      }).catch(error => {
        throw Error(`Initial schema creation was fail ${error}`)
      })
  } else {
    __logger.info(`'${config.knex.connection.database}' database already has required schema`)
  }
}

module.exports = createSchema
@gomeznieto
Copy link

great, tahnks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment