1
1
import './style.css'
2
- import Dexie , { type EntityTable } from "dexie" ;
2
+ import Dexie , { type EntityTable , liveQuery } from "dexie" ;
3
3
4
4
interface Note {
5
5
id : number ;
@@ -18,27 +18,30 @@ db.version(1).stores({
18
18
notes : "++id, time, text, lat, lon" ,
19
19
} ) ;
20
20
21
- const form = document . querySelector ( "form" ) ! ;
22
- const posts = document . querySelector ( "ol" ) ! ;
21
+ const query = liveQuery ( ( ) => db . notes . orderBy ( "time" ) . reverse ( ) . toArray ( ) ) ;
23
22
24
- async function update ( ) {
25
- const notes = await db . notes . orderBy ( "time" ) . reverse ( ) . toArray ( ) ;
23
+ query . subscribe ( {
24
+ next ( notes ) {
25
+ console . log ( "Subs" , notes ) ;
26
26
27
- console . log ( notes ) ;
27
+ posts . innerHTML = "" ;
28
28
29
- posts . innerHTML = "" ;
29
+ for ( const note of notes ) {
30
+ const li = document . createElement ( "li" ) ;
30
31
31
- for ( const note of notes ) {
32
- const li = document . createElement ( "li" ) ;
32
+ const timestamp = new Date ( note . time ) ;
33
+ li . innerText = ` ${ note . text } ( ${ timestamp . toLocaleString ( ) } )` ;
33
34
34
- const timestamp = new Date ( note . time ) ;
35
- li . innerText = `${ note . text } (${ timestamp . toLocaleString ( ) } )` ;
36
-
37
- posts . appendChild ( li ) ;
38
- }
39
- }
35
+ posts . appendChild ( li ) ;
36
+ }
37
+ } ,
38
+ error ( err ) {
39
+ console . error ( err ) ;
40
+ } ,
41
+ } ) ;
40
42
41
- update ( ) ;
43
+ const form = document . querySelector ( "form" ) ! ;
44
+ const posts = document . querySelector ( "ol" ) ! ;
42
45
43
46
form ?. addEventListener ( "submit" , async ( e ) => {
44
47
e . preventDefault ( ) ;
@@ -68,8 +71,6 @@ form?.addEventListener("submit", async (e) => {
68
71
69
72
console . log ( noteId ) ;
70
73
71
- update ( ) ;
72
-
73
74
form . reset ( ) ;
74
75
} ) ;
75
76
0 commit comments