Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

789 B
Raw Blame
import type { Storage, StorageValue } from 'unstorage';

export async function getValues<T extends StorageValue>(storage: Storage<T>): Promise<T[]> {
  const keys = await storage.getKeys();

  const values = (await Promise.all(keys.map(async (key) => storage.getItem(key)))) as T[];

  return values;
}

export async function findOne<T extends StorageValue>(
  storage: Storage<T>,
  predicate: (value: T) => boolean,
): Promise<T | null> {
  const values = await getValues(storage);
  const found = values.find(predicate);

  return found ?? null;
}

export async function findMany<T extends StorageValue>(
  storage: Storage<T>,
  predicate: (value: T) => boolean,
): Promise<T[]> {
  const values = await getValues(storage);
  const found = values.filter(predicate);

  return found;
}