0.2.1 • Published 6 months ago

naughty-storage v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Naughty Storage

license snyk npm version NPM Downloads NPM Downloads

Usage

  • Install: npm install naughty-storage
  • Require: const utils = require('naughty-storage')

Storages implement Map interface with async contract

  • Initial purpose to use different storages like - memory, file, and more to replace dependency in your project.
  • Also can be useful for convenient access to file system and much more.
  • Intially FileStorage saves data into root package folder (node_modules), to change this use File.location(path) static method in root project file

change FileStorage root storage location

const main = () => {
  //root project file
  FileStorage.location("./");
  await startDB();
  await startSomeServer();
};

pick - choose callback last err first contract function to put result into storage

const storage = new FileStorage("collection");
await storage.pick("key", fs.readFile, "./path");
const output = await storage.get("key");

get/set

const storage = new FileStorage("collection");
await storage.set("key", "some valuable string");
const data = await storage.get("key");

delete

const storage = new MemoryStorage("collection");
await storage.set("key", { greet: "hello" });
await storage.delete("key"); // boolean

iteration

const storage = FileStorage("collection");
await storage.set("key", {a: 1});
await storage.set("key-1", {b: 2});
await storage.set("key-2", {c: 3});

for await (const entry of storage) {
  // ["key", {a: 1}]
  // ["key-1", {b: 2}]
  // ["key-2", {c: 3}]
}

for await (const key of storage.keys()) {
  // "key"
  // "key-1"
  // "key-2"
}

for await (const value of storage.values()) {
  // {a: 1}
  // {b: 2}
  // {c: 3}
}

Part of naughty stack