@varasto/fs-storage

Varasto storage implementation that stores data to hard disk

Usage no npm install needed!

<script type="module">
  import varastoFsStorage from 'https://cdn.skypack.dev/@varasto/fs-storage';
</script>

README

@varasto/fs-storage

Implementation of storage which stores data to hard disk.

Installation

$ npm install --save @varasto/fs-storage

Usage

The package provides an function called createFileSystemStorage, which returns an storage implementation that is capable of storing JSON objects into disk, where each object is identified by namespace and key, that must be valid URL slugs.

Basic usage of file system storage looks like this:

import { createFileSystemStorage } from '@varasto/fs-storage';

const storage = createFileSystemStorage({ dir: './data' });

The function takes an optional configuration object, which supports these settings:

Property Default value Description
dir ./data Directory where the items will be persisted into.
encoding utf-8 Character encoding to use when items are stored into disk.

If dir does not exist, it will be created when an item is placed into the storage.

Custom serializers

By default, JSON.stringify is used for serializing data written to file system and JSON.parse is used for deserializing data retrieved from file system. However, you can also use your own custom serialization functions by passing them as options to the createFileSystemStorage function.

import { createFileSystemStorage } from '@varasto/fs-storage';
import { JsonObject } from 'type-fest';

const storage = createFileSystemStorage({
  serialize: (data: string): JsonObject => ({}),
  deserialize: (data: JsonObject): string => "",
});