https://github.com/naughtysora/naughty-storage
Nodejs Storage- interfaces for inverting dependency of you application
https://github.com/naughtysora/naughty-storage
async dependency-inversion filesystem nodejs storage
Last synced: 11 months ago
JSON representation
Nodejs Storage- interfaces for inverting dependency of you application
- Host: GitHub
- URL: https://github.com/naughtysora/naughty-storage
- Owner: NaughtySora
- License: mit
- Created: 2025-01-21T18:48:12.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-26T12:01:09.000Z (over 1 year ago)
- Last Synced: 2025-03-26T14:42:03.816Z (about 1 year ago)
- Topics: async, dependency-inversion, filesystem, nodejs, storage
- Language: JavaScript
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# Naughty Storage
[](https://github.com/NaughtySora/naughty-storage/blob/master/LICENSE)
[](https://snyk.io/test/github/NaughtySora/naughty-storage)
[](https://badge.fury.io/js/naughty-storage)
[](https://www.npmjs.com/package/naughty-storage)
[](https://www.npmjs.com/package/naughty-storage)
## 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
```js
const main = () => {
//root project file
FileStorage.location("./");
await startDB();
await startSomeServer();
};
```
### pick - choose callback last err first contract function to put result into storage
```js
const storage = new FileStorage("collection");
await storage.pick("key", fs.readFile, "./path");
const output = await storage.get("key");
```
### get/set
```js
const storage = new FileStorage("collection");
await storage.set("key", "some valuable string");
const data = await storage.get("key");
```
### delete
```js
const storage = new MemoryStorage("collection");
await storage.set("key", { greet: "hello" });
await storage.delete("key"); // boolean
```
### iteration
```js
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