Skip to content

Commit

Permalink
Merge branch 'main' into pr/ascorbic/407
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 14, 2024
2 parents 3cd1fd7 + 85bdbb7 commit 2d446c8
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -24,7 +24,7 @@ Unstorage provides an async Key-Value storage API with conventional features lik
- Binary and raw value support
- State [snapshots](https://unstorage.unjs.io/getting-started/utils#snapshots) and hydration
- Storage watcher
- HTTP Storage with [built-in server](https://unstorage.unjs.io/getting-started/http-server)
- HTTP Storage with [built-in server](https://unstorage.unjs.io/guide/http-server)

## Usage

Expand Down
6 changes: 6 additions & 0 deletions docs/2.drivers/redis.md
Expand Up @@ -16,6 +16,12 @@ Learn more about Redis.
Unstorage uses [`ioredis`](https://github.com/luin/ioredis) internally to connect to Redis.
::

To use it, you will need to install `ioredis` in your project:

```bash [Terminal]
npm i ioredis
```

Usage with single Redis instance:

```js
Expand Down
8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -47,7 +47,6 @@
"chokidar": "^3.6.0",
"destr": "^2.0.3",
"h3": "^1.11.1",
"ioredis": "^5.3.2",
"listhen": "^1.7.2",
"lru-cache": "^10.2.0",
"mri": "^1.2.0",
Expand Down Expand Up @@ -80,6 +79,7 @@
"eslint-config-unjs": "^0.2.1",
"fake-indexeddb": "^5.0.2",
"idb-keyval": "^6.2.1",
"ioredis": "^5.3.2",
"ioredis-mock": "^8.9.0",
"jiti": "^1.21.0",
"jsdom": "^24.0.0",
Expand Down Expand Up @@ -107,7 +107,8 @@
"@planetscale/database": "^1.16.0",
"@upstash/redis": "^1.28.4",
"@vercel/kv": "^0.2.4",
"idb-keyval": "^6.2.1"
"idb-keyval": "^6.2.1",
"ioredis": "^5.3.2"
},
"peerDependenciesMeta": {
"@azure/app-configuration": {
Expand Down Expand Up @@ -145,6 +146,9 @@
},
"idb-keyval": {
"optional": true
},
"ioredis": {
"optional": true
}
},
"packageManager": "pnpm@8.15.4"
Expand Down
19 changes: 16 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/drivers/vercel-kv.ts
Expand Up @@ -21,6 +21,8 @@ export interface VercelKVOptions extends Partial<RedisConfigNodejs> {
ttl?: number;
}

const DRIVER_NAME = "vercel-kv";

export default defineDriver<VercelKVOptions>((opts) => {
const base = normalizeKey(opts?.base);
const r = (...keys: string[]) => joinKeys(base, ...keys);
Expand Down Expand Up @@ -60,6 +62,7 @@ export default defineDriver<VercelKVOptions>((opts) => {
};

return {
name: DRIVER_NAME,
hasItem(key) {
return getClient().exists(r(key)).then(Boolean);
},
Expand Down
2 changes: 1 addition & 1 deletion src/storage.ts
Expand Up @@ -231,7 +231,7 @@ export function createStorage<T extends StorageValue>(
async setItems(items, commonOptions) {
await runBatch(items, commonOptions, async (batch) => {
if (batch.driver.setItems) {
await asyncCall(
return asyncCall(
batch.driver.setItems,
batch.items.map((item) => ({
key: item.relativeKey,
Expand Down
30 changes: 30 additions & 0 deletions test/storage.test.ts
Expand Up @@ -160,3 +160,33 @@ describe("utils", () => {
expect(async () => await storage.setItem("foo", [])).not.toThrow();
});
});

describe("Regression", () => {
it("setItems doeesn't upload twice", async () => {
/**
* https://github.com/unjs/unstorage/pull/392
*/

const setItem = vi.fn();
const setItems = vi.fn();

const driver = memory();
const storage = createStorage({
driver: {
...driver,
setItem: (...args) => {
setItem(...args);
return driver.setItem?.(...args);
},
setItems: (...args) => {
setItems(...args);
return driver.setItems?.(...args);
},
},
});

await storage.setItems([{ key: "foo.txt", value: "bar" }]);
expect(setItem).toHaveBeenCalledTimes(0);
expect(setItems).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 2d446c8

Please sign in to comment.