Skip to content

Commit 3eb0c34

Browse files
committed
Add delete functionality
1 parent a57ab86 commit 3eb0c34

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,6 @@ Note: This is same as `getId();` the difference being it only returns the first
342342
const requestIdentifier = store.getShortId();
343343
```
344344

345-
346-
347345
## Example Projects
348346

349347
1. [Node Web Server (TypeScript)](examples/node-http-server-ts)

src/AsyncStore.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface AsyncStore {
1414
getId: () => string | undefined;
1515
getShortId: () => string | undefined;
1616
reset: () => void;
17+
del: (key: string) => void;
1718
}
1819

1920
export default AsyncStore;

src/impl/domain.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ export function reset() {
109109
activeDomain[STORE_KEY] = null;
110110
}
111111

112+
/**
113+
* Delete a key from the store.
114+
*
115+
* @param {string} key
116+
*/
117+
export function del(key: string) {
118+
logDomain(`Deleting ${key} from the domain store.`);
119+
120+
const store = getStore();
121+
122+
if (!store) {
123+
return;
124+
}
125+
126+
delete store[key];
127+
}
128+
112129
/**
113130
* Get a value by a key from the store.
114131
* Throws an error if anything fails while getting the value.

src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ export function reset() {
129129
getInstance(initializedAdapter).reset();
130130
}
131131

132+
/**
133+
* Delete a key from the store.
134+
*
135+
* @param {string} key
136+
*/
137+
export function del(key: string) {
138+
if (!isInitialized()) {
139+
throw new Error('Async store not initialized.');
140+
}
141+
142+
coreLog(`Deleting ${key} from the store`);
143+
144+
getInstance(initializedAdapter).del(key);
145+
}
146+
132147
/**
133148
* Sets properties in the store.
134149
*

test/domain.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as globalStore from '../src';
88
import Middleware from '../src/Middleware';
99
import { STORE_KEY } from '../src/StoreDomain';
1010
import AsyncStoreAdapter from '../src/AsyncStoreAdapter';
11+
import { getActiveDomain } from '../src/impl/domain';
1112

1213
describe('store: [adapter=DOMAIN]', () => {
1314
const adapter = AsyncStoreAdapter.DOMAIN;
@@ -503,6 +504,47 @@ describe('store: [adapter=DOMAIN]', () => {
503504
globalStore.reset();
504505

505506
expect(globalStore.isInitialized()).to.equal(false);
507+
508+
const activeDomain = getActiveDomain();
509+
expect(activeDomain[STORE_KEY]).to.equal(null);
510+
});
511+
});
512+
513+
describe('del():', () => {
514+
it('should delete a key from the store.', (done) => {
515+
const callback = () => {
516+
globalStore.set({ foo: 'foo', bar: 'bar' });
517+
518+
expect(globalStore.get('foo')).to.equal('foo');
519+
expect(globalStore.get('bar')).to.equal('bar');
520+
521+
globalStore.del('foo');
522+
523+
expect(globalStore.get('foo')).to.equal(undefined);
524+
expect(globalStore.get('bar')).to.equal('bar');
525+
526+
done();
527+
};
528+
529+
globalStore.initialize(adapter)(callback);
530+
});
531+
532+
it('should do nothing if the key does not exist.', (done) => {
533+
const callback = () => {
534+
globalStore.set({ foo: 'foo', bar: 'bar' });
535+
536+
expect(globalStore.get('foo')).to.equal('foo');
537+
expect(globalStore.get('bar')).to.equal('bar');
538+
539+
globalStore.del('baz');
540+
541+
expect(globalStore.get('foo')).to.equal('foo');
542+
expect(globalStore.get('bar')).to.equal('bar');
543+
544+
done();
545+
};
546+
547+
globalStore.initialize(adapter)(callback);
506548
});
507549
});
508550

0 commit comments

Comments
 (0)