Skip to content

Commit e7fd73c

Browse files
authored
keyv - feat: (breaking) removing opts from KeyvStorageAdapter (#1906)
* keyv - feat: (breaking) removing opts from KeyvStorageAdapter * removing opts fixes
1 parent 03d050b commit e7fd73c

File tree

36 files changed

+222
-527
lines changed

36 files changed

+222
-527
lines changed

core/keyv/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ The storage adapter instance to be used by Keyv.
538538
Type: `String`
539539
Default: `'keyv'`
540540

541-
This is the namespace for the current instance. When you set it it will set it also on the storage adapter. This is the preferred way to set the namespace over `.opts.namespace`.
541+
This is the namespace for the current instance. When you set it it will set it also on the storage adapter.
542542

543543
## options
544544

@@ -630,8 +630,10 @@ await keyv.setRaw('foo', { value: 'bar' });
630630

631631
// Round-trip: get raw, modify, set raw
632632
const raw = await keyv.getRaw('foo');
633-
raw.value = 'updated';
634-
await keyv.setRaw('foo', raw);
633+
if (raw) {
634+
raw.value = 'updated';
635+
await keyv.setRaw('foo', raw);
636+
}
635637
```
636638

637639
## .setManyRaw(entries)

core/keyv/src/generic-store.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ export type KeyPrefixData = {
9393
* ```
9494
*/
9595
export class KeyvGenericStore extends Hookified implements KeyvStorageAdapter {
96-
private readonly _options?: KeyvGenericStoreOptions;
9796
private _store: KeyvMapType;
9897
private _namespace?: string | (() => string);
9998
private _keySeparator = "::";
@@ -106,7 +105,6 @@ export class KeyvGenericStore extends Hookified implements KeyvStorageAdapter {
106105
constructor(store: KeyvMapType, options?: KeyvGenericStoreOptions) {
107106
super();
108107
this._store = store;
109-
this._options = options;
110108

111109
if (options?.keySeparator) {
112110
this._keySeparator = options.keySeparator;
@@ -145,13 +143,6 @@ export class KeyvGenericStore extends Hookified implements KeyvStorageAdapter {
145143
this._keySeparator = separator;
146144
}
147145

148-
/**
149-
* Gets the options passed to the constructor.
150-
*/
151-
public get opts() {
152-
return this._options;
153-
}
154-
155146
/**
156147
* Gets the current namespace value (resolved if it's a function).
157148
*/

core/keyv/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ export type StoredDataRaw<Value> = DeserializedData<Value> | undefined;
9595
export type StoredData<Value> = StoredDataNoRaw<Value> | StoredDataRaw<Value>;
9696

9797
export type KeyvStorageAdapter = {
98-
// biome-ignore lint/suspicious/noExplicitAny: type format
99-
opts: any;
10098
namespace?: string | undefined;
10199
get<Value>(key: string): Promise<StoredData<Value> | undefined>;
102100
// biome-ignore lint/suspicious/noExplicitAny: type format

core/keyv/test/generic-store-integration.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ describe("KeyvGenericStore with QuickLRU - Store Options", () => {
3939

4040
test("should be able to get the options", () => {
4141
const lru = new QuickLRU<string, unknown>({ maxSize: 100 });
42-
const options = { namespace: "test", keySeparator: ":" };
43-
const keyv = new KeyvGenericStore(lru, options);
44-
expect(keyv.opts).toEqual(options);
42+
const keyv = new KeyvGenericStore(lru, {
43+
namespace: "test",
44+
keySeparator: ":",
45+
});
46+
expect(keyv.namespace).toBe("test");
47+
expect(keyv.keySeparator).toBe(":");
4548
});
4649
});
4750

@@ -689,9 +692,12 @@ describe("KeyvGenericStore with lru.min - Store Options", () => {
689692

690693
test("should be able to get the options", () => {
691694
const lru = createLRU<string, unknown>({ max: 100 });
692-
const options = { namespace: "test", keySeparator: ":" };
693-
const keyv = new KeyvGenericStore(lru, options);
694-
expect(keyv.opts).toEqual(options);
695+
const keyv = new KeyvGenericStore(lru, {
696+
namespace: "test",
697+
keySeparator: ":",
698+
});
699+
expect(keyv.namespace).toBe("test");
700+
expect(keyv.keySeparator).toBe(":");
695701
});
696702
});
697703

@@ -1128,9 +1134,12 @@ describe("KeyvGenericStore with Map - Store Options", () => {
11281134

11291135
test("should be able to get the options", () => {
11301136
const map = new Map<string, unknown>();
1131-
const options = { namespace: "test", keySeparator: ":" };
1132-
const keyv = new KeyvGenericStore(map, options);
1133-
expect(keyv.opts).toEqual(options);
1137+
const keyv = new KeyvGenericStore(map, {
1138+
namespace: "test",
1139+
keySeparator: ":",
1140+
});
1141+
expect(keyv.namespace).toBe("test");
1142+
expect(keyv.keySeparator).toBe(":");
11341143
});
11351144
});
11361145

core/keyv/test/generic-store.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ describe("Keyv Generic Store Options", () => {
3030
expect(keyv.keySeparator).toBe("new");
3131
});
3232

33-
test("should be able to get the options", () => {
33+
test("should be able to get the namespace from options", () => {
3434
const store = new Map();
35-
const options = { namespace: "test" };
36-
const keyv = new KeyvGenericStore(store, options);
37-
expect(keyv.opts).toEqual(options);
35+
const keyv = new KeyvGenericStore(store, { namespace: "test" });
36+
expect(keyv.namespace).toBe("test");
3837
});
3938
});
4039

core/test-suite/test/unit.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,9 @@ import keyvTestSuite, {
77
} from "../src/index.js";
88

99
const storeExtended = () => {
10-
class MapExtend extends Map {
11-
// biome-ignore lint/correctness/noUnusedPrivateClassMembers: allowed
12-
private readonly opts: { timeout: number };
13-
// biome-ignore lint/suspicious/noExplicitAny: type format for Map
14-
constructor(map: Map<any, any>, options: { timeout: number }) {
15-
// @ts-expect-error - super don't accept arguments
16-
super(map);
17-
this.opts = options;
18-
}
19-
}
10+
class MapExtend extends Map {}
2011

21-
return new MapExtend(new Map(), { timeout: 1000 });
12+
return new MapExtend();
2213
};
2314

2415
keyvTestSuite(test, Keyv, storeExtended);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"scripts": {
1515
"build:keyv": "pnpm recursive --filter=keyv run build",
1616
"build": "pnpm build:keyv && pnpm recursive --filter '!keyv' run build",
17-
"build:sequential": "pnpm build:keyv && pnpm --filter '!keyv' --workspace-concurrency 1 -r build",
17+
"build:list": "pnpm build:keyv && pnpm --filter '!keyv' --workspace-concurrency 1 -r build",
1818
"test": "pnpm -r test",
19-
"test:sequential": "pnpm -r --workspace-concurrency 1 test",
19+
"test:list": "pnpm -r --workspace-concurrency 1 test",
2020
"test:ci": "pnpm -r test:ci",
2121
"test:services:start": "chmod +x ./scripts/test-services-start.sh && ./scripts/test-services-start.sh",
2222
"test:services:stop": "chmod +x ./scripts/test-services-stop.sh && ./scripts/test-services-stop.sh",

storage/dynamo/README.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
- [.namespace](#namespace)
3333
- [.keyPrefixSeparator](#keyprefixseparator)
3434
- [.sixHoursInMilliseconds](#sixhoursinmilliseconds)
35-
- [.opts](#opts)
3635
- [Methods](#methods)
3736
- [constructor(options?)](#constructoroptions)
3837
- [.get(key)](#getkey)
@@ -208,7 +207,6 @@ Options extend [`DynamoDBClientConfig`](https://docs.aws.amazon.com/AWSJavaScrip
208207
|---|---|---|---|
209208
| `tableName` | `string` | `'keyv'` | The DynamoDB table name. Created automatically if it doesn't exist. |
210209
| `namespace` | `string` | `undefined` | Key prefix for namespace isolation |
211-
| `dialect` | `string` | `'dynamo'` | Storage dialect identifier (read-only) |
212210
| `endpoint` | `string` || The DynamoDB endpoint URL (e.g., `'http://localhost:8000'` for local development) |
213211
| `region` | `string` || The AWS region (e.g., `'us-east-1'`) |
214212

@@ -256,27 +254,13 @@ The default TTL fallback in milliseconds. Used when no TTL is specified in a `se
256254
|---|---|
257255
| `number` | `21600000` (6 hours) |
258256

259-
### .opts
260-
261-
Read-only object containing the current configuration options.
262-
263-
| Type |
264-
|---|
265-
| `KeyvDynamoOptions` |
266-
267-
```js
268-
const store = new KeyvDynamo({ endpoint: 'http://localhost:8000', tableName: 'cacheTable' });
269-
console.log(store.opts);
270-
// { tableName: 'cacheTable', dialect: 'dynamo', endpoint: 'http://localhost:8000' }
271-
```
272-
273257
## Methods
274258

275259
### constructor(options?)
276260

277261
Creates a new `KeyvDynamo` instance. Automatically creates the DynamoDB table if it doesn't exist.
278262

279-
- `options` — A `KeyvDynamoOptions` object or an endpoint string. Defaults to `{ tableName: 'keyv', dialect: 'dynamo' }`.
263+
- `options` — A `KeyvDynamoOptions` object or an endpoint string. Defaults to `{ tableName: 'keyv' }`.
280264

281265
```js
282266
import KeyvDynamo from '@keyv/dynamo';

storage/dynamo/src/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ export class KeyvDynamo extends Hookified implements KeyvStorageAdapter {
4242

4343
this._opts = {
4444
tableName: "keyv",
45-
dialect: "dynamo",
4645
...options,
4746
};
4847

48+
if (this._opts.namespace) {
49+
this._namespace = this._opts.namespace;
50+
}
51+
4952
this._client = DynamoDBDocument.from(new DynamoDB(this._opts));
5053

5154
this._tableReady = this.ensureTable(this._opts.tableName).catch(
@@ -83,15 +86,6 @@ export class KeyvDynamo extends Hookified implements KeyvStorageAdapter {
8386
this._namespace = value;
8487
}
8588

86-
/**
87-
* Gets the merged configuration options. Read-only.
88-
*/
89-
public get opts(): Omit<KeyvDynamoOptions, "tableName"> & {
90-
tableName: string;
91-
} {
92-
return this._opts;
93-
}
94-
9589
/**
9690
* Gets the underlying DynamoDB Document client instance.
9791
*/
@@ -106,6 +100,20 @@ export class KeyvDynamo extends Hookified implements KeyvStorageAdapter {
106100
this._client = value;
107101
}
108102

103+
/**
104+
* Gets the DynamoDB table name.
105+
*/
106+
public get tableName(): string {
107+
return this._opts.tableName;
108+
}
109+
110+
/**
111+
* Gets the DynamoDB endpoint URL, if configured.
112+
*/
113+
public get endpoint(): string | undefined {
114+
return this._opts.endpoint as string | undefined;
115+
}
116+
109117
/**
110118
* Gets the separator between the namespace and key.
111119
* @default ':'
@@ -621,7 +629,6 @@ export class KeyvDynamo extends Hookified implements KeyvStorageAdapter {
621629
export default KeyvDynamo;
622630
export type KeyvDynamoOptions = {
623631
namespace?: string;
624-
dialect?: string;
625632
tableName?: string;
626633
} & DynamoDBClientConfig;
627634

storage/dynamo/test/test.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,13 @@ test.it("should ensure table creation", async (t) => {
3737

3838
test.it("should be able to create a keyv instance", (t) => {
3939
const keyv = new Keyv<string>({ store: keyvDynamodb });
40-
t.expect(keyv.store.opts.endpoint).toEqual(dynamoURL);
40+
t.expect((keyv.store as KeyvDynamo).endpoint).toEqual(dynamoURL);
4141
});
4242

4343
test.it("should be able to create a keyv instance with namespace", (t) => {
44-
const keyv = new Keyv<string>({
45-
store: new KeyvDynamo({ endpoint: dynamoURL, namespace: "test" }),
46-
});
47-
t.expect(keyv.store.opts.endpoint).toEqual(dynamoURL);
48-
t.expect(keyv.store.opts.namespace).toEqual("test");
44+
const store = new KeyvDynamo({ endpoint: dynamoURL, namespace: "test" });
45+
t.expect(store.endpoint).toEqual(dynamoURL);
46+
t.expect(store.namespace).toEqual("test");
4947
});
5048

5149
test.it(".clear() entire cache store with default namespace", async (t) => {
@@ -351,7 +349,7 @@ test.it("has returns false for expired key", async (t) => {
351349
await store.set(key, "value", 0);
352350
// Manually overwrite with an already-expired expiresAt
353351
await store.client.put({
354-
TableName: store.opts.tableName,
352+
TableName: store.tableName,
355353
Item: {
356354
id: store.formatKey(key),
357355
value: "value",
@@ -369,7 +367,7 @@ test.it("hasMany returns false for expired keys", async (t) => {
369367
await store.set(key2, "value2");
370368
// Overwrite key1 with an expired expiresAt
371369
await store.client.put({
372-
TableName: store.opts.tableName,
370+
TableName: store.tableName,
373371
Item: {
374372
id: store.formatKey(key1),
375373
value: "value1",
@@ -396,7 +394,7 @@ test.describe("createKeyv", () => {
396394
t.expect(keyv.namespace).toBeUndefined();
397395
t.expect((keyv.store as KeyvDynamo).namespace).toBeUndefined();
398396

399-
t.expect((keyv.store as KeyvDynamo).opts.endpoint).toBe(dynamoURL);
397+
t.expect((keyv.store as KeyvDynamo).endpoint).toBe(dynamoURL);
400398
});
401399

402400
test.it("should create Keyv instance with custom namespace", (t) => {
@@ -407,7 +405,7 @@ test.describe("createKeyv", () => {
407405
t.expect(keyv.namespace).toBe(namespace);
408406
t.expect((keyv.store as KeyvDynamo).namespace).toBe(namespace);
409407

410-
t.expect((keyv.store as KeyvDynamo).opts.endpoint).toBe(dynamoURL);
408+
t.expect((keyv.store as KeyvDynamo).endpoint).toBe(dynamoURL);
411409
});
412410

413411
test.it("should create Keyv instance with custom table name", (t) => {
@@ -418,7 +416,7 @@ test.describe("createKeyv", () => {
418416
t.expect(keyv.namespace).toBeUndefined();
419417
t.expect((keyv.store as KeyvDynamo).namespace).toBeUndefined();
420418

421-
t.expect((keyv.store as KeyvDynamo).opts.tableName).toBe(tableName);
419+
t.expect((keyv.store as KeyvDynamo).tableName).toBe(tableName);
422420
});
423421

424422
test.it(
@@ -432,7 +430,7 @@ test.describe("createKeyv", () => {
432430
t.expect(keyv.namespace).toBe(namespace);
433431
t.expect((keyv.store as KeyvDynamo).namespace).toBe(namespace);
434432

435-
t.expect((keyv.store as KeyvDynamo).opts.tableName).toBe(tableName);
433+
t.expect((keyv.store as KeyvDynamo).tableName).toBe(tableName);
436434
},
437435
);
438436

0 commit comments

Comments
 (0)