naughty-storage
Version:
Nodejs storage for inverting dependency of you application, same contract
79 lines (63 loc) • 2.4 kB
Markdown
[](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)
- Install: `npm install naughty-storage`
- Require: `const utils = require('naughty-storage')`
- 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();
};
```
```js
const storage = new FileStorage("collection");
await storage.pick("key", fs.readFile, "./path");
const output = await storage.get("key");
```
```js
const storage = new FileStorage("collection");
await storage.set("key", "some valuable string");
const data = await storage.get("key");
```
```js
const storage = new MemoryStorage("collection");
await storage.set("key", { greet: "hello" });
await storage.delete("key"); // boolean
```
```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}
}
```