Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add null driver #495

Merged
merged 6 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/2.drivers/null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
icon: bi:trash3-fill
---

# Null

> Discards all data.

::warning
This driver does NOT store any data. It will discard any data written to it and will always return null similar to [`/dev/null`](https://en.wikipedia.org/wiki/Null_device)
::

```js
import { createStorage } from "unstorage";
import nullDriver from "unstorage/drivers/null";

const storage = createStorage({
driver: nullDriver(),
});
```
32 changes: 32 additions & 0 deletions src/drivers/null.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { defineDriver } from "./utils";

const DRIVER_NAME = "null";

export default defineDriver<void>(() => {
return {
name: DRIVER_NAME,
hasItem() {
return false;
},
getItem() {
return null;
},
getItemRaw() {
return null;
},
getItems() {
return [];
},
getMeta() {
return null;
},
getKeys() {
return [];
},
setItem() {},
setItemRaw() {},
setItems() {},
removeItem() {},
clear() {},
};
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const builtinDrivers = {
memory: "unstorage/drivers/memory",
mongodb: "unstorage/drivers/mongodb",
netlifyBlobs: "unstorage/drivers/netlify-blobs",
null: "unstorage/drivers/null",
overlay: "unstorage/drivers/overlay",
planetscale: "unstorage/drivers/planetscale",
redis: "unstorage/drivers/redis",
Expand Down
13 changes: 13 additions & 0 deletions test/drivers/null.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, it } from "vitest";
import driver from "../../src/drivers/null";
import { createStorage } from "../../src";

describe("drivers: null", async () => {
const storage = createStorage({ driver: driver() });
it("setItem", async () => {
await storage.setItem("key", "value");
});
it("getItem", async () => {
expect(await storage.getItem("key")).toEqual(null);
});
});
Loading