· 6 years ago · Mar 27, 2019, 11:22 PM
1# Knex: Create table if not exists
2
3```
4const Knex = require('knex')
5const config = require('./config')
6const knex = Knex(config.knex)
7
8async function createSchema () {
9 const hasTable = await knex.schema.hasTable('videoRequests')
10 if (!hasTable) {
11 return knex.migrate.latest()
12 .then(() => {
13 __logger.info(`Server was successfully creates initial table in '${config.knex.connection.database}' database`)
14 }).catch(error => {
15 throw Error(`Initial schema creation was fail ${error}`)
16 })
17 } else {
18 __logger.info(`'${config.knex.connection.database}' database already has required schema`)
19 }
20}
21
22module.exports = createSchema
23```