Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
import { scopedKey } from '../ItemRef';
import type { LogoKind, Item, ItemField, ItemLogo, ItemTagRef, ItemType } from '@aliasvault/models/vault';
/**
* Item with optional DeletedAt field for recently deleted items.
*/
export type ItemWithDeletedAt = Item & { DeletedAt?: string };
/**
* Item with its ArchivedAt timestamp, for the archived list.
*/
export type ItemWithArchivedAt = Item & { ArchivedAt?: string };
/**
* Raw item row from database query.
*/
export type ItemRow = {
Id: string;
/** The manifest the item belongs to; the other half of its key. */
ManifestId: string;
Name: string;
ItemType: string;
FolderId: string | null;
Logo: Uint8Array | null;
LogoId: string | null;
LogoKind: string | null;
LogoSource: string | null;
LogoName: string | null;
HasPasskey: number;
HasAttachment: number;
HasTotp: number;
CreatedAt: string;
UpdatedAt: string;
DeletedAt?: string | null;
ArchivedAt?: string | null;
}
/**
* An active item as the folder counts need it: its key, its folder and the flags the type filters read.
*/
export type ItemSummary = Pick<Item, 'Id' | 'ManifestId' | 'ItemType' | 'FolderId' | 'HasPasskey' | 'HasAttachment' | 'HasTotp'>;
/**
* Raw summary row from database query.
*/
export type ItemSummaryRow = Pick<ItemRow, 'Id' | 'ManifestId' | 'ItemType' | 'FolderId' | 'HasPasskey' | 'HasAttachment' | 'HasTotp'>;
/**
* Raw tag row from database query.
*/
export type TagRow = {
ItemId: string;
/** The manifest of the item this tag is attached to. */
ManifestId: string;
Id: string;
Name: string;
Color: string | null;
}
/**
* Mapper class for converting database rows to Item objects.
*/
export class ItemMapper {
/**
* Map a single database row to an Item object.
* @param row - Raw item row from database
* @param fields - Processed fields for this item
* @param tags - Tags for this item
* @param folderPath - Computed folder path array (optional)
* @returns Item object
*/
public static mapRow(
row: ItemRow,
fields: ItemField[] = [],
tags: ItemTagRef[] = [],
folderPath?: string[]
): Item {
return {
Id: row.Id,
ManifestId: row.ManifestId,
Name: row.Name,
ItemType: row.ItemType as ItemType,
Logo: row.Logo ?? undefined,
LogoInfo: this.mapLogo(row),
FolderId: row.FolderId,
FolderPath: folderPath,
Tags: tags,
Fields: fields,
HasPasskey: row.HasPasskey === 1,
HasAttachment: row.HasAttachment === 1,
HasTotp: row.HasTotp === 1,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt
};
}
/**
* Map multiple database rows to Item objects with their fields and tags.
* @param rows - Raw item rows from database
* @param fieldsByItem - Map of scoped item key to array of fields
* @param tagsByItem - Map of scoped item key to array of tags
* @param folderPathsByFolderId - Map of scoped folder key to folder path array (optional)
* @returns Array of Item objects
*/
public static mapRows(
rows: ItemRow[],
fieldsByItem: Map<string, ItemField[]>,
tagsByItem: Map<string, ItemTagRef[]>,
folderPathsByFolderId?: Map<string, string[]>
): Item[] {
return rows.map(row => this.mapRow(
row,
fieldsByItem.get(scopedKey(row.ManifestId, row.Id)) || [],
tagsByItem.get(scopedKey(row.ManifestId, row.Id)) || [],
row.FolderId && folderPathsByFolderId ? folderPathsByFolderId.get(scopedKey(row.ManifestId, row.FolderId)) : undefined
));
}
/**
* Map raw summary rows to ItemSummary objects.
* @param rows - Raw summary rows from database
* @returns Array of ItemSummary objects
*/
public static mapSummaryRows(rows: ItemSummaryRow[]): ItemSummary[] {
return rows.map(row => ({
Id: row.Id,
ManifestId: row.ManifestId,
ItemType: row.ItemType as ItemType,
FolderId: row.FolderId,
HasPasskey: row.HasPasskey === 1,
HasAttachment: row.HasAttachment === 1,
HasTotp: row.HasTotp === 1
}));
}
/**
* Map the joined logo columns of a row into the item's logo, or undefined when it has none.
* A row written before the Kind column existed reads as a favicon, which is what it was.
* @param row - Raw item row from database
* @returns The item's logo, or undefined
*/
private static mapLogo(row: ItemRow): ItemLogo | undefined {
if (!row.LogoId || !row.LogoSource) {
return undefined;
}
return {
Id: row.LogoId,
Kind: (row.LogoKind ?? 'favicon') as LogoKind,
Source: row.LogoSource,
Name: row.LogoName
};
}
/**
* Group tag rows by their item's scoped key into a map.
* @param tagRows - Raw tag rows from database
* @returns Map of scoped item key to array of ItemTagRef
*/
public static groupTagsByItem(tagRows: TagRow[]): Map<string, ItemTagRef[]> {
const tagsByItem = new Map<string, ItemTagRef[]>();
for (const tag of tagRows) {
const key = scopedKey(tag.ManifestId, tag.ItemId);
if (!tagsByItem.has(key)) {
tagsByItem.set(key, []);
}
tagsByItem.get(key)!.push({
Id: tag.Id,
Name: tag.Name,
Color: tag.Color || undefined
});
}
return tagsByItem;
}
/**
* Map tag rows to ItemTagRef array (for single item).
* @param tagRows - Raw tag rows without ItemId
* @returns Array of ItemTagRef
*/
public static mapTagRows(tagRows: Omit<TagRow, 'ItemId' | 'ManifestId'>[]): ItemTagRef[] {
return tagRows.map(tag => ({
Id: tag.Id,
Name: tag.Name,
Color: tag.Color || undefined
}));
}
/**
* Map a single item row for recently deleted items (includes DeletedAt).
* @param row - Raw item row with DeletedAt
* @param fields - Processed fields for this item
* @param folderPath - Computed folder path array (optional)
* @returns Item object with DeletedAt
*/
public static mapDeletedItemRow(
row: ItemRow & { DeletedAt: string },
fields: ItemField[] = [],
folderPath?: string[]
): ItemWithDeletedAt {
return {
Id: row.Id,
ManifestId: row.ManifestId,
Name: row.Name,
ItemType: row.ItemType as ItemType,
Logo: row.Logo ? new Uint8Array(row.Logo) : undefined,
LogoInfo: this.mapLogo(row),
FolderId: row.FolderId,
FolderPath: folderPath,
DeletedAt: row.DeletedAt,
HasPasskey: row.HasPasskey === 1,
HasAttachment: row.HasAttachment === 1,
HasTotp: row.HasTotp === 1,
Fields: fields,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt
};
}
}