Skip to content

Commit cef0ef0

Browse files
committed
[feat] new folder feature
1 parent bc4d74d commit cef0ef0

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/lib/data/dbStore.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { localStorageStore } from 'fractils'
55
import { liveQuery } from 'dexie'
66
import db from '$lib/data/db'
77

8+
export const folders = writable<Folder[]>()
89
export const activeFolder = writable<Folder>()
910

1011
//? Will always contain the current Folder's bookmarks
@@ -17,7 +18,7 @@ export const activeBookmarks = writable<Bookmark[]>()
1718

1819
export const lastActiveFolderId = localStorageStore('lastActiveFolderId', '')
1920

20-
export const uniqueTags: any = liveQuery(async () => await db.bookmarks.orderBy('tags').uniqueKeys())
21+
export const uniqueTags: any = liveQuery(() => db.bookmarks.orderBy('tags').uniqueKeys())
2122

2223
//? Filter bookmarks by tag
2324
export const tagFilter = writable<string | null>(null)

src/lib/data/transactions.ts

+49-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Bookmark, Folder } from './types'
22

3-
import { activeBookmarks, activeFolder, activeFolderBookmarks, tagFilter } from './dbStore'
3+
import { activeBookmarks, activeFolder, activeFolderBookmarks, folders, tagFilter } from './dbStore'
44
import { log, wait } from 'fractils'
55
import { get } from 'svelte/store'
66
import db from './db'
@@ -16,11 +16,12 @@ export async function init_db() {
1616
await wait(100)
1717
const id = localStorage.getItem('lastActiveFolderId')
1818

19-
//? Update stores
19+
//? Populate stores
2020
let lastActiveFolder: Folder | undefined
2121
lastActiveFolder = await db.table('folders').where('folder_id').equals(id).first()
2222
activeFolder.set(lastActiveFolder)
2323
activeBookmarks.set(await db.bookmarks.bulkGet(lastActiveFolder.bookmarks))
24+
folders.set(await db.table('folders').toArray())
2425
}
2526

2627

@@ -30,16 +31,16 @@ export async function init_db() {
3031
*/
3132
export async function newBookmark_db(bookmark: Bookmark) {
3233
log('🎬 Creating new bookmark: ', '#fa8', 'dimgray', 25)
33-
log(bookmark)
34-
3534
// Todo: Consolidate this into a single transaction?
3635

36+
$db.bookmarks = [...$db.bookmarks, bookmark]
37+
3738
//? Add to bookmarks table
3839
await db.bookmarks.add(bookmark)
3940

4041
//? Add id to activeFolder store
4142
activeFolder.update((f) => {
42-
f.bookmarks = [...f.bookmarks, bookmark.bookmark_id]
43+
f.bookmarks.push(bookmark.bookmark_id);
4344
return f
4445
})
4546

@@ -58,6 +59,31 @@ export async function newBookmark_db(bookmark: Bookmark) {
5859
}
5960

6061

62+
/**
63+
** Creates a new folder.
64+
* @param {folder} The bookmark to add.
65+
*/
66+
export async function newFolder_db(folder: Folder) {
67+
log('🎬 Creating new folder: ', '#fd8', '#333', 25)
68+
69+
// Todo: Consolidate this into a single transaction?
70+
71+
//? Clear any filters
72+
tagFilter.set(null)
73+
74+
//? Add to folders table
75+
await db.folders.add(folder)
76+
77+
//? Set activeFolder store
78+
activeFolder.set(folder)
79+
80+
//? Update activeBookmarks store
81+
activeBookmarks.set(await db.bookmarks.bulkGet(folder.bookmarks))
82+
83+
log('🏁 New Folder added', '#fd8', '#333', 25)
84+
}
85+
86+
6187
/**
6288
** Gets bookmark settings.
6389
* @param {id} The id of the bookmark to retrieve.
@@ -69,6 +95,24 @@ export async function getBookmark_db(id: Bookmark['bookmark_id']) {
6995
}
7096

7197

98+
/**
99+
** Gets folder settings.
100+
* @param {id} The id of the folder to retrieve.
101+
*/
102+
export async function getFolder_db(id: Folder['folder_id']) {
103+
log(`🎬 Getting folder with id of ${id}`, '#fa8', 'dimgray', 25)
104+
105+
return db.folders.where('folder_id').equals(id).first()
106+
}
107+
108+
109+
/**
110+
** Counts total folders.
111+
* @returns {Promise<number>} The number of folders in the database.
112+
*/
113+
export const getFolderCount_db = () => db.folders.count()
114+
115+
72116
/**
73117
** Updates the db after an index/position swap.
74118
*/

0 commit comments

Comments
 (0)