Implement model

This commit is contained in:
2024-06-11 21:59:51 +02:00
parent 54eed02ddd
commit babeed7c53
3 changed files with 41 additions and 33 deletions

View File

@@ -1,34 +1,14 @@
<script lang="ts">
import Router from '$router/Router.svelte'
import { mode, ModeWatcher } from 'mode-watcher'
import Header from '$lib/components/Header.svelte'
import { Toaster } from 'svelte-sonner'
import { onMount } from 'svelte'
import { appConfigDir } from '@tauri-apps/api/path'
import Database from 'tauri-plugin-sql-api'
import { type Food, FoodService } from '$lib/database/food'
import type { Writable } from 'svelte/store'
let food: Food[] = []
onMount(async () => {
console.log(await appConfigDir())
window.invoke = window.__TAURI_INVOKE__
const db = await Database.load('sqlite:food.db')
await db.execute(
'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);'
)
type User = {
id: number
name: string
age: number
}
await db.execute('INSERT INTO users (name, age) VALUES (\'John\', 30);')
const user = await db.select<User>('SELECT * FROM users WHERE name = $1;', [
'John'
])
console.log(user)
food = await FoodService.GetAll()
})
</script>
@@ -36,8 +16,15 @@
<Toaster theme={$mode} />
<div class="relative flex flex-col h-screen" data-vaul-drawer-wrapper id="page">
<Header />
<main class="flex-1">
<Router />
</main>
<div>
<pre>
{#each food as f}
{f.amount}
{/each}
</pre>
</div>
<!-- <Header />-->
<!-- <main class="flex-1">-->
<!-- <Router />-->
<!-- </main>-->
</div>

18
src/lib/database/food.ts Normal file
View File

@@ -0,0 +1,18 @@
import { db } from '$lib/database'
export type Food = {
rowid: number,
food: string,
amount: number,
per100: number,
energy: number,
}
const FoodService = {
async GetAll() {
return await db.select<Food[]>('SELECT rowid, food, amount, per100, energy FROM food ORDER BY date DESC')
},
async SetAll(data: Food[]) {}
}
export {FoodService}

View File

@@ -0,0 +1,3 @@
import Database from 'tauri-plugin-sql-api'
export const db = await Database.load('sqlite:food.db')