core: fix The item must contain the id field. error on migration

This commit is contained in:
Abdullah Atta
2024-03-23 06:27:59 +05:00
parent b1d13c125f
commit 0afcf8cbab
3 changed files with 21 additions and 12 deletions

View File

@@ -105,6 +105,8 @@ export class Settings implements ICollection {
value: SettingItemMap[TKey]
) {
const id = KEY_IDS[key];
if (!id) throw new Error(`Invalid settings key: ${key}.`);
const oldItem = this.collection.get(id);
if (oldItem && oldItem.key !== key) throw new Error("Key conflict.");

View File

@@ -25,14 +25,14 @@ import { makeId } from "./utils/id";
import {
Color,
ContentItem,
GroupingKey,
HistorySession,
Item,
ItemMap,
ItemType,
MaybeDeletedItem,
ToolbarConfigPlatforms,
isDeleted
isDeleted,
isGroupingKey
} from "./types";
import { isCipher } from "./database/crypto";
import { IndexedCollection } from "./database/indexed-collection";
@@ -390,9 +390,10 @@ const migrations: Migration[] = [
if (item.groupOptions) {
for (const key in item.groupOptions) {
const value = item.groupOptions[key as GroupingKey];
if (!isGroupingKey(key)) continue;
const value = item.groupOptions[key];
if (!value) continue;
await db.settings.setGroupOptions(key as GroupingKey, value);
await db.settings.setGroupOptions(key, value);
}
}
if (item.toolbarConfig) {

View File

@@ -40,14 +40,16 @@ export type GroupOptions = SortOptions & {
export type GroupedItems<T> = (T | GroupHeader)[];
export type GroupingKey =
| "home"
| "notes"
| "notebooks"
| "tags"
| "trash"
| "favorites"
| "reminders";
export const GroupingKey = [
"home",
"notes",
"notebooks",
"tags",
"trash",
"favorites",
"reminders"
] as const;
export type GroupingKey = (typeof GroupingKey)[number];
export type ValueOf<T> = T[keyof T];
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
@@ -552,6 +554,10 @@ export function isGroupHeader(item: any): item is GroupHeader {
return item.type === "header";
}
export function isGroupingKey(key: any): key is GroupingKey {
return GroupingKey.includes(key);
}
export type ContentBlock = {
content: string;
type: string;