package database

import (
	"github.com/asdine/storm"
	"github.com/rs/zerolog"
)

type DB struct {
	*storm.DB
	*zerolog.Logger
}

// OpenOrCreate returns a new database object, either from existing database or creates a new one
func OpenOrCreateDB(dbPath string, log *zerolog.Logger) (*DB, error) {
	var db DB
	databaseLogger := log.With().Str("module", "database").Logger() // Setting up a sub logger for the database module
	db.Logger = &databaseLogger
	if err := db.ConfigureDB(dbPath); err != nil {
		db.Err(err).Msg("unable to configure database")
		return &db, err
	}
	return &db, nil
}
