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

import Realm from 'realm';

export const setEntry = async <T2 extends databaseConf<T, any>, T>(
  schema: T2,
  value: T,
  suffix?: string,
) => {
  const nameObj = {name: schema.details.name, suffix};
  const dbName = mergeDBName(nameObj);

  const realm = await getDatabase(nameObj);

  realm.write(() => {
    realm.create(nameObj.name, value as any, Realm.UpdateMode.Modified);
  });
};
