Skip to content

Commit

Permalink
fix: enhance database job restoration process and UI components
Browse files Browse the repository at this point in the history
- Added a unique constraint to the 'tag' model in the Prisma schema to ensure data integrity.
- Updated the opacity of the background in the CollapsibleCard component for improved visual clarity.
- Refactored the RestoreDB method in the DBJob class to optimize account handling and note restoration, including the use of a map to track accounts and streamline the upsert process for notes and attachments.

These changes improve data consistency, enhance user interface elements, and optimize the database restoration workflow.
  • Loading branch information
blinko-space committed Dec 16, 2024
1 parent 6e49bcf commit 121755a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/components/Common/CollapsibleCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const CollapsibleCard = ({

return (
<Card shadow="none" className={`flex flex-col p-4 bg-background relative ${className}`}>
<div className="absolute inset-0 overflow-hidden opacity-[0.02] pointer-events-none">
<div className="absolute inset-0 overflow-hidden opacity-[0.03] pointer-events-none">
<RandomIcon className="right-[10%] bottom-[20%]" />
<RandomIcon className="left-[15%] top-[25%]" />
<RandomIcon className="right-[30%] top-[15%]" />
Expand Down
126 changes: 63 additions & 63 deletions src/server/plugins/dbjob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class DBJob {
}
}

static async *RestoreDB(filePath: string, ctx: any): AsyncGenerator<RestoreResult & { progress: { current: number; total: number } }, void, unknown> {
static async *RestoreDB(filePath: string, ctx: Context): AsyncGenerator<RestoreResult & { progress: { current: number; total: number } }, void, unknown> {
try {
const zip = new AdmZip(filePath);
zip.extractAllTo(ROOT_PATH, true);
Expand All @@ -87,90 +87,90 @@ export class DBJob {

const attachmentsCount = backupData.notes.reduce((acc, note) =>
acc + (note.attachments?.length || 0), 0);
const total = backupData.notes.length + attachmentsCount - 1;
const total = backupData.notes.length + attachmentsCount;
let current = 0;

const accountMap = new Map();
for (const note of backupData.notes) {
current++;
try {
const userCaller = createCaller(ctx)
if (!accountMap.has(note.account.name)) {
const account = await prisma.accounts.findFirst({
where: { name: note.account.name }
});

const createdNote = await userCaller.notes.upsert({
content: note.content,
isArchived: note.isArchived,
type: note.type,
isTop: note.isTop,
isShare: note.isShare,
})
if (!account) {
const newAccount = await prisma.accounts.create({
data: {
name: note.account.name,
password: note.account.password,
role: note.account.role
}
});
accountMap.set(note.account.name, newAccount);
} else {
accountMap.set(note.account.name, account);
}
}
}

if (createdNote.id) {
const account = await prisma.accounts.findFirst({
where: { name: note.account.name }
})
let updateData: any = {
createdAt: note.createdAt,
updatedAt: note.updatedAt,
for (const note of backupData.notes) {
current++;
try {
await prisma.$transaction(async (tx) => {
let ctx = {
name: accountMap.get(note.account.name).name,
sub: accountMap.get(note.account.name).id,
role: accountMap.get(note.account.name).role,
id: accountMap.get(note.account.name).id,
exp: 0,
iat: 0,
}
if (!account) {
const _newAccount = await prisma.accounts.create({
const userCaller = createCaller(ctx);
const createdNote = await userCaller.notes.upsert({
content: note.content,
isArchived: note.isArchived,
type: note.type,
isTop: note.isTop,
isShare: note.isShare,
});

if (createdNote.id) {
const account = accountMap.get(note.account.name);
await tx.notes.update({
where: { id: createdNote.id },
data: {
name: note.account.name,
password: note.account.password,
role: 'user'
accountId: account.id,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
}
})
updateData.accountId = _newAccount.id
} else {
updateData.accountId = account.id
});
if (note.attachments?.length) {
const attachmentData = note.attachments.map(attachment => ({
...attachment,
noteId: createdNote.id
}));
await tx.attachments.createMany({
data: attachmentData,
skipDuplicates: true
});
current += note.attachments.length;
}
}
await prisma.notes.update({
where: { id: createdNote.id },
data: updateData
});
}
});

yield {
type: 'success',
content: note.content.slice(0, 30),
content: note.account.name + ' - ' + note.content.slice(0, 30),
progress: { current, total }
};

if (note.attachments?.length) {
for (const attachment of note.attachments) {
current++;
try {
const existingAttachment = await prisma.attachments.findFirst({
where: { name: attachment.name }
});
await prisma.attachments.create({
data: {
...attachment,
noteId: createdNote.id
}
});

yield {
type: 'success',
content: attachment.name,
progress: { current, total }
};
} catch (error) {
yield {
type: 'error',
content: attachment.name,
error,
progress: { current, total }
};
}
}
}
} catch (error) {
yield {
type: 'error',
content: note.content.slice(0, 30),
error,
progress: { current, total }
};
continue;
}
}
} catch (error) {
Expand Down

0 comments on commit 121755a

Please sign in to comment.