import Database from 'tauri-plugin-sql-api'
import { dbStateStore } from '$lib/store/dbState'

export const db = await Database.load('sqlite:food.db')

const DBService = {
	Begin() {
		dbStateStore.update((state) => {
			state.transacting = true
			return state
		})
		return db.execute('begin transaction')
	},
	Commit() {
		dbStateStore.update((state) => {
			state.transacting = false
			return state
		})
		return db.execute('commit')
	},
	Rollback() {
		dbStateStore.update((state) => {
			state.transacting = false
			return state
		})
		return db.execute('rollback')
	}
}

export { DBService }
