
/* <%- lintDisable -%> no-console */
// <%= serviceName %>-model.<%- js %> - A KnexJS model
<%- tplTsOnly([`import { App } from '../${subFolderReverse}app.interface'${sc}`, '']) -%>
// 
// See http://knexjs.org/
// for more of what you can do here.
<%- insertFragment('knex_imports') %>
<%- insertFragment('knex_init') %>

<%- tplModuleExports(null, 'function (app) {', 'function (app: App) {') %>
  let db = app.get('knexClient')<%- sc %>
  let tableName = '<%= snakeName %>'<%- sc %>
  <%- insertFragment('knex_func_init') %>

  db.schema.hasTable(tableName).then(exists => {
    if(!exists) {
      db.schema.createTable(tableName, table => {
        table.increments('id')<%- sc %>
        table.string('text')<%- sc %>
      })
        .then(() => console.log(`Created ${tableName} table`))
        .catch(e => console.error(`Error creating ${tableName} table`, e))<%- sc %>
    }
  })<%- sc %>

  <%- insertFragment('knex_func_return') %>
  return db<%- sc %>
}<%- sc %>
<%- insertFragment('knex_more') %>

<%- insertFragment('knex_exports') %>
<%- `${tplExport('moduleExports')}${sc}` %>

<%- insertFragment('knex_funcs') %>
<%- insertFragment('knex_end') %>
