@varasto/single-file-storage
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

@varasto/single-file-storage

npm

Implementation of storage which stores all data into a single file, in flat-file database kind of fashion. Because of it's design, it's quite inefficient way of storing data and fs-storage should be used instead whenever possible.

Installation

$ npm install --save @varasto/single-file-storage

Usage

The package provides an function called createSingleFileStorage, 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. These objects are then combined into single large JSON object that is stored to an file on disk.

Basic usage of single file storage looks like this:

import { createSingleFileStorage } from '@varasto/single-file-storage';

const storage = createSingleFileStorage({ path: './data.json' });

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

Property Default value Description
path ./data.json Path to the file where data will be stored into.
encoding utf-8 Character encoding to use when data is stored into disk.

If path 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 createSingleFileStorage function.

import { createSingleFileStorage } from '@varasto/single-file-storage';
import { JsonObject } from 'type-fest';

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

Readme

Keywords

Package Sidebar

Install

npm i @varasto/single-file-storage

Weekly Downloads

12

Version

3.0.0

License

MIT

Unpacked Size

14 kB

Total Files

10

Last publish

Collaborators

  • rauli