
/* <%- lintDisable -%> no-console */
// <%= serviceName %>-model.<%- js %> - A KnexJS
<%- 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) {') %>
  const db = app.get('knexClient')<%- sc %>
  const tableName = '<%= snakeName %>'
  <%- insertFragment('knex_func_init') %>

  db.schema.hasTable(tableName).then(exists => {
    if (!exists) {
      db.schema.createTable(tableName, table => {
        table.increments('id')<%- sc %>
      <% if(authentication.strategies.indexOf('local') !== -1) { %>
        table.string('email').unique()<%- sc %>
        table.string('password')<%- sc %>
      <% } %>
      <% oauthProviders.forEach(provider => { %>
        table.string('<%= provider.name %>Id')<%- 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') %>
