item handling

This commit is contained in:
2025-06-16 22:06:15 +02:00
parent 0b8f2c5532
commit 2972bb9e91
63 changed files with 3975 additions and 979 deletions

View File

@ -1,10 +1,14 @@
import { eq, isNull } from 'drizzle-orm'
import {
haexPasswordsGroupItems,
haexPasswordsGroups,
haexPasswordsItems,
type InsertHaexPasswordsItems,
type InsertHaexPasswordsItemsKeyValues,
haexPasswordsItemDetails,
haexPasswordsItemHistory,
haexPasswordsItemKeyValues,
type InsertHaexPasswordsItemDetails,
type InserthaexPasswordsItemKeyValues,
type SelectHaexPasswordsGroups,
type SelectHaexPasswordsItemDetails,
type SelectHaexPasswordsItemKeyValues,
} from '~~/src-tauri/database/schemas/vault'
export const usePasswordItemStore = defineStore('passwordItemStore', () => {
@ -17,34 +21,114 @@ export const usePasswordItemStore = defineStore('passwordItemStore', () => {
},
})
const currentItem = computedAsync(
async () => await readAsync(currentItemId.value),
)
return {
currentItemId,
currentItem,
addAsync,
addKeyValueAsync,
addKeyValuesAsync,
deleteAsync,
deleteKeyValueAsync,
readByGroupIdAsync,
readAsync,
readKeyValuesAsync,
updateAsync,
}
})
const addAsync = async (
item: InsertHaexPasswordsItems,
keyValues: InsertHaexPasswordsItemsKeyValues,
details: SelectHaexPasswordsItemDetails,
keyValues: SelectHaexPasswordsItemKeyValues[],
group?: SelectHaexPasswordsGroups | null,
) => {
const { currentVault } = useVaultStore()
/* const { currentGroupId } = useVaultGroupStore();
console.log('addItem', details, group)
entry.id = crypto.randomUUID();
entry.createdAt = null;
entry.updateAt = null;
console.log('store create entry', entry, currentGroupId);
await currentVault?.drizzle.transaction(async (tx) => {
await tx.insert(vaultEntry).values(entry);
await tx
.insert(vaultGroupEntry)
.values({ entryId: entry.id, groupId: currentGroupId });
});
const newDetails: InsertHaexPasswordsItemDetails = {
id: crypto.randomUUID(),
icon: details.icon || group?.icon || null,
note: details.note,
password: details.password,
tags: details.tags,
title: details.title,
url: details.url,
username: details.username,
}
return entry.id; */
const newKeyValues: InserthaexPasswordsItemKeyValues[] = keyValues.map(
(keyValue) => ({
id: crypto.randomUUID(),
itemId: newDetails.id,
key: keyValue.key,
value: keyValue.value,
}),
)
try {
await currentVault?.drizzle.transaction(async (tx) => {
await tx.insert(haexPasswordsItemDetails).values(newDetails)
await tx
.insert(haexPasswordsGroupItems)
.values({ itemId: newDetails.id, groupId: group?.id ?? null })
if (newKeyValues.length)
await tx.insert(haexPasswordsItemKeyValues).values(newKeyValues)
})
} catch (error) {
console.error('ERROR addItem', error)
}
return newDetails.id
}
const addKeyValueAsync = async (
item?: InserthaexPasswordsItemKeyValues | null,
itemId?: string,
) => {
const newKeyValue: InserthaexPasswordsItemKeyValues = {
id: crypto.randomUUID(),
itemId: item?.itemId || itemId,
key: item?.key,
value: item?.value,
}
try {
const { currentVault } = useVaultStore()
return await currentVault?.drizzle
.insert(haexPasswordsItemKeyValues)
.values(newKeyValue)
} catch (error) {
console.error('ERROR addItem', error)
}
}
const addKeyValuesAsync = async (
items: InserthaexPasswordsItemKeyValues[],
itemId?: string,
) => {
const { currentVault } = useVaultStore()
console.log('addKeyValues', items, itemId)
const newKeyValues: InserthaexPasswordsItemKeyValues[] = items?.map(
(item) => ({
id: crypto.randomUUID(),
itemId: item.itemId || itemId,
key: item.key,
value: item.value,
}),
)
try {
return await currentVault?.drizzle
.insert(haexPasswordsItemKeyValues)
.values(newKeyValues)
} catch (error) {
console.error('ERROR addItem', error)
}
}
const readByGroupIdAsync = async (groupId?: string | null) => {
@ -58,25 +142,25 @@ const readByGroupIdAsync = async (groupId?: string | null) => {
.select()
.from(haexPasswordsGroupItems)
.innerJoin(
haexPasswordsItems,
eq(haexPasswordsItems.id, haexPasswordsGroupItems.itemId),
haexPasswordsItemDetails,
eq(haexPasswordsItemDetails.id, haexPasswordsGroupItems.itemId),
)
.where(eq(haexPasswordsGroupItems.groupId, groupId))
console.log('found entries by groupId', entries)
return entries.map((entry) => entry.haex_passwords_items)
return entries.map((entry) => entry.haex_passwords_item_details)
} else {
const entries = await currentVault.drizzle
.select()
.from(haexPasswordsGroupItems)
.innerJoin(
haexPasswordsItems,
eq(haexPasswordsItems.id, haexPasswordsGroupItems.itemId),
haexPasswordsItemDetails,
eq(haexPasswordsItemDetails.id, haexPasswordsGroupItems.itemId),
)
.where(isNull(haexPasswordsGroupItems.groupId))
console.log('found entries', entries)
return entries.map((entry) => entry.haex_passwords_items)
return entries.map((entry) => entry.haex_passwords_item_details)
}
} catch (error) {
console.error(error)
@ -91,11 +175,11 @@ const readAsync = async (itemId: string | null) => {
const { currentVault } = useVaultStore()
const details =
await currentVault.drizzle.query.haexPasswordsItems.findFirst({
where: eq(haexPasswordsItems.id, itemId),
await currentVault.drizzle.query.haexPasswordsItemDetails.findFirst({
where: eq(haexPasswordsItemDetails.id, itemId),
})
if (!details) return {}
if (!details) return null
const history = (await usePasswordHistoryStore().getAsync(itemId)) ?? []
const keyValues = (await readKeyValuesAsync(itemId)) ?? []
@ -113,8 +197,127 @@ const readKeyValuesAsync = async (itemId: string | null) => {
const { currentVault } = useVaultStore()
const keyValues =
await currentVault.drizzle.query.haexPasswordsItemsKeyValues.findMany({
where: eq(haexPasswordsItems.id, itemId),
await currentVault.drizzle.query.haexPasswordsItemKeyValues.findMany({
where: eq(haexPasswordsGroupItems.itemId, itemId),
})
return keyValues
}
const updateAsync = async ({
details,
keyValues,
keyValuesAdd,
keyValuesDelete,
groupId,
}: {
details: SelectHaexPasswordsItemDetails
keyValues: SelectHaexPasswordsItemKeyValues[]
keyValuesAdd: SelectHaexPasswordsItemKeyValues[]
keyValuesDelete: SelectHaexPasswordsItemKeyValues[]
groupId: string | null
}) => {
const { currentVault } = useVaultStore()
if (!details.id) return
const newDetails: InsertHaexPasswordsItemDetails = {
id: details.id,
icon: details.icon,
note: details.note,
password: details.password,
tags: details.tags,
title: details.title,
url: details.url,
username: details.username,
}
const newKeyValues: InserthaexPasswordsItemKeyValues[] = keyValues
.map((keyValue) => ({
id: keyValue.id,
itemId: newDetails.id,
key: keyValue.key,
value: keyValue.value,
}))
.filter((keyValue) => keyValue.id)
const newKeyValuesAdd: InserthaexPasswordsItemKeyValues[] = keyValuesAdd.map(
(keyValue) => ({
id: keyValue.id || crypto.randomUUID(),
itemId: newDetails.id,
key: keyValue.key,
value: keyValue.value,
}),
)
console.log('update item', newDetails, newKeyValues, newKeyValuesAdd, groupId)
return await currentVault?.drizzle.transaction(async (tx) => {
await tx
.update(haexPasswordsItemDetails)
.set(newDetails)
.where(eq(haexPasswordsItemDetails.id, newDetails.id))
await tx
.update(haexPasswordsGroupItems)
.set({ itemId: newDetails.id, groupId })
.where(eq(haexPasswordsGroupItems.itemId, newDetails.id))
const promises = newKeyValues.map((keyValue) =>
tx
.update(haexPasswordsItemKeyValues)
.set(keyValue)
.where(eq(haexPasswordsItemKeyValues.id, keyValue.id)),
)
await Promise.all(promises)
if (newKeyValuesAdd.length)
await tx.insert(haexPasswordsItemKeyValues).values(newKeyValuesAdd)
const promisesDelete = keyValuesDelete.map((keyValue) =>
tx
.delete(haexPasswordsItemKeyValues)
.where(eq(haexPasswordsItemKeyValues.id, keyValue.id)),
)
await Promise.all(promisesDelete)
return newDetails.id
})
}
const deleteAsync = async (itemId: string, final: boolean = false) => {
const { currentVault } = useVaultStore()
const { createTrashIfNotExistsAsync, trashId } = usePasswordGroupStore()
console.log('deleteAsync', itemId, final)
if (final)
await currentVault?.drizzle.transaction(async (tx) => {
await tx
.delete(haexPasswordsItemKeyValues)
.where(eq(haexPasswordsItemKeyValues.itemId, itemId))
await tx
.delete(haexPasswordsItemHistory)
.where(eq(haexPasswordsItemHistory.itemId, itemId))
await tx
.delete(haexPasswordsGroupItems)
.where(eq(haexPasswordsGroupItems.itemId, itemId))
await tx
.delete(haexPasswordsItemDetails)
.where(eq(haexPasswordsItemDetails.id, itemId))
})
else {
if (await createTrashIfNotExistsAsync())
await currentVault.drizzle
.update(haexPasswordsGroupItems)
.set({ groupId: trashId })
.where(eq(haexPasswordsGroupItems.itemId, itemId))
}
}
const deleteKeyValueAsync = async (id: string) => {
console.log('deleteKeyValueAsync', id)
const { currentVault } = useVaultStore()
return await currentVault.drizzle
.delete(haexPasswordsItemKeyValues)
.where(eq(haexPasswordsItemKeyValues.id, id))
}