import Dexie from 'dexie'

const db = new Dexie('metaDb')
db.version(1).stores({
  player: '++id, name',
  game: '++id, team, season, day'
})

db.open().catch(e => {
  console.error(`metaDb open failed: ${e.stack}`)
})

export const resetPlayer = () => db.table('player').clear()
  .catch(e => console.log(`error resetting player table: ${e}`))
export const resetGame = () => db.table('game').clear()
  .catch(e => console.log(`error resetting game table: ${e}`))

export const getPlayer = () => db.table('player').toArray()
  .then(players => players[0].name)
  .catch(() => null)

export default db

//
// // DB is your Dexie object
// // objectList is your list of objects
// DB.transaction('rw', DB.table, function()
// {
//     var numObjects = objectList.length
//
//     for ( var i = 0 i < numObjects i++ )
//     {
//         DB.table.put( objectList[i] )
//     }
// })