import {getDatabase} from './getDB';
import {databaseConf, possibleDBKeys} from './types';

export const getEntry = async <T2 extends databaseConf<T>, T>(
  schema: T2,
  key: possibleDBKeys,
): Promise<null | T> => {
  const dbName = schema.details.name;
  const realm = await getDatabase(dbName);

  const val = realm.objectForPrimaryKey<typeof schema.details.properties>(
    dbName,
    key,
  );

  return val as T;
};
