@keyv/mongo 3.1.0 → 6.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,20 +1,16 @@
1
- import EventEmitter from 'node:events';
2
- import { KeyvStoreAdapter, StoredData } from 'keyv';
3
- import { ReadPreference, GridFSBucket, Collection, Db, MongoClient } from 'mongodb';
1
+ import { Hookified } from 'hookified';
2
+ import Keyv, { KeyvStoreAdapter, StoredData } from 'keyv';
3
+ import { GridFSBucket, Collection, Db, MongoClient, ReadPreference, MongoClientOptions } from 'mongodb';
4
4
 
5
5
  type Options = {
6
- [key: string]: unknown;
7
- url?: string | undefined;
6
+ url?: string;
8
7
  collection?: string;
9
8
  namespace?: string;
10
- serialize?: any;
11
- deserialize?: any;
12
9
  useGridFS?: boolean;
13
10
  uri?: string;
14
- dialect?: string;
15
11
  db?: string;
16
12
  readPreference?: ReadPreference;
17
- };
13
+ } & MongoClientOptions;
18
14
  type KeyvMongoOptions = Options | string;
19
15
  type KeyvMongoConnect = {
20
16
  bucket?: GridFSBucket;
@@ -23,23 +19,212 @@ type KeyvMongoConnect = {
23
19
  mongoClient: MongoClient;
24
20
  };
25
21
 
26
- declare class KeyvMongo extends EventEmitter implements KeyvStoreAdapter {
27
- ttlSupport: boolean;
28
- opts: Options;
22
+ /**
23
+ * MongoDB storage adapter for Keyv.
24
+ * Provides a persistent key-value store using MongoDB as the backend.
25
+ */
26
+ declare class KeyvMongo extends Hookified implements KeyvStoreAdapter {
27
+ /**
28
+ * The MongoDB connection URI.
29
+ * @default 'mongodb://127.0.0.1:27017'
30
+ */
31
+ private _url;
32
+ /**
33
+ * The collection name used for storage.
34
+ * @default 'keyv'
35
+ */
36
+ private _collection;
37
+ /**
38
+ * The namespace used to prefix keys for multi-tenant separation.
39
+ */
40
+ private _namespace?;
41
+ /**
42
+ * Whether to use GridFS for storing values.
43
+ * @default false
44
+ */
45
+ private _useGridFS;
46
+ /**
47
+ * The database name for the MongoDB connection.
48
+ * @default undefined
49
+ */
50
+ private _db?;
51
+ /**
52
+ * The MongoDB read preference for GridFS operations.
53
+ * @default undefined
54
+ */
55
+ private _readPreference?;
56
+ /**
57
+ * Additional MongoClientOptions passed through to the MongoDB driver.
58
+ */
59
+ private _mongoOptions;
60
+ /**
61
+ * Promise that resolves to the MongoDB connection details.
62
+ */
29
63
  connect: Promise<KeyvMongoConnect>;
30
- namespace?: string;
64
+ /**
65
+ * Get the MongoDB connection URI.
66
+ * @default 'mongodb://127.0.0.1:27017'
67
+ */
68
+ get url(): string;
69
+ /**
70
+ * Set the MongoDB connection URI.
71
+ */
72
+ set url(value: string);
73
+ /**
74
+ * Get the collection name used for storage.
75
+ * @default 'keyv'
76
+ */
77
+ get collection(): string;
78
+ /**
79
+ * Set the collection name used for storage.
80
+ */
81
+ set collection(value: string);
82
+ /**
83
+ * Get the namespace for the adapter. If undefined, no namespace prefix is applied.
84
+ */
85
+ get namespace(): string | undefined;
86
+ /**
87
+ * Set the namespace for the adapter. Used for key prefixing and scoping operations like `clear()`.
88
+ */
89
+ set namespace(value: string | undefined);
90
+ /**
91
+ * Get whether GridFS is used for storing values. This is read-only and can only be set via the constructor
92
+ * because the MongoDB connection shape differs between GridFS and standard modes.
93
+ * @default false
94
+ */
95
+ get useGridFS(): boolean;
96
+ /**
97
+ * Get the database name for the MongoDB connection.
98
+ */
99
+ get db(): string | undefined;
100
+ /**
101
+ * Set the database name for the MongoDB connection.
102
+ */
103
+ set db(value: string | undefined);
104
+ /**
105
+ * Get the MongoDB read preference for GridFS operations.
106
+ */
107
+ get readPreference(): ReadPreference | undefined;
108
+ /**
109
+ * Set the MongoDB read preference for GridFS operations.
110
+ */
111
+ set readPreference(value: ReadPreference | undefined);
112
+ /**
113
+ * Get the options for the adapter. This is provided for backward compatibility.
114
+ */
115
+ get opts(): any;
116
+ /**
117
+ * Creates a new KeyvMongo instance.
118
+ * @param url - Configuration options, connection URI string, or undefined for defaults.
119
+ * @param options - Additional configuration options that override the first parameter.
120
+ */
31
121
  constructor(url?: KeyvMongoOptions, options?: Options);
122
+ /**
123
+ * Get a value from the store by key. In GridFS mode, also updates the `lastAccessed` timestamp.
124
+ * @param key - The key to retrieve.
125
+ * @returns The stored value, or `undefined` if the key does not exist.
126
+ */
32
127
  get<Value>(key: string): Promise<StoredData<Value>>;
128
+ /**
129
+ * Get multiple values from the store at once. In standard mode, uses a single query with the `$in` operator.
130
+ * In GridFS mode, each key is fetched individually in parallel.
131
+ * @param keys - Array of keys to retrieve.
132
+ * @returns Array of values in the same order as the input keys. Missing keys return `undefined`.
133
+ */
33
134
  getMany<Value>(keys: string[]): Promise<StoredData<Value>[]>;
135
+ /**
136
+ * Set a value in the store.
137
+ * @param key - The key to set.
138
+ * @param value - The value to store.
139
+ * @param ttl - Time to live in milliseconds. If specified, the key will expire after this duration.
140
+ */
34
141
  set(key: string, value: any, ttl?: number): Promise<unknown>;
142
+ /**
143
+ * Set multiple values in the store at once. In standard mode, uses a single `bulkWrite` operation.
144
+ * In GridFS mode, each entry is set individually in parallel.
145
+ * @param entries - Array of entries to set. Each entry has a `key`, `value`, and optional `ttl` in milliseconds.
146
+ */
147
+ setMany(entries: Array<{
148
+ key: string;
149
+ value: any;
150
+ ttl?: number;
151
+ }>): Promise<void>;
152
+ /**
153
+ * Delete a key from the store.
154
+ * @param key - The key to delete.
155
+ * @returns `true` if the key was deleted, `false` if the key was not found.
156
+ */
35
157
  delete(key: string): Promise<boolean>;
158
+ /**
159
+ * Delete multiple keys from the store at once. In standard mode, uses a single query with the `$in` operator.
160
+ * In GridFS mode, all matching files are found and deleted in parallel.
161
+ * @param keys - Array of keys to delete.
162
+ * @returns `true` if any keys were deleted, `false` if none were found.
163
+ */
36
164
  deleteMany(keys: string[]): Promise<boolean>;
165
+ /**
166
+ * Delete all keys in the current namespace.
167
+ */
37
168
  clear(): Promise<void>;
169
+ /**
170
+ * Remove all expired files from GridFS. This method only works in GridFS mode
171
+ * and is a no-op that returns `false` in standard mode.
172
+ * @returns `true` if running in GridFS mode, `false` otherwise.
173
+ */
38
174
  clearExpired(): Promise<boolean>;
175
+ /**
176
+ * Remove all GridFS files that have not been accessed for the specified duration. This method only works
177
+ * in GridFS mode and is a no-op that returns `false` in standard mode.
178
+ * @param seconds - The number of seconds of inactivity after which files should be removed.
179
+ * @returns `true` if running in GridFS mode, `false` otherwise.
180
+ */
39
181
  clearUnusedFor(seconds: number): Promise<boolean>;
182
+ /**
183
+ * Iterate over all key-value pairs in the store matching the given namespace.
184
+ * @param namespace - The namespace to iterate over. When used through Keyv, this is passed automatically.
185
+ * @yields `[key, value]` pairs as an async generator.
186
+ */
40
187
  iterator(namespace?: string): AsyncGenerator<any[], void, void>;
188
+ /**
189
+ * Check if a key exists in the store.
190
+ * @param key - The key to check.
191
+ * @returns `true` if the key exists, `false` otherwise.
192
+ */
41
193
  has(key: string): Promise<boolean>;
194
+ /**
195
+ * Check if multiple keys exist in the store at once. Uses a single query with the `$in` operator.
196
+ * @param keys - Array of keys to check.
197
+ * @returns Array of booleans in the same order as the input keys.
198
+ */
199
+ hasMany(keys: string[]): Promise<boolean[]>;
200
+ /**
201
+ * Close the MongoDB connection.
202
+ */
42
203
  disconnect(): Promise<void>;
204
+ /**
205
+ * Strips the namespace prefix from a key that was added by the Keyv core.
206
+ * For example, if namespace is "ns" and key is "ns:foo", returns "foo".
207
+ */
208
+ private removeKeyPrefix;
209
+ /**
210
+ * Returns the namespace value for query filters.
211
+ * Returns empty string when no namespace is set.
212
+ */
213
+ private getNamespaceValue;
214
+ /**
215
+ * Extracts MongoDB driver options from the provided options, filtering out Keyv-specific properties.
216
+ */
217
+ private extractMongoOptions;
218
+ /**
219
+ * Initializes the MongoDB connection and sets up indexes.
220
+ */
221
+ private initConnection;
43
222
  }
223
+ /**
224
+ * Helper function to create a Keyv instance with KeyvMongo as the storage adapter.
225
+ * @param options - Optional {@link KeyvMongoOptions} configuration object or connection URI string.
226
+ * @returns A new Keyv instance backed by MongoDB.
227
+ */
228
+ declare const createKeyv: (options?: KeyvMongoOptions) => Keyv<any>;
44
229
 
45
- export { KeyvMongo, type KeyvMongoOptions, KeyvMongo as default };
230
+ export { KeyvMongo, type KeyvMongoOptions, createKeyv, KeyvMongo as default };
package/dist/index.d.ts CHANGED
@@ -1,20 +1,16 @@
1
- import EventEmitter from 'node:events';
2
- import { KeyvStoreAdapter, StoredData } from 'keyv';
3
- import { ReadPreference, GridFSBucket, Collection, Db, MongoClient } from 'mongodb';
1
+ import { Hookified } from 'hookified';
2
+ import Keyv, { KeyvStoreAdapter, StoredData } from 'keyv';
3
+ import { GridFSBucket, Collection, Db, MongoClient, ReadPreference, MongoClientOptions } from 'mongodb';
4
4
 
5
5
  type Options = {
6
- [key: string]: unknown;
7
- url?: string | undefined;
6
+ url?: string;
8
7
  collection?: string;
9
8
  namespace?: string;
10
- serialize?: any;
11
- deserialize?: any;
12
9
  useGridFS?: boolean;
13
10
  uri?: string;
14
- dialect?: string;
15
11
  db?: string;
16
12
  readPreference?: ReadPreference;
17
- };
13
+ } & MongoClientOptions;
18
14
  type KeyvMongoOptions = Options | string;
19
15
  type KeyvMongoConnect = {
20
16
  bucket?: GridFSBucket;
@@ -23,23 +19,212 @@ type KeyvMongoConnect = {
23
19
  mongoClient: MongoClient;
24
20
  };
25
21
 
26
- declare class KeyvMongo extends EventEmitter implements KeyvStoreAdapter {
27
- ttlSupport: boolean;
28
- opts: Options;
22
+ /**
23
+ * MongoDB storage adapter for Keyv.
24
+ * Provides a persistent key-value store using MongoDB as the backend.
25
+ */
26
+ declare class KeyvMongo extends Hookified implements KeyvStoreAdapter {
27
+ /**
28
+ * The MongoDB connection URI.
29
+ * @default 'mongodb://127.0.0.1:27017'
30
+ */
31
+ private _url;
32
+ /**
33
+ * The collection name used for storage.
34
+ * @default 'keyv'
35
+ */
36
+ private _collection;
37
+ /**
38
+ * The namespace used to prefix keys for multi-tenant separation.
39
+ */
40
+ private _namespace?;
41
+ /**
42
+ * Whether to use GridFS for storing values.
43
+ * @default false
44
+ */
45
+ private _useGridFS;
46
+ /**
47
+ * The database name for the MongoDB connection.
48
+ * @default undefined
49
+ */
50
+ private _db?;
51
+ /**
52
+ * The MongoDB read preference for GridFS operations.
53
+ * @default undefined
54
+ */
55
+ private _readPreference?;
56
+ /**
57
+ * Additional MongoClientOptions passed through to the MongoDB driver.
58
+ */
59
+ private _mongoOptions;
60
+ /**
61
+ * Promise that resolves to the MongoDB connection details.
62
+ */
29
63
  connect: Promise<KeyvMongoConnect>;
30
- namespace?: string;
64
+ /**
65
+ * Get the MongoDB connection URI.
66
+ * @default 'mongodb://127.0.0.1:27017'
67
+ */
68
+ get url(): string;
69
+ /**
70
+ * Set the MongoDB connection URI.
71
+ */
72
+ set url(value: string);
73
+ /**
74
+ * Get the collection name used for storage.
75
+ * @default 'keyv'
76
+ */
77
+ get collection(): string;
78
+ /**
79
+ * Set the collection name used for storage.
80
+ */
81
+ set collection(value: string);
82
+ /**
83
+ * Get the namespace for the adapter. If undefined, no namespace prefix is applied.
84
+ */
85
+ get namespace(): string | undefined;
86
+ /**
87
+ * Set the namespace for the adapter. Used for key prefixing and scoping operations like `clear()`.
88
+ */
89
+ set namespace(value: string | undefined);
90
+ /**
91
+ * Get whether GridFS is used for storing values. This is read-only and can only be set via the constructor
92
+ * because the MongoDB connection shape differs between GridFS and standard modes.
93
+ * @default false
94
+ */
95
+ get useGridFS(): boolean;
96
+ /**
97
+ * Get the database name for the MongoDB connection.
98
+ */
99
+ get db(): string | undefined;
100
+ /**
101
+ * Set the database name for the MongoDB connection.
102
+ */
103
+ set db(value: string | undefined);
104
+ /**
105
+ * Get the MongoDB read preference for GridFS operations.
106
+ */
107
+ get readPreference(): ReadPreference | undefined;
108
+ /**
109
+ * Set the MongoDB read preference for GridFS operations.
110
+ */
111
+ set readPreference(value: ReadPreference | undefined);
112
+ /**
113
+ * Get the options for the adapter. This is provided for backward compatibility.
114
+ */
115
+ get opts(): any;
116
+ /**
117
+ * Creates a new KeyvMongo instance.
118
+ * @param url - Configuration options, connection URI string, or undefined for defaults.
119
+ * @param options - Additional configuration options that override the first parameter.
120
+ */
31
121
  constructor(url?: KeyvMongoOptions, options?: Options);
122
+ /**
123
+ * Get a value from the store by key. In GridFS mode, also updates the `lastAccessed` timestamp.
124
+ * @param key - The key to retrieve.
125
+ * @returns The stored value, or `undefined` if the key does not exist.
126
+ */
32
127
  get<Value>(key: string): Promise<StoredData<Value>>;
128
+ /**
129
+ * Get multiple values from the store at once. In standard mode, uses a single query with the `$in` operator.
130
+ * In GridFS mode, each key is fetched individually in parallel.
131
+ * @param keys - Array of keys to retrieve.
132
+ * @returns Array of values in the same order as the input keys. Missing keys return `undefined`.
133
+ */
33
134
  getMany<Value>(keys: string[]): Promise<StoredData<Value>[]>;
135
+ /**
136
+ * Set a value in the store.
137
+ * @param key - The key to set.
138
+ * @param value - The value to store.
139
+ * @param ttl - Time to live in milliseconds. If specified, the key will expire after this duration.
140
+ */
34
141
  set(key: string, value: any, ttl?: number): Promise<unknown>;
142
+ /**
143
+ * Set multiple values in the store at once. In standard mode, uses a single `bulkWrite` operation.
144
+ * In GridFS mode, each entry is set individually in parallel.
145
+ * @param entries - Array of entries to set. Each entry has a `key`, `value`, and optional `ttl` in milliseconds.
146
+ */
147
+ setMany(entries: Array<{
148
+ key: string;
149
+ value: any;
150
+ ttl?: number;
151
+ }>): Promise<void>;
152
+ /**
153
+ * Delete a key from the store.
154
+ * @param key - The key to delete.
155
+ * @returns `true` if the key was deleted, `false` if the key was not found.
156
+ */
35
157
  delete(key: string): Promise<boolean>;
158
+ /**
159
+ * Delete multiple keys from the store at once. In standard mode, uses a single query with the `$in` operator.
160
+ * In GridFS mode, all matching files are found and deleted in parallel.
161
+ * @param keys - Array of keys to delete.
162
+ * @returns `true` if any keys were deleted, `false` if none were found.
163
+ */
36
164
  deleteMany(keys: string[]): Promise<boolean>;
165
+ /**
166
+ * Delete all keys in the current namespace.
167
+ */
37
168
  clear(): Promise<void>;
169
+ /**
170
+ * Remove all expired files from GridFS. This method only works in GridFS mode
171
+ * and is a no-op that returns `false` in standard mode.
172
+ * @returns `true` if running in GridFS mode, `false` otherwise.
173
+ */
38
174
  clearExpired(): Promise<boolean>;
175
+ /**
176
+ * Remove all GridFS files that have not been accessed for the specified duration. This method only works
177
+ * in GridFS mode and is a no-op that returns `false` in standard mode.
178
+ * @param seconds - The number of seconds of inactivity after which files should be removed.
179
+ * @returns `true` if running in GridFS mode, `false` otherwise.
180
+ */
39
181
  clearUnusedFor(seconds: number): Promise<boolean>;
182
+ /**
183
+ * Iterate over all key-value pairs in the store matching the given namespace.
184
+ * @param namespace - The namespace to iterate over. When used through Keyv, this is passed automatically.
185
+ * @yields `[key, value]` pairs as an async generator.
186
+ */
40
187
  iterator(namespace?: string): AsyncGenerator<any[], void, void>;
188
+ /**
189
+ * Check if a key exists in the store.
190
+ * @param key - The key to check.
191
+ * @returns `true` if the key exists, `false` otherwise.
192
+ */
41
193
  has(key: string): Promise<boolean>;
194
+ /**
195
+ * Check if multiple keys exist in the store at once. Uses a single query with the `$in` operator.
196
+ * @param keys - Array of keys to check.
197
+ * @returns Array of booleans in the same order as the input keys.
198
+ */
199
+ hasMany(keys: string[]): Promise<boolean[]>;
200
+ /**
201
+ * Close the MongoDB connection.
202
+ */
42
203
  disconnect(): Promise<void>;
204
+ /**
205
+ * Strips the namespace prefix from a key that was added by the Keyv core.
206
+ * For example, if namespace is "ns" and key is "ns:foo", returns "foo".
207
+ */
208
+ private removeKeyPrefix;
209
+ /**
210
+ * Returns the namespace value for query filters.
211
+ * Returns empty string when no namespace is set.
212
+ */
213
+ private getNamespaceValue;
214
+ /**
215
+ * Extracts MongoDB driver options from the provided options, filtering out Keyv-specific properties.
216
+ */
217
+ private extractMongoOptions;
218
+ /**
219
+ * Initializes the MongoDB connection and sets up indexes.
220
+ */
221
+ private initConnection;
43
222
  }
223
+ /**
224
+ * Helper function to create a Keyv instance with KeyvMongo as the storage adapter.
225
+ * @param options - Optional {@link KeyvMongoOptions} configuration object or connection URI string.
226
+ * @returns A new Keyv instance backed by MongoDB.
227
+ */
228
+ declare const createKeyv: (options?: KeyvMongoOptions) => Keyv<any>;
44
229
 
45
- export { KeyvMongo, type KeyvMongoOptions, KeyvMongo as default };
230
+ export { KeyvMongo, type KeyvMongoOptions, createKeyv, KeyvMongo as default };