0.0.1 • Published 12 months ago

@bpleco/fsdb v0.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
12 months ago

FSDB

File System DB is designed as a lightweight FS Database.

import { Document } from '@pitwall/fsdb';

interface SettingsSchema {
  x: boolean;
  testing: Partial<{ x: number; y: number }>;
}

const settings = new Document<SettingsSchema>({
  electronChildProcess: false,
  defaultValue: {
    testing: {
      x: 123,
    },
  },
});

const result = await settings.create({ x: false, testing: { x: 21 } });

await settings.update({ x: true, testing: { y: 42 } }, result.fileId);

const data = await settings.read(result.fileId);

console.log(data);

await settings.delete(result.fileId);

})();

Electron Child Processes are a first class citizen

import { Document } from '@pitwall/fsdb';
import { AppSettingsModel } from '@pitwall/types';
import { SetGameSettingsPayload } from 'src/shared/types';

try {
  // this is because the electron APIs are not avaible in the child process
  // its a tiny bit clunky but i think the best solution when you have state
  // that needs to be managed in both processes
  const { app } = require('electron');

  const dataPath = app.getPath('userData');

  process.env['FSDB_STORE_PATH'] = dataPath;
} catch (error) {}

export const settingsStore = new Document<AppSettingsModel>({
  electronChildProcess: true,
  fileId: 'settings',
  defaultValue: {
    currentGameSlug: 'acc',
    isInitialized: false,
    currentGame: 'acc',
    acc: {
      address: 'localhost',
      cmdPassword: '',
      gameTitle: 'Assetto Corsa Competizione',
      password: 'asd',
      port: 9000,
      slug: 'acc',
    },
  },
});

export async function setGameSettings(_event: any, settings: SetGameSettingsPayload) {
  const savedSettings = await settingsStore.read();
  await settingsStore.update({ [savedSettings.currentGameSlug]: { ...settings.settings } });
}

export async function getGameSettings() {
  return await settingsStore.read();
}
0.0.1

12 months ago