Skip to content

Commit

Permalink
Add delete functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
samirsilwal committed Oct 20, 2024
1 parent a57ab86 commit 3eb0c34
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/AsyncStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface AsyncStore {
getId: () => string | undefined;
getShortId: () => string | undefined;
reset: () => void;
del: (key: string) => void;
}

export default AsyncStore;
17 changes: 17 additions & 0 deletions src/impl/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ export function reset() {
activeDomain[STORE_KEY] = null;

Check warning on line 109 in src/impl/domain.ts

View check run for this annotation

Codecov / codecov/patch

src/impl/domain.ts#L108-L109

Added lines #L108 - L109 were not covered by tests
}

/**
* 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;

Check warning on line 123 in src/impl/domain.ts

View check run for this annotation

Codecov / codecov/patch

src/impl/domain.ts#L123

Added line #L123 was not covered by tests
}

delete store[key];
}

/**
* Get a value by a key from the store.
* Throws an error if anything fails while getting the value.
Expand Down
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ export function reset() {
getInstance(initializedAdapter).reset();

Check warning on line 129 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L129

Added line #L129 was not covered by tests
}

/**
* Delete a key from the store.
*
* @param {string} key
*/
export function del(key: string) {
if (!isInitialized()) {
throw new Error('Async store not initialized.');

Check warning on line 139 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L139

Added line #L139 was not covered by tests
}

coreLog(`Deleting ${key} from the store`);

getInstance(initializedAdapter).del(key);
}

/**
* Sets properties in the store.
*
Expand Down
42 changes: 42 additions & 0 deletions test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 3eb0c34

Please sign in to comment.