Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@eds-fw/storage

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eds-fw/storage

Tiny & Simply 'Map'-based storage: set(), get(), save() and more

latest
Source
npmnpm
Version
3.0.2
Version published
Weekly downloads
22
175%
Maintainers
1
Weekly downloads
 
Created
Source

Tiny & Simply 'Map'/'Array'-based storage with .save() method

Features

  • VERY easy to use
  • "One File - One Storage Exemplar" mechanism (bugs & multi-load protection)
  • Safe asynchronous save() (protection from multi-saving)
  • "Exact key types": you can only use the specified keys, e.g. day ${number} (day 6, day 293, day 32, etc., but not hour 7)
  • Additional Map methods: hasValue(), getKey(), filter() & easy JSON convertion
  • Pretty JSON format (depth=1)

API

  • class Storage <V? extends JSONSupported, K? extends string> default export
  • extends Map<K, V>
  • constructor (path: string, autosave?: boolean | number)
  • field path: string
  • hasValue (value: V): boolean
  • getKey (value: V, single?: boolean): K[]
  • filter (callbackfn: (value: V, key: K, map: Map<K, V>) => boolean): Map<K, V>
  • async save (): Promise<void>
  • getter asJSON: string
  • [Symbol.toStringTag]: string
  • static create <V? extends JSONSupported, K? extends string> (...params: ConstructorParameters<typeof Storage>): Storage<V, K>
  • static asJSON (map: Map<string, JSONSupported>): string
  • static field oneFile_oneStorage: boolean
  • class ArrayStorage <V? extends JSONSupported>
  • extends Array<V>
  • private constructor ()
  • field path: string
  • async save (): Promise<void>
  • getter asJSON: string
  • [Symbol.toStringTag]: string
  • static create <V? extends JSONSupported> (path: string, autosave?: boolean | number): ArrayStorage<V>
  • static asJSON (arr: JSONSupported[]): string
  • static field oneFile_oneStorage: boolean

Requirements

  • NodeJS, recommended v18 or newer

Setup

  • Install storage via npm:
npm i @eds-fw/storage
  • Use storage:
//file.js, type: CJS
const { Storage } = require("@eds-fw/storage");
const scores = new Storage(
    "./scores_data.json", //Warning! Path is calculated from CWD
    60_000 //Autosave timeout. Will be saved automatically every minute
);
scores.set("me", 186);
scores.save();
scores.set("john", 231);
if (storage.has("peter"))
    storage.delete("peter");
console.log(storage.asJSON());
/* Output (pretty):
{
    "me": 186,
    "john": 231
}
*/

Or, using TypeScript and "exact key types":

//file.ts
import { Storage } from "@eds-fw/storage";
type Keys_t = `score_${string}` | `bestScore_${string}`;
const scores = new Storage<number, Keys_t>(
    "./scores.json",
    60_000
);
scores.set("score_me", 132); //OK
scores.set("score_john", 452); //OK
scores.set("bestScore_me", 972); //OK
scores.set("previousScore_me", 274); //TypeScript Error

Source (git)

Issues (git)

Keywords

typescript

FAQs

Package last updated on 16 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts