From c3a771d6579eee8c2ec1eab83d138b51f8385e83 Mon Sep 17 00:00:00 2001 From: samirsilwal Date: Sun, 6 Oct 2024 21:44:06 +0545 Subject: [PATCH 1/5] Add 'reset' method to store --- README.md | 12 ++++++++++++ src/AsyncStore.ts | 1 + src/impl/domain.ts | 22 ++++++++++++++++++++++ src/index.ts | 15 +++++++++++++++ test/domain.test.ts | 31 +++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+) diff --git a/README.md b/README.md index eecf830..900300a 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,18 @@ Note: This is same as `getId();` the difference being it only returns the first const requestIdentifier = store.getShortId(); ``` +### reset() + +Reset the store or a specific key of the global store. + +- `@returns {void}` + +```js +store.reset(); // Reset the whole store + +store.reset('foo') // It will delete the key foo from store and get will result undefined +``` + ## Example Projects 1. [Node Web Server (TypeScript)](examples/node-http-server-ts) diff --git a/src/AsyncStore.ts b/src/AsyncStore.ts index a385e82..7e2c9d9 100644 --- a/src/AsyncStore.ts +++ b/src/AsyncStore.ts @@ -13,6 +13,7 @@ interface AsyncStore { isInitialized: () => boolean; getId: () => string | undefined; getShortId: () => string | undefined; + reset: (key?: string) => void; } export default AsyncStore; diff --git a/src/impl/domain.ts b/src/impl/domain.ts index 6c131f3..878d7c3 100644 --- a/src/impl/domain.ts +++ b/src/impl/domain.ts @@ -99,6 +99,28 @@ export function set(properties: any) { throw new Error('Invalid arguments provided for asyncStore.set()'); } +/** + * Reset the store by removing all values or a specific value by key. + * + * @param {string} key + */ +export function reset(key?: string) { + const store = getStore(); + + if (key) { + logDomain(`Resetting ${key} in the domain store.`); + + delete store[key]; + + return; + } + + logDomain('Resetting the domain store.'); + + const activeDomain = getActiveDomain(); + activeDomain[STORE_KEY] = null; +} + /** * Get a value by a key from the store. * Throws an error if anything fails while getting the value. diff --git a/src/index.ts b/src/index.ts index 1ba3276..c24bc47 100644 --- a/src/index.ts +++ b/src/index.ts @@ -115,6 +115,21 @@ export function initialize(adapter: AsyncStoreAdapter = AsyncStoreAdapter.DOMAIN }; } +/** + * Reset the store or a specific key. + * + * @param {string} [key] + */ +export function reset(key?: string) { + if (!isInitialized()) { + throw new Error('Async store not initialized.'); + } + + coreLog(`Resetting store or key = ${key}`); + + getInstance(initializedAdapter).reset(key); +} + /** * Sets properties in the store. * diff --git a/test/domain.test.ts b/test/domain.test.ts index 231e1e1..a8640b1 100644 --- a/test/domain.test.ts +++ b/test/domain.test.ts @@ -485,6 +485,37 @@ describe('store: [adapter=DOMAIN]', () => { }); }); + describe('Reset():', () => { + it('should reset the store by removing all values', (done) => { + const callback = () => { + globalStore.set({ foo: 'foo', bar: 'bar' }); + + globalStore.reset(); + + expect(globalStore.isInitialized()).to.equal(false); + + done(); + }; + + globalStore.initialize(adapter)(callback); + }); + + it('should reset the store by removing a specific value by key', (done) => { + const callback = () => { + globalStore.set({ foo: 'foo', bar: 'bar' }); + + globalStore.reset('foo'); + + expect(globalStore.get('foo')).to.equal(undefined); + expect(globalStore.get('bar')).to.equal('bar'); + + done(); + }; + + globalStore.initialize(adapter)(callback); + }); + }); + describe('Test Cases:', () => { it('should work with a chain of sequentially invoked callbacks (synchronous).', (done) => { const callback = () => { From 36649b41fd606fa59ea3d3575e15a81fc277a670 Mon Sep 17 00:00:00 2001 From: samirsilwal Date: Sun, 6 Oct 2024 21:55:08 +0545 Subject: [PATCH 2/5] Fix test for the added reset function --- test/domain.test.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/domain.test.ts b/test/domain.test.ts index a8640b1..e9b2227 100644 --- a/test/domain.test.ts +++ b/test/domain.test.ts @@ -490,14 +490,19 @@ describe('store: [adapter=DOMAIN]', () => { const callback = () => { globalStore.set({ foo: 'foo', bar: 'bar' }); - globalStore.reset(); + expect(globalStore.isInitialized()).to.equal(true); - expect(globalStore.isInitialized()).to.equal(false); + expect(globalStore.get('foo')).to.equal('foo'); + expect(globalStore.get('bar')).to.equal('bar'); done(); }; globalStore.initialize(adapter)(callback); + + globalStore.reset(); + + expect(globalStore.isInitialized()).to.equal(false); }); it('should reset the store by removing a specific value by key', (done) => { From a57ab86532decd0e50c4e76c4562086a0afffa5e Mon Sep 17 00:00:00 2001 From: samirsilwal Date: Sun, 20 Oct 2024 17:10:06 +0545 Subject: [PATCH 3/5] Remove reseting specific keys --- README.md | 10 ---------- src/AsyncStore.ts | 2 +- src/impl/domain.ts | 14 +------------- src/index.ts | 13 ++++++------- test/domain.test.ts | 15 --------------- 5 files changed, 8 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 900300a..cbeaaf3 100644 --- a/README.md +++ b/README.md @@ -342,17 +342,7 @@ Note: This is same as `getId();` the difference being it only returns the first const requestIdentifier = store.getShortId(); ``` -### reset() -Reset the store or a specific key of the global store. - -- `@returns {void}` - -```js -store.reset(); // Reset the whole store - -store.reset('foo') // It will delete the key foo from store and get will result undefined -``` ## Example Projects diff --git a/src/AsyncStore.ts b/src/AsyncStore.ts index 7e2c9d9..fb5bd44 100644 --- a/src/AsyncStore.ts +++ b/src/AsyncStore.ts @@ -13,7 +13,7 @@ interface AsyncStore { isInitialized: () => boolean; getId: () => string | undefined; getShortId: () => string | undefined; - reset: (key?: string) => void; + reset: () => void; } export default AsyncStore; diff --git a/src/impl/domain.ts b/src/impl/domain.ts index 878d7c3..afdca7c 100644 --- a/src/impl/domain.ts +++ b/src/impl/domain.ts @@ -101,20 +101,8 @@ export function set(properties: any) { /** * Reset the store by removing all values or a specific value by key. - * - * @param {string} key */ -export function reset(key?: string) { - const store = getStore(); - - if (key) { - logDomain(`Resetting ${key} in the domain store.`); - - delete store[key]; - - return; - } - +export function reset() { logDomain('Resetting the domain store.'); const activeDomain = getActiveDomain(); diff --git a/src/index.ts b/src/index.ts index c24bc47..1a9cb47 100644 --- a/src/index.ts +++ b/src/index.ts @@ -116,18 +116,17 @@ export function initialize(adapter: AsyncStoreAdapter = AsyncStoreAdapter.DOMAIN } /** - * Reset the store or a specific key. - * - * @param {string} [key] - */ -export function reset(key?: string) { + * Reset the store. + * + * */ +export function reset() { if (!isInitialized()) { throw new Error('Async store not initialized.'); } - coreLog(`Resetting store or key = ${key}`); + coreLog(`Resetting the store`); - getInstance(initializedAdapter).reset(key); + getInstance(initializedAdapter).reset(); } /** diff --git a/test/domain.test.ts b/test/domain.test.ts index e9b2227..08c1913 100644 --- a/test/domain.test.ts +++ b/test/domain.test.ts @@ -504,21 +504,6 @@ describe('store: [adapter=DOMAIN]', () => { expect(globalStore.isInitialized()).to.equal(false); }); - - it('should reset the store by removing a specific value by key', (done) => { - const callback = () => { - globalStore.set({ foo: 'foo', bar: 'bar' }); - - globalStore.reset('foo'); - - expect(globalStore.get('foo')).to.equal(undefined); - expect(globalStore.get('bar')).to.equal('bar'); - - done(); - }; - - globalStore.initialize(adapter)(callback); - }); }); describe('Test Cases:', () => { From 3eb0c34dee18bf7f87e459e30ab784acf2b35f2e Mon Sep 17 00:00:00 2001 From: samirsilwal Date: Sun, 20 Oct 2024 17:29:57 +0545 Subject: [PATCH 4/5] Add delete functionality --- README.md | 2 -- src/AsyncStore.ts | 1 + src/impl/domain.ts | 17 +++++++++++++++++ src/index.ts | 15 +++++++++++++++ test/domain.test.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cbeaaf3..eecf830 100644 --- a/README.md +++ b/README.md @@ -342,8 +342,6 @@ Note: This is same as `getId();` the difference being it only returns the first const requestIdentifier = store.getShortId(); ``` - - ## Example Projects 1. [Node Web Server (TypeScript)](examples/node-http-server-ts) diff --git a/src/AsyncStore.ts b/src/AsyncStore.ts index fb5bd44..c6a571c 100644 --- a/src/AsyncStore.ts +++ b/src/AsyncStore.ts @@ -14,6 +14,7 @@ interface AsyncStore { getId: () => string | undefined; getShortId: () => string | undefined; reset: () => void; + del: (key: string) => void; } export default AsyncStore; diff --git a/src/impl/domain.ts b/src/impl/domain.ts index afdca7c..73a2193 100644 --- a/src/impl/domain.ts +++ b/src/impl/domain.ts @@ -109,6 +109,23 @@ export function reset() { activeDomain[STORE_KEY] = null; } +/** + * Delete a key from the store. + * + * @param {string} key + */ +export function del(key: string) { + logDomain(`Deleting ${key} from the domain store.`); + + const store = getStore(); + + if (!store) { + return; + } + + delete store[key]; +} + /** * Get a value by a key from the store. * Throws an error if anything fails while getting the value. diff --git a/src/index.ts b/src/index.ts index 1a9cb47..155da7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -129,6 +129,21 @@ export function reset() { getInstance(initializedAdapter).reset(); } +/** + * Delete a key from the store. + * + * @param {string} key + */ +export function del(key: string) { + if (!isInitialized()) { + throw new Error('Async store not initialized.'); + } + + coreLog(`Deleting ${key} from the store`); + + getInstance(initializedAdapter).del(key); +} + /** * Sets properties in the store. * diff --git a/test/domain.test.ts b/test/domain.test.ts index 08c1913..4e39a98 100644 --- a/test/domain.test.ts +++ b/test/domain.test.ts @@ -8,6 +8,7 @@ import * as globalStore from '../src'; import Middleware from '../src/Middleware'; import { STORE_KEY } from '../src/StoreDomain'; import AsyncStoreAdapter from '../src/AsyncStoreAdapter'; +import { getActiveDomain } from '../src/impl/domain'; describe('store: [adapter=DOMAIN]', () => { const adapter = AsyncStoreAdapter.DOMAIN; @@ -503,6 +504,47 @@ describe('store: [adapter=DOMAIN]', () => { globalStore.reset(); expect(globalStore.isInitialized()).to.equal(false); + + const activeDomain = getActiveDomain(); + expect(activeDomain[STORE_KEY]).to.equal(null); + }); + }); + + describe('del():', () => { + it('should delete a key from the store.', (done) => { + const callback = () => { + globalStore.set({ foo: 'foo', bar: 'bar' }); + + expect(globalStore.get('foo')).to.equal('foo'); + expect(globalStore.get('bar')).to.equal('bar'); + + globalStore.del('foo'); + + expect(globalStore.get('foo')).to.equal(undefined); + expect(globalStore.get('bar')).to.equal('bar'); + + done(); + }; + + globalStore.initialize(adapter)(callback); + }); + + it('should do nothing if the key does not exist.', (done) => { + const callback = () => { + globalStore.set({ foo: 'foo', bar: 'bar' }); + + expect(globalStore.get('foo')).to.equal('foo'); + expect(globalStore.get('bar')).to.equal('bar'); + + globalStore.del('baz'); + + expect(globalStore.get('foo')).to.equal('foo'); + expect(globalStore.get('bar')).to.equal('bar'); + + done(); + }; + + globalStore.initialize(adapter)(callback); }); }); From db0e90fe6cd409a3dc7cd1cdf2957398606111e1 Mon Sep 17 00:00:00 2001 From: samirsilwal Date: Sun, 20 Oct 2024 17:33:27 +0545 Subject: [PATCH 5/5] Update tests --- test/domain.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/domain.test.ts b/test/domain.test.ts index 4e39a98..0a17390 100644 --- a/test/domain.test.ts +++ b/test/domain.test.ts @@ -501,6 +501,8 @@ describe('store: [adapter=DOMAIN]', () => { globalStore.initialize(adapter)(callback); + expect(globalStore.reset).to.not.throw('Async store not initialized.'); + globalStore.reset(); expect(globalStore.isInitialized()).to.equal(false); @@ -508,6 +510,12 @@ describe('store: [adapter=DOMAIN]', () => { const activeDomain = getActiveDomain(); expect(activeDomain[STORE_KEY]).to.equal(null); }); + + it('should do nothing if the store is not initialized.', () => { + expect(globalStore.reset).to.throw('Async store not initialized.'); + }); + + }); describe('del():', () => { @@ -536,6 +544,7 @@ describe('store: [adapter=DOMAIN]', () => { expect(globalStore.get('foo')).to.equal('foo'); expect(globalStore.get('bar')).to.equal('bar'); + expect(globalStore.del.bind(globalStore, 'baz')).to.not.throw('Async store not initialized.'); globalStore.del('baz'); expect(globalStore.get('foo')).to.equal('foo'); @@ -546,6 +555,10 @@ describe('store: [adapter=DOMAIN]', () => { globalStore.initialize(adapter)(callback); }); + + it('should do nothing if the store is not initialized.', () => { + expect(globalStore.del.bind(globalStore, 'foo')).to.throw('Async store not initialized.'); + }); }); describe('Test Cases:', () => {