Skip to content

Commit 6adfc35

Browse files
committed
feat(mongodb-helpers): add key deletion
Signed-off-by: Álvaro Orduna León <[email protected]>
1 parent d6e9cc9 commit 6adfc35

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const { mongoDBPaginate, EMPTY_PAGINATED_RESULT } = require('./mongoDBPaginate');
2-
const mongoDBPaginateAggregationPipeline = require('./mongoDBPaginateAggregationPipeline');
3-
const { getKeyValueModel } = require('./key-value/getKeyValueModel');
1+
const { deleteKey } = require('./key-value/deleteKey');
42
const { getKey } = require('./key-value/getKey');
3+
const { getKeyValueModel } = require('./key-value/getKeyValueModel');
54
const { hasKey } = require('./key-value/hasKey');
6-
const { setKey } = require('./key-value/setKey');
75
const { hasKeys } = require('./key-value/hasKeys');
6+
const { setKey } = require('./key-value/setKey');
7+
const { mongoDBPaginate, EMPTY_PAGINATED_RESULT } = require('./mongoDBPaginate');
8+
const mongoDBPaginateAggregationPipeline = require('./mongoDBPaginateAggregationPipeline');
89

910
module.exports = {
1011
mongoDBPaginate,
@@ -15,4 +16,5 @@ module.exports = {
1516
hasKey,
1617
setKey,
1718
hasKeys,
19+
deleteKey,
1820
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @typedef {import('mongoose').Model} Model
3+
* @typedef {import('mongoose').DeleteResult} DeleteResult
4+
*
5+
* Deletes a key from the key-value store.
6+
*
7+
* @param {Model} model - The model to delete the key from.
8+
* @param {string} key - The key to delete.
9+
* @returns {Promise<DeleteResult>} A promise that resolves to the result of the delete operation.
10+
*/
11+
function deleteKey(model, key) {
12+
return model.deleteOne({ key });
13+
}
14+
15+
module.exports = {
16+
deleteKey,
17+
};

packages/leemons-mongodb-helpers/src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type SetKeyQueryResult = {
3434
export function getKey<T>(model: Model<T>, key: string): Promise<GetKeyQueryResult<T>>;
3535
export function hasKey(model: Model<unknown>, key: string): Promise<boolean>;
3636
export function setKey<T>(model: Model<T>, key: string, value?: T): Promise<SetKeyQueryResult>;
37+
export function deleteKey<T>(model: Model<T>, key: string): Promise<DeleteResult>;
3738

3839
export type GetKeyValueModel = {
3940
id: string;

packages/leemons-mongodb-helpers/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { Model, PaginatedQueryResult } from '@leemons/mongodb';
3-
import { FilterQuery, SortOrder } from 'mongoose';
3+
import { FilterQuery, SortOrder, DeleteResult } from 'mongoose';
44

55
type Params<M extends Model<any> = Model<any>, R = object> = {
66
model: M;
@@ -44,6 +44,7 @@ export type SetKeyQueryResult = {
4444
export function getKey<T>(model: Model<T>, key: string): Promise<GetKeyQueryResult<T>>;
4545
export function hasKey(model: Model<unknown>, key: string): Promise<boolean>;
4646
export function setKey<T>(model: Model<T>, key: string, value?: T): Promise<SetKeyQueryResult>;
47+
export function deleteKey<T>(model: Model<T>, key: string): Promise<DeleteResult>;
4748

4849
export type GetKeyValueModel = {
4950
id: string;

0 commit comments

Comments
 (0)