@bpleco/fsdb
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

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();
}

Readme

Keywords

none

Package Sidebar

Install

npm i @bpleco/fsdb

Weekly Downloads

0

Version

0.0.1

License

ISC

Unpacked Size

54.3 kB

Total Files

10

Last publish

Collaborators

  • bpleco