
// Initializes the `<%= serviceName %>` service on path `/<%= path %>`
<%- tplTsOnly([`import { App } from '../../${subFolderReverse}app.interface'${sc}`, '', '']) -%>
<%- tplImports('createService', 'feathers-mongodb', 'req') %>
<%- tplImports('hooks', `./${kebabName}.hooks`) %>
<%- tplTsOnly([`import { Db, MongoClient } from 'mongodb'${sc}`, '']) -%>
<%- insertFragment('imports', [
  `// ${tplImports('$jsonSchema', `./${kebabName}.mongo`, 'req', 'let')}`
]) %>
<%- insertFragment('init') %>

<%- tplModuleExports(null, 'function (app) {', 'function (app: App) {') %>
  let paginate = app.get('paginate')<%- sc %>
  let mongoClient = app.get('mongoClient')<%- tplTsOnly([' as Promise<Db>']) -%><%- sc %>
  let options = { paginate }<%- sc %>
  <%- insertFragment('func_init') %>

  // Initialize our service with any options it requires
  <%- insertFragment('extend', `  app.use('/${path}', createService(options))${sc}`) %>

  // Get our initialized service so that we can register hooks
  const service = app.service('<%= path %>')<%- sc %>

  // eslint-disable-next-line no-unused-vars
  let promise = mongoClient.then(db => {
    return db.createCollection('<%= kebabName %>', {
      <%- insertFragment('create_collection', [
        '      // validator: { $jsonSchema: $jsonSchema },',
        '      // validationLevel: \'strict\', // The MongoDB default',
        '      // validationAction: \'error\', // The MongoDB default',
      ]) %>
    })<%- sc %>
  })
    .then(serviceModel => {
      <%- tplJsOrTs('service', '(service as any)') %>.Model = serviceModel<%- sc %>
    })<%- sc %>

  service.hooks(hooks)<%- sc %>
  <%- insertFragment('func_return') %>
}<%- sc %>
<%- insertFragment('more') %>

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

<%- insertFragment('funcs') %>
<%- insertFragment('end') %>
