@keyv/mongo 2.0.0 → 2.1.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keyv/mongo",
3
- "version": "2.0.0",
3
+ "version": "2.1.6",
4
4
  "description": "MongoDB storage adapter for Keyv",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -9,11 +9,11 @@
9
9
  "clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov"
10
10
  },
11
11
  "xo": {
12
- "extends": "xo-lukechilds",
13
12
  "rules": {
14
13
  "unicorn/prefer-module": 0,
15
14
  "unicorn/no-array-reduce": 0,
16
- "unicorn/prefer-object-from-entries": 0
15
+ "unicorn/prefer-object-from-entries": 0,
16
+ "unicorn/prefer-node-protocol": 0
17
17
  }
18
18
  },
19
19
  "ava": {
@@ -44,17 +44,25 @@
44
44
  },
45
45
  "homepage": "https://github.com/jaredwray/keyv",
46
46
  "dependencies": {
47
- "mongodb": "^4.3.1",
47
+ "mongodb": "^4.4.1",
48
48
  "pify": "^5.0.0"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@keyv/test-suite": "*",
52
- "ava": "^3.15.0",
53
- "eslint-config-xo-lukechilds": "^1.0.1",
52
+ "ava": "^4.1.0",
54
53
  "keyv": "*",
55
54
  "nyc": "^15.1.0",
56
55
  "requirable": "^1.0.5",
57
56
  "this": "^1.1.0",
58
- "xo": "^0.47.0"
59
- }
57
+ "tsd": "^0.20.0",
58
+ "typescript": "^4.6.3",
59
+ "xo": "^0.48.0"
60
+ },
61
+ "tsd" : {
62
+ "directory" : "test"
63
+ },
64
+ "types": "./src/index.d.ts",
65
+ "files": [
66
+ "src"
67
+ ]
60
68
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ import {EventEmitter} from 'events';
2
+ import GridFSBucket from 'mongodb';
3
+
4
+ declare class KeyvMongo extends EventEmitter {
5
+ readonly ttlSupport: false;
6
+ opts: Record<string, any>;
7
+ connect: Promise<any>;
8
+ db: import('mongodb').Db;
9
+ bucket: GridFSBucket;
10
+ store: import('mongodb').Collection<import('bson').Document>;
11
+ constructor(options?: string | KeyvMongo.Options);
12
+ get(key: string): Promise<string | undefined>;
13
+ getMany(keys: string[]): Promise<string[] | undefined>;
14
+ set(key: string, value: string | undefined): Promise<any>;
15
+ delete(key: string): boolean;
16
+ deleteMany(keys: string[]): boolean;
17
+ clearExpired(): false | Promise<boolean>;
18
+ clearUnusedFor(seconds: any): false | Promise<boolean>;
19
+ clear(): Promise<void>;
20
+ iterator(namespace: string | undefined): AsyncGenerator<any, void, any>;
21
+ has(key: string): boolean;
22
+ }
23
+
24
+ export = KeyvMongo;
25
+
26
+ declare namespace KeyvMongo {
27
+ interface Options {
28
+ url?: string | undefined;
29
+ collection?: string | undefined;
30
+ namespace?: string | undefined;
31
+ serialize?: any;
32
+ deserialize?: any;
33
+ useGridFS?: boolean | undefined;
34
+ uri?: string | undefined;
35
+ dialect?: string | undefined;
36
+ }
37
+ }
package/src/index.js CHANGED
@@ -1,33 +1,31 @@
1
1
  'use strict';
2
2
 
3
3
  const EventEmitter = require('events');
4
- const Buffer = require('buffer').Buffer;
4
+ const {Buffer} = require('buffer');
5
5
  const mongoClient = require('mongodb').MongoClient;
6
- const GridFSBucket = require('mongodb').GridFSBucket;
6
+ const {GridFSBucket} = require('mongodb');
7
7
  const pify = require('pify');
8
8
 
9
- const keyvMongoKeys = new Set(['url', 'collection', 'namespace', 'serialize', 'deserialize', 'uri', 'useGridFS']);
9
+ const keyvMongoKeys = new Set(['url', 'collection', 'namespace', 'serialize', 'deserialize', 'uri', 'useGridFS', 'dialect']);
10
10
  class KeyvMongo extends EventEmitter {
11
11
  constructor(url, options) {
12
12
  super();
13
13
  this.ttlSupport = false;
14
14
  url = url || {};
15
15
  if (typeof url === 'string') {
16
- url = { url };
16
+ url = {url};
17
17
  }
18
18
 
19
19
  if (url.uri) {
20
- url = Object.assign({ url: url.uri }, url);
20
+ url = {url: url.uri, ...url};
21
21
  }
22
22
 
23
- this.opts = Object.assign(
24
- {
25
- url: 'mongodb://127.0.0.1:27017',
26
- collection: 'keyv',
27
- },
28
- url,
29
- options,
30
- );
23
+ this.opts = {
24
+ url: 'mongodb://127.0.0.1:27017',
25
+ collection: 'keyv',
26
+ ...url,
27
+ ...options,
28
+ };
31
29
 
32
30
  const mongoOptions = Object.fromEntries(
33
31
  Object.entries(this.opts).filter(
@@ -43,75 +41,76 @@ class KeyvMongo extends EventEmitter {
43
41
 
44
42
  // Implementation from sql by lukechilds,
45
43
  this.connect = new Promise(resolve => {
46
- mongoClient.connect(this.opts.url, mongoOptions
47
- , (error, client) => {
48
- if (error) {
49
- return this.emit('error', error);
50
- }
51
-
52
- this.db = client.db(this.opts.db);
53
- if (this.opts.useGridFS) {
54
- this.bucket = new GridFSBucket(this.db, {
55
- readPreference: this.opts.readPreference || 'primary',
56
- bucketName: this.opts.collection,
57
- });
58
- this.store = this.db.collection(this.opts.collection + '.files');
59
- this.store.createIndex({
60
- filename: 'hashed',
61
- });
62
- this.store.createIndex({
63
- uploadDate: -1,
64
- });
65
- this.store.createIndex({
66
- 'metadata.expiresAt': 1,
67
- });
68
- this.store.createIndex({
69
- 'metadata.lastAccessed': 1,
70
- });
44
+ mongoClient.connect(this.opts.url, mongoOptions, (error, client) => {
45
+ if (error) {
46
+ return this.emit('error', error);
47
+ }
71
48
 
72
- for (const method of [
73
- 'updateOne',
74
- ]) {
75
- this.store[method] = pify(this.store[method].bind(this.store));
76
- }
49
+ this.db = client.db(this.opts.db);
50
+ if (this.opts.useGridFS) {
51
+ this.bucket = new GridFSBucket(this.db, {
52
+ readPreference: this.opts.readPreference || 'primary',
53
+ bucketName: this.opts.collection,
54
+ });
55
+ this.store = this.db.collection(this.opts.collection + '.files');
56
+ this.store.createIndex({
57
+ filename: 'hashed',
58
+ });
59
+ this.store.createIndex({
60
+ uploadDate: -1,
61
+ });
62
+ this.store.createIndex({
63
+ 'metadata.expiresAt': 1,
64
+ });
65
+ this.store.createIndex({
66
+ 'metadata.lastAccessed': 1,
67
+ });
77
68
 
78
- for (const method of [
79
- 'find',
80
- 'drop',
81
- ]) {
82
- this.bucket[method] = pify(this.bucket[method].bind(this.bucket));
83
- }
69
+ for (const method of [
70
+ 'updateOne',
71
+ 'count',
72
+ ]) {
73
+ this.store[method] = pify(this.store[method].bind(this.store));
74
+ }
84
75
 
85
- resolve({ bucket: this.bucket, store: this.store, db: this.db });
86
- } else {
87
- this.store = this.db.collection(this.opts.collection);
88
- this.store.createIndex(
89
- { key: 1 },
90
- {
91
- unique: true,
92
- background: true,
93
- },
94
- );
95
- this.store.createIndex(
96
- { expiresAt: 1 },
97
- {
98
- expireAfterSeconds: 0,
99
- background: true,
100
- },
101
- );
102
-
103
- for (const method of [
104
- 'updateOne',
105
- 'findOne',
106
- 'deleteOne',
107
- 'deleteMany',
108
- ]) {
109
- this.store[method] = pify(this.store[method].bind(this.store));
110
- }
76
+ for (const method of [
77
+ 'find',
78
+ 'drop',
79
+ ]) {
80
+ this.bucket[method] = pify(this.bucket[method].bind(this.bucket));
81
+ }
111
82
 
112
- resolve(this.store);
83
+ resolve({bucket: this.bucket, store: this.store, db: this.db});
84
+ } else {
85
+ this.store = this.db.collection(this.opts.collection);
86
+ this.store.createIndex(
87
+ {key: 1},
88
+ {
89
+ unique: true,
90
+ background: true,
91
+ },
92
+ );
93
+ this.store.createIndex(
94
+ {expiresAt: 1},
95
+ {
96
+ expireAfterSeconds: 0,
97
+ background: true,
98
+ },
99
+ );
100
+
101
+ for (const method of [
102
+ 'updateOne',
103
+ 'findOne',
104
+ 'deleteOne',
105
+ 'deleteMany',
106
+ 'count',
107
+ ]) {
108
+ this.store[method] = pify(this.store[method].bind(this.store));
113
109
  }
114
- });
110
+
111
+ resolve(this.store);
112
+ }
113
+ });
115
114
  });
116
115
  }
117
116
 
@@ -144,7 +143,7 @@ class KeyvMongo extends EventEmitter {
144
143
  }
145
144
 
146
145
  return this.connect.then(store =>
147
- store.findOne({ key: { $eq: key } }).then(doc => {
146
+ store.findOne({key: {$eq: key}}).then(doc => {
148
147
  if (!doc) {
149
148
  return undefined;
150
149
  }
@@ -154,6 +153,48 @@ class KeyvMongo extends EventEmitter {
154
153
  );
155
154
  }
156
155
 
156
+ getMany(keys) {
157
+ if (this.opts.useGridFS) {
158
+ const promises = [];
159
+ for (const key of keys) {
160
+ promises.push(this.get(key));
161
+ }
162
+
163
+ return Promise.allSettled(promises)
164
+ .then(values => {
165
+ const data = [];
166
+ for (const value of values) {
167
+ data.push(value.value);
168
+ }
169
+
170
+ return data.every(x => x === undefined) ? [] : data;
171
+ });
172
+ }
173
+
174
+ const results = [...keys];
175
+ return this.connect.then(store =>
176
+ store.s.db.collection(this.opts.collection)
177
+ .find({key: {$in: keys}})
178
+ .project({_id: 0, value: 1, key: 1})
179
+ .toArray().then(values => {
180
+ let i = 0;
181
+ for (const key of keys) {
182
+ const rowIndex = values.findIndex(row => row.key === key);
183
+
184
+ if (rowIndex > -1) {
185
+ results[i] = values[rowIndex].value;
186
+ } else {
187
+ results[i] = undefined;
188
+ }
189
+
190
+ i++;
191
+ }
192
+
193
+ return results.every(x => x === undefined) ? [] : results;
194
+ }),
195
+ );
196
+ }
197
+
157
198
  set(key, value, ttl) {
158
199
  const expiresAt = typeof ttl === 'number' ? new Date(Date.now() + ttl) : null;
159
200
 
@@ -177,9 +218,9 @@ class KeyvMongo extends EventEmitter {
177
218
 
178
219
  return this.connect.then(store =>
179
220
  store.updateOne(
180
- { key: { $eq: key } },
181
- { $set: { key, value, expiresAt } },
182
- { upsert: true },
221
+ {key: {$eq: key}},
222
+ {$set: {key, value, expiresAt}},
223
+ {upsert: true},
183
224
  ),
184
225
  );
185
226
  }
@@ -195,7 +236,7 @@ class KeyvMongo extends EventEmitter {
195
236
  const bucket = new GridFSBucket(connection, {
196
237
  bucketName: this.opts.collection,
197
238
  });
198
- return bucket.find({ filename: key }).toArray()
239
+ return bucket.find({filename: key}).toArray()
199
240
  .then(files => client.bucket.delete(files[0]._id).then(() => true))
200
241
  .catch(() => false);
201
242
  });
@@ -203,7 +244,34 @@ class KeyvMongo extends EventEmitter {
203
244
 
204
245
  return this.connect.then(store =>
205
246
  store
206
- .deleteOne({ key: { $eq: key } })
247
+ .deleteOne({key: {$eq: key}})
248
+ .then(object => object.deletedCount > 0),
249
+ );
250
+ }
251
+
252
+ deleteMany(keys) {
253
+ if (this.opts.useGridFS) {
254
+ return this.connect.then(client => {
255
+ const connection = client.db;
256
+ const bucket = new GridFSBucket(connection, {
257
+ bucketName: this.opts.collection,
258
+ });
259
+ return bucket.find({filename: {$in: keys}}).toArray()
260
+ .then(
261
+ files => {
262
+ if (files.length === 0) {
263
+ return false;
264
+ }
265
+
266
+ files.map(file => client.bucket.delete(file._id));
267
+ return true;
268
+ });
269
+ });
270
+ }
271
+
272
+ return this.connect.then(store =>
273
+ store
274
+ .deleteMany({key: {$in: keys}})
207
275
  .then(object => object.deletedCount > 0),
208
276
  );
209
277
  }
@@ -216,7 +284,7 @@ class KeyvMongo extends EventEmitter {
216
284
  return this.connect.then(store =>
217
285
  store
218
286
  .deleteMany({
219
- key: new RegExp(`^${this.namespace}:`),
287
+ key: {$regex: this.namespace ? `^${this.namespace}:*` : ''},
220
288
  })
221
289
  .then(() => undefined),
222
290
  );
@@ -261,6 +329,31 @@ class KeyvMongo extends EventEmitter {
261
329
  .then(lastAccessedFiles => Promise.all(lastAccessedFiles.map(file => client.bucket.delete(file._id))).then(() => true));
262
330
  });
263
331
  }
332
+
333
+ async * iterator(namespace) {
334
+ const iterator = await this.connect.then(store =>
335
+ store
336
+ .find({
337
+ key: new RegExp(`^${namespace ? namespace + ':' : '.*'}`),
338
+ })
339
+ .map(x => [x.key, x.value]),
340
+ );
341
+ yield * iterator;
342
+ }
343
+
344
+ has(key) {
345
+ if (this.opts.useGridFS) {
346
+ return this.connect.then(client => client.store.count(
347
+ {filename: {$eq: key}},
348
+ ).then(doc => doc !== 0));
349
+ }
350
+
351
+ return this.connect.then(store =>
352
+ store.count(
353
+ {key: {$eq: key}},
354
+ ),
355
+ ).then(doc => doc !== 0);
356
+ }
264
357
  }
265
358
 
266
359
  module.exports = KeyvMongo;
@@ -1 +0,0 @@
1
- {"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo/src/index.js":{"path":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo/src/index.js","statementMap":{"0":{"start":{"line":3,"column":21},"end":{"line":3,"column":38}},"1":{"start":{"line":4,"column":15},"end":{"line":4,"column":39}},"2":{"start":{"line":5,"column":20},"end":{"line":5,"column":50}},"3":{"start":{"line":6,"column":21},"end":{"line":6,"column":52}},"4":{"start":{"line":7,"column":13},"end":{"line":7,"column":28}},"5":{"start":{"line":9,"column":22},"end":{"line":9,"column":113}},"6":{"start":{"line":12,"column":2},"end":{"line":12,"column":10}},"7":{"start":{"line":13,"column":2},"end":{"line":13,"column":26}},"8":{"start":{"line":14,"column":2},"end":{"line":14,"column":18}},"9":{"start":{"line":15,"column":2},"end":{"line":17,"column":3}},"10":{"start":{"line":16,"column":3},"end":{"line":16,"column":17}},"11":{"start":{"line":19,"column":2},"end":{"line":21,"column":3}},"12":{"start":{"line":20,"column":3},"end":{"line":20,"column":46}},"13":{"start":{"line":23,"column":2},"end":{"line":30,"column":4}},"14":{"start":{"line":32,"column":23},"end":{"line":36,"column":3}},"15":{"start":{"line":34,"column":13},"end":{"line":34,"column":34}},"16":{"start":{"line":38,"column":2},"end":{"line":42,"column":4}},"17":{"start":{"line":40,"column":13},"end":{"line":40,"column":33}},"18":{"start":{"line":45,"column":2},"end":{"line":115,"column":5}},"19":{"start":{"line":46,"column":3},"end":{"line":114,"column":7}},"20":{"start":{"line":48,"column":5},"end":{"line":50,"column":6}},"21":{"start":{"line":49,"column":6},"end":{"line":49,"column":39}},"22":{"start":{"line":52,"column":5},"end":{"line":52,"column":39}},"23":{"start":{"line":53,"column":5},"end":{"line":113,"column":6}},"24":{"start":{"line":54,"column":6},"end":{"line":57,"column":9}},"25":{"start":{"line":58,"column":6},"end":{"line":58,"column":71}},"26":{"start":{"line":59,"column":6},"end":{"line":61,"column":9}},"27":{"start":{"line":62,"column":6},"end":{"line":64,"column":9}},"28":{"start":{"line":65,"column":6},"end":{"line":67,"column":9}},"29":{"start":{"line":68,"column":6},"end":{"line":70,"column":9}},"30":{"start":{"line":72,"column":6},"end":{"line":76,"column":7}},"31":{"start":{"line":75,"column":7},"end":{"line":75,"column":70}},"32":{"start":{"line":78,"column":6},"end":{"line":83,"column":7}},"33":{"start":{"line":82,"column":7},"end":{"line":82,"column":73}},"34":{"start":{"line":85,"column":6},"end":{"line":85,"column":71}},"35":{"start":{"line":87,"column":6},"end":{"line":87,"column":60}},"36":{"start":{"line":88,"column":6},"end":{"line":94,"column":8}},"37":{"start":{"line":95,"column":6},"end":{"line":101,"column":8}},"38":{"start":{"line":103,"column":6},"end":{"line":110,"column":7}},"39":{"start":{"line":109,"column":7},"end":{"line":109,"column":70}},"40":{"start":{"line":112,"column":6},"end":{"line":112,"column":26}},"41":{"start":{"line":119,"column":2},"end":{"line":144,"column":3}},"42":{"start":{"line":120,"column":3},"end":{"line":143,"column":6}},"43":{"start":{"line":121,"column":4},"end":{"line":127,"column":7}},"44":{"start":{"line":129,"column":19},"end":{"line":129,"column":62}},"45":{"start":{"line":130,"column":4},"end":{"line":142,"column":7}},"46":{"start":{"line":131,"column":16},"end":{"line":131,"column":18}},"47":{"start":{"line":132,"column":5},"end":{"line":132,"column":41}},"48":{"start":{"line":132,"column":30},"end":{"line":132,"column":39}},"49":{"start":{"line":134,"column":5},"end":{"line":137,"column":8}},"50":{"start":{"line":135,"column":6},"end":{"line":135,"column":51}},"51":{"start":{"line":136,"column":6},"end":{"line":136,"column":20}},"52":{"start":{"line":139,"column":5},"end":{"line":141,"column":8}},"53":{"start":{"line":140,"column":6},"end":{"line":140,"column":23}},"54":{"start":{"line":146,"column":2},"end":{"line":154,"column":4}},"55":{"start":{"line":147,"column":3},"end":{"line":153,"column":5}},"56":{"start":{"line":148,"column":4},"end":{"line":150,"column":5}},"57":{"start":{"line":149,"column":5},"end":{"line":149,"column":22}},"58":{"start":{"line":152,"column":4},"end":{"line":152,"column":21}},"59":{"start":{"line":158,"column":20},"end":{"line":158,"column":79}},"60":{"start":{"line":160,"column":2},"end":{"line":176,"column":3}},"61":{"start":{"line":161,"column":3},"end":{"line":175,"column":6}},"62":{"start":{"line":162,"column":19},"end":{"line":167,"column":6}},"63":{"start":{"line":169,"column":4},"end":{"line":174,"column":7}},"64":{"start":{"line":170,"column":5},"end":{"line":172,"column":8}},"65":{"start":{"line":171,"column":6},"end":{"line":171,"column":22}},"66":{"start":{"line":173,"column":5},"end":{"line":173,"column":23}},"67":{"start":{"line":178,"column":2},"end":{"line":184,"column":4}},"68":{"start":{"line":179,"column":3},"end":{"line":183,"column":4}},"69":{"start":{"line":188,"column":2},"end":{"line":190,"column":3}},"70":{"start":{"line":189,"column":3},"end":{"line":189,"column":33}},"71":{"start":{"line":192,"column":2},"end":{"line":202,"column":3}},"72":{"start":{"line":193,"column":3},"end":{"line":201,"column":6}},"73":{"start":{"line":194,"column":23},"end":{"line":194,"column":32}},"74":{"start":{"line":195,"column":19},"end":{"line":197,"column":6}},"75":{"start":{"line":198,"column":4},"end":{"line":200,"column":25}},"76":{"start":{"line":199,"column":20},"end":{"line":199,"column":71}},"77":{"start":{"line":199,"column":66},"end":{"line":199,"column":70}},"78":{"start":{"line":200,"column":18},"end":{"line":200,"column":23}},"79":{"start":{"line":204,"column":2},"end":{"line":208,"column":4}},"80":{"start":{"line":205,"column":3},"end":{"line":207,"column":44}},"81":{"start":{"line":207,"column":20},"end":{"line":207,"column":43}},"82":{"start":{"line":212,"column":2},"end":{"line":214,"column":3}},"83":{"start":{"line":213,"column":3},"end":{"line":213,"column":82}},"84":{"start":{"line":213,"column":38},"end":{"line":213,"column":80}},"85":{"start":{"line":213,"column":70},"end":{"line":213,"column":79}},"86":{"start":{"line":216,"column":2},"end":{"line":222,"column":4}},"87":{"start":{"line":217,"column":3},"end":{"line":221,"column":26}},"88":{"start":{"line":221,"column":16},"end":{"line":221,"column":25}},"89":{"start":{"line":226,"column":2},"end":{"line":228,"column":3}},"90":{"start":{"line":227,"column":3},"end":{"line":227,"column":16}},"91":{"start":{"line":230,"column":2},"end":{"line":242,"column":5}},"92":{"start":{"line":231,"column":22},"end":{"line":231,"column":31}},"93":{"start":{"line":232,"column":18},"end":{"line":234,"column":5}},"94":{"start":{"line":236,"column":3},"end":{"line":241,"column":114}},"95":{"start":{"line":241,"column":26},"end":{"line":241,"column":112}},"96":{"start":{"line":241,"column":63},"end":{"line":241,"column":93}},"97":{"start":{"line":241,"column":107},"end":{"line":241,"column":111}},"98":{"start":{"line":246,"column":2},"end":{"line":248,"column":3}},"99":{"start":{"line":247,"column":3},"end":{"line":247,"column":16}},"100":{"start":{"line":250,"column":2},"end":{"line":262,"column":5}},"101":{"start":{"line":251,"column":22},"end":{"line":251,"column":31}},"102":{"start":{"line":252,"column":18},"end":{"line":254,"column":5}},"103":{"start":{"line":256,"column":3},"end":{"line":261,"column":124}},"104":{"start":{"line":261,"column":31},"end":{"line":261,"column":122}},"105":{"start":{"line":261,"column":73},"end":{"line":261,"column":103}},"106":{"start":{"line":261,"column":117},"end":{"line":261,"column":121}},"107":{"start":{"line":266,"column":0},"end":{"line":266,"column":27}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":11,"column":1},"end":{"line":11,"column":2}},"loc":{"start":{"line":11,"column":27},"end":{"line":116,"column":2}},"line":11},"1":{"name":"(anonymous_1)","decl":{"start":{"line":34,"column":4},"end":{"line":34,"column":5}},"loc":{"start":{"line":34,"column":13},"end":{"line":34,"column":34}},"line":34},"2":{"name":"(anonymous_2)","decl":{"start":{"line":40,"column":4},"end":{"line":40,"column":5}},"loc":{"start":{"line":40,"column":13},"end":{"line":40,"column":33}},"line":40},"3":{"name":"(anonymous_3)","decl":{"start":{"line":45,"column":29},"end":{"line":45,"column":30}},"loc":{"start":{"line":45,"column":40},"end":{"line":115,"column":3}},"line":45},"4":{"name":"(anonymous_4)","decl":{"start":{"line":47,"column":6},"end":{"line":47,"column":7}},"loc":{"start":{"line":47,"column":25},"end":{"line":114,"column":5}},"line":47},"5":{"name":"(anonymous_5)","decl":{"start":{"line":118,"column":1},"end":{"line":118,"column":2}},"loc":{"start":{"line":118,"column":10},"end":{"line":155,"column":2}},"line":118},"6":{"name":"(anonymous_6)","decl":{"start":{"line":120,"column":28},"end":{"line":120,"column":29}},"loc":{"start":{"line":120,"column":38},"end":{"line":143,"column":4}},"line":120},"7":{"name":"(anonymous_7)","decl":{"start":{"line":130,"column":23},"end":{"line":130,"column":24}},"loc":{"start":{"line":130,"column":34},"end":{"line":142,"column":5}},"line":130},"8":{"name":"(anonymous_8)","decl":{"start":{"line":132,"column":24},"end":{"line":132,"column":25}},"loc":{"start":{"line":132,"column":30},"end":{"line":132,"column":39}},"line":132},"9":{"name":"(anonymous_9)","decl":{"start":{"line":134,"column":22},"end":{"line":134,"column":23}},"loc":{"start":{"line":134,"column":28},"end":{"line":137,"column":6}},"line":134},"10":{"name":"(anonymous_10)","decl":{"start":{"line":139,"column":23},"end":{"line":139,"column":24}},"loc":{"start":{"line":139,"column":32},"end":{"line":141,"column":6}},"line":139},"11":{"name":"(anonymous_11)","decl":{"start":{"line":146,"column":27},"end":{"line":146,"column":28}},"loc":{"start":{"line":147,"column":3},"end":{"line":153,"column":5}},"line":147},"12":{"name":"(anonymous_12)","decl":{"start":{"line":147,"column":45},"end":{"line":147,"column":46}},"loc":{"start":{"line":147,"column":52},"end":{"line":153,"column":4}},"line":147},"13":{"name":"(anonymous_13)","decl":{"start":{"line":157,"column":1},"end":{"line":157,"column":2}},"loc":{"start":{"line":157,"column":22},"end":{"line":185,"column":2}},"line":157},"14":{"name":"(anonymous_14)","decl":{"start":{"line":161,"column":28},"end":{"line":161,"column":29}},"loc":{"start":{"line":161,"column":38},"end":{"line":175,"column":4}},"line":161},"15":{"name":"(anonymous_15)","decl":{"start":{"line":169,"column":23},"end":{"line":169,"column":24}},"loc":{"start":{"line":169,"column":34},"end":{"line":174,"column":5}},"line":169},"16":{"name":"(anonymous_16)","decl":{"start":{"line":170,"column":25},"end":{"line":170,"column":26}},"loc":{"start":{"line":170,"column":31},"end":{"line":172,"column":6}},"line":170},"17":{"name":"(anonymous_17)","decl":{"start":{"line":178,"column":27},"end":{"line":178,"column":28}},"loc":{"start":{"line":179,"column":3},"end":{"line":183,"column":4}},"line":179},"18":{"name":"(anonymous_18)","decl":{"start":{"line":187,"column":1},"end":{"line":187,"column":2}},"loc":{"start":{"line":187,"column":13},"end":{"line":209,"column":2}},"line":187},"19":{"name":"(anonymous_19)","decl":{"start":{"line":193,"column":28},"end":{"line":193,"column":29}},"loc":{"start":{"line":193,"column":38},"end":{"line":201,"column":4}},"line":193},"20":{"name":"(anonymous_20)","decl":{"start":{"line":199,"column":11},"end":{"line":199,"column":12}},"loc":{"start":{"line":199,"column":20},"end":{"line":199,"column":71}},"line":199},"21":{"name":"(anonymous_21)","decl":{"start":{"line":199,"column":60},"end":{"line":199,"column":61}},"loc":{"start":{"line":199,"column":66},"end":{"line":199,"column":70}},"line":199},"22":{"name":"(anonymous_22)","decl":{"start":{"line":200,"column":12},"end":{"line":200,"column":13}},"loc":{"start":{"line":200,"column":18},"end":{"line":200,"column":23}},"line":200},"23":{"name":"(anonymous_23)","decl":{"start":{"line":204,"column":27},"end":{"line":204,"column":28}},"loc":{"start":{"line":205,"column":3},"end":{"line":207,"column":44}},"line":205},"24":{"name":"(anonymous_24)","decl":{"start":{"line":207,"column":10},"end":{"line":207,"column":11}},"loc":{"start":{"line":207,"column":20},"end":{"line":207,"column":43}},"line":207},"25":{"name":"(anonymous_25)","decl":{"start":{"line":211,"column":1},"end":{"line":211,"column":2}},"loc":{"start":{"line":211,"column":9},"end":{"line":223,"column":2}},"line":211},"26":{"name":"(anonymous_26)","decl":{"start":{"line":213,"column":28},"end":{"line":213,"column":29}},"loc":{"start":{"line":213,"column":38},"end":{"line":213,"column":80}},"line":213},"27":{"name":"(anonymous_27)","decl":{"start":{"line":213,"column":64},"end":{"line":213,"column":65}},"loc":{"start":{"line":213,"column":70},"end":{"line":213,"column":79}},"line":213},"28":{"name":"(anonymous_28)","decl":{"start":{"line":216,"column":27},"end":{"line":216,"column":28}},"loc":{"start":{"line":217,"column":3},"end":{"line":221,"column":26}},"line":217},"29":{"name":"(anonymous_29)","decl":{"start":{"line":221,"column":10},"end":{"line":221,"column":11}},"loc":{"start":{"line":221,"column":16},"end":{"line":221,"column":25}},"line":221},"30":{"name":"(anonymous_30)","decl":{"start":{"line":225,"column":1},"end":{"line":225,"column":2}},"loc":{"start":{"line":225,"column":16},"end":{"line":243,"column":2}},"line":225},"31":{"name":"(anonymous_31)","decl":{"start":{"line":230,"column":27},"end":{"line":230,"column":28}},"loc":{"start":{"line":230,"column":37},"end":{"line":242,"column":3}},"line":230},"32":{"name":"(anonymous_32)","decl":{"start":{"line":241,"column":10},"end":{"line":241,"column":11}},"loc":{"start":{"line":241,"column":26},"end":{"line":241,"column":112}},"line":241},"33":{"name":"(anonymous_33)","decl":{"start":{"line":241,"column":55},"end":{"line":241,"column":56}},"loc":{"start":{"line":241,"column":63},"end":{"line":241,"column":93}},"line":241},"34":{"name":"(anonymous_34)","decl":{"start":{"line":241,"column":101},"end":{"line":241,"column":102}},"loc":{"start":{"line":241,"column":107},"end":{"line":241,"column":111}},"line":241},"35":{"name":"(anonymous_35)","decl":{"start":{"line":245,"column":1},"end":{"line":245,"column":2}},"loc":{"start":{"line":245,"column":25},"end":{"line":263,"column":2}},"line":245},"36":{"name":"(anonymous_36)","decl":{"start":{"line":250,"column":27},"end":{"line":250,"column":28}},"loc":{"start":{"line":250,"column":37},"end":{"line":262,"column":3}},"line":250},"37":{"name":"(anonymous_37)","decl":{"start":{"line":261,"column":10},"end":{"line":261,"column":11}},"loc":{"start":{"line":261,"column":31},"end":{"line":261,"column":122}},"line":261},"38":{"name":"(anonymous_38)","decl":{"start":{"line":261,"column":65},"end":{"line":261,"column":66}},"loc":{"start":{"line":261,"column":73},"end":{"line":261,"column":103}},"line":261},"39":{"name":"(anonymous_39)","decl":{"start":{"line":261,"column":111},"end":{"line":261,"column":112}},"loc":{"start":{"line":261,"column":117},"end":{"line":261,"column":121}},"line":261}},"branchMap":{"0":{"loc":{"start":{"line":14,"column":8},"end":{"line":14,"column":17}},"type":"binary-expr","locations":[{"start":{"line":14,"column":8},"end":{"line":14,"column":11}},{"start":{"line":14,"column":15},"end":{"line":14,"column":17}}],"line":14},"1":{"loc":{"start":{"line":15,"column":2},"end":{"line":17,"column":3}},"type":"if","locations":[{"start":{"line":15,"column":2},"end":{"line":17,"column":3}},{"start":{"line":15,"column":2},"end":{"line":17,"column":3}}],"line":15},"2":{"loc":{"start":{"line":19,"column":2},"end":{"line":21,"column":3}},"type":"if","locations":[{"start":{"line":19,"column":2},"end":{"line":21,"column":3}},{"start":{"line":19,"column":2},"end":{"line":21,"column":3}}],"line":19},"3":{"loc":{"start":{"line":48,"column":5},"end":{"line":50,"column":6}},"type":"if","locations":[{"start":{"line":48,"column":5},"end":{"line":50,"column":6}},{"start":{"line":48,"column":5},"end":{"line":50,"column":6}}],"line":48},"4":{"loc":{"start":{"line":53,"column":5},"end":{"line":113,"column":6}},"type":"if","locations":[{"start":{"line":53,"column":5},"end":{"line":113,"column":6}},{"start":{"line":53,"column":5},"end":{"line":113,"column":6}}],"line":53},"5":{"loc":{"start":{"line":55,"column":23},"end":{"line":55,"column":60}},"type":"binary-expr","locations":[{"start":{"line":55,"column":23},"end":{"line":55,"column":47}},{"start":{"line":55,"column":51},"end":{"line":55,"column":60}}],"line":55},"6":{"loc":{"start":{"line":119,"column":2},"end":{"line":144,"column":3}},"type":"if","locations":[{"start":{"line":119,"column":2},"end":{"line":144,"column":3}},{"start":{"line":119,"column":2},"end":{"line":144,"column":3}}],"line":119},"7":{"loc":{"start":{"line":148,"column":4},"end":{"line":150,"column":5}},"type":"if","locations":[{"start":{"line":148,"column":4},"end":{"line":150,"column":5}},{"start":{"line":148,"column":4},"end":{"line":150,"column":5}}],"line":148},"8":{"loc":{"start":{"line":158,"column":20},"end":{"line":158,"column":79}},"type":"cond-expr","locations":[{"start":{"line":158,"column":46},"end":{"line":158,"column":72}},{"start":{"line":158,"column":75},"end":{"line":158,"column":79}}],"line":158},"9":{"loc":{"start":{"line":160,"column":2},"end":{"line":176,"column":3}},"type":"if","locations":[{"start":{"line":160,"column":2},"end":{"line":176,"column":3}},{"start":{"line":160,"column":2},"end":{"line":176,"column":3}}],"line":160},"10":{"loc":{"start":{"line":188,"column":2},"end":{"line":190,"column":3}},"type":"if","locations":[{"start":{"line":188,"column":2},"end":{"line":190,"column":3}},{"start":{"line":188,"column":2},"end":{"line":190,"column":3}}],"line":188},"11":{"loc":{"start":{"line":192,"column":2},"end":{"line":202,"column":3}},"type":"if","locations":[{"start":{"line":192,"column":2},"end":{"line":202,"column":3}},{"start":{"line":192,"column":2},"end":{"line":202,"column":3}}],"line":192},"12":{"loc":{"start":{"line":212,"column":2},"end":{"line":214,"column":3}},"type":"if","locations":[{"start":{"line":212,"column":2},"end":{"line":214,"column":3}},{"start":{"line":212,"column":2},"end":{"line":214,"column":3}}],"line":212},"13":{"loc":{"start":{"line":226,"column":2},"end":{"line":228,"column":3}},"type":"if","locations":[{"start":{"line":226,"column":2},"end":{"line":228,"column":3}},{"start":{"line":226,"column":2},"end":{"line":228,"column":3}}],"line":226},"14":{"loc":{"start":{"line":246,"column":2},"end":{"line":248,"column":3}},"type":"if","locations":[{"start":{"line":246,"column":2},"end":{"line":248,"column":3}},{"start":{"line":246,"column":2},"end":{"line":248,"column":3}}],"line":246}},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":0,"11":1,"12":1,"13":1,"14":1,"15":9,"16":1,"17":9,"18":1,"19":1,"20":0,"21":0,"22":0,"23":0,"24":0,"25":0,"26":0,"27":0,"28":0,"29":0,"30":0,"31":0,"32":0,"33":0,"34":0,"35":0,"36":0,"37":0,"38":0,"39":0,"40":0,"41":0,"42":0,"43":0,"44":0,"45":0,"46":0,"47":0,"48":0,"49":0,"50":0,"51":0,"52":0,"53":0,"54":0,"55":0,"56":0,"57":0,"58":0,"59":0,"60":0,"61":0,"62":0,"63":0,"64":0,"65":0,"66":0,"67":0,"68":0,"69":0,"70":0,"71":0,"72":0,"73":0,"74":0,"75":0,"76":0,"77":0,"78":0,"79":0,"80":0,"81":0,"82":1,"83":0,"84":0,"85":0,"86":1,"87":0,"88":0,"89":0,"90":0,"91":0,"92":0,"93":0,"94":0,"95":0,"96":0,"97":0,"98":0,"99":0,"100":0,"101":0,"102":0,"103":0,"104":0,"105":0,"106":0,"107":1},"f":{"0":1,"1":9,"2":9,"3":1,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0,"20":0,"21":0,"22":0,"23":0,"24":0,"25":1,"26":0,"27":0,"28":0,"29":0,"30":0,"31":0,"32":0,"33":0,"34":0,"35":0,"36":0,"37":0,"38":0,"39":0},"b":{"0":[1,0],"1":[0,1],"2":[1,0],"3":[0,0],"4":[0,0],"5":[0,0],"6":[0,0],"7":[0,0],"8":[0,0],"9":[0,0],"10":[0,0],"11":[0,0],"12":[0,1],"13":[0,0],"14":[0,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"ffbed58573fdb70302f2fa5eca63feeee182cc4f","contentHash":"957ac5d6ce8026345907664940444261249abfcba91db614178eece09bbfb910"}}
@@ -1 +0,0 @@
1
- {"parent":"6cdae809-9b2c-44f0-b9c2-41555a0e2af9","pid":99259,"argv":["/Users/jaredwray/.nvm/versions/node/v16.13.1/bin/node","/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo/node_modules/ava/lib/worker/base.js"],"execArgv":["--enable-source-maps"],"cwd":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo","time":1643222020427,"ppid":99258,"coverageFilename":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo/.nyc_output/216ebf05-ecde-4f49-891b-59762203bcc0.json","externalId":"","uuid":"216ebf05-ecde-4f49-891b-59762203bcc0","files":["/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo/src/index.js"]}
@@ -1 +0,0 @@
1
- {"parent":null,"pid":99259,"argv":["/Users/jaredwray/.nvm/versions/node/v16.13.1/bin/node","/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo/node_modules/.bin/ava"],"execArgv":[],"cwd":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo","time":1643222020176,"ppid":99258,"coverageFilename":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo/.nyc_output/6cdae809-9b2c-44f0-b9c2-41555a0e2af9.json","externalId":"","uuid":"6cdae809-9b2c-44f0-b9c2-41555a0e2af9","files":[]}
@@ -1 +0,0 @@
1
- {"processes":{"216ebf05-ecde-4f49-891b-59762203bcc0":{"parent":"6cdae809-9b2c-44f0-b9c2-41555a0e2af9","children":[]},"6cdae809-9b2c-44f0-b9c2-41555a0e2af9":{"parent":null,"children":["216ebf05-ecde-4f49-891b-59762203bcc0"]}},"files":{"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/mongo/src/index.js":["216ebf05-ecde-4f49-891b-59762203bcc0"]},"externalIds":{}}
package/test/test.js DELETED
@@ -1,159 +0,0 @@
1
- const test = require('ava');
2
- const keyvTestSuite = require('@keyv/test-suite').default;
3
- const Keyv = require('keyv');
4
- const KeyvMongo = require('this');
5
- const { keyvOfficialTests } = require('@keyv/test-suite');
6
-
7
- const options = { useNewUrlParser: true, useUnifiedTopology: true, serverSelectionTimeoutMS: 5000 };
8
-
9
- const mongoURL = 'mongodb://127.0.0.1:27017';
10
-
11
- keyvOfficialTests(test, Keyv, mongoURL, 'mongodb://foo', options);
12
-
13
- const store = () => new KeyvMongo(mongoURL, options);
14
- keyvTestSuite(test, Keyv, store);
15
-
16
- test('default options', t => {
17
- const store = new KeyvMongo();
18
- t.deepEqual(store.opts, {
19
- url: mongoURL,
20
- collection: 'keyv',
21
- });
22
- });
23
-
24
- test('default options with url.uri', t => {
25
- const store = new KeyvMongo({ uri: mongoURL });
26
- t.is(store.opts.uri, mongoURL);
27
- t.is(store.opts.url, mongoURL);
28
- });
29
-
30
- test('Collection option merges into default options', t => {
31
- const store = new KeyvMongo({ collection: 'foo' });
32
- t.deepEqual(store.opts, {
33
- url: mongoURL,
34
- collection: 'foo',
35
- });
36
- });
37
-
38
- test('useGridFS option merges into default options', t => {
39
- const store = new KeyvMongo({ useGridFS: true, collection: 'foo' });
40
- t.deepEqual(store.opts, {
41
- url: mongoURL,
42
- useGridFS: true,
43
- collection: 'foo',
44
- });
45
- });
46
-
47
- test('Collection option merges into default options if URL is passed', t => {
48
- const store = new KeyvMongo(mongoURL, { collection: 'foo' });
49
- t.deepEqual(store.opts, {
50
- url: mongoURL,
51
- collection: 'foo',
52
- });
53
- });
54
-
55
- test('.delete() with no args doesn\'t empty the collection', async t => {
56
- const store = new KeyvMongo('mongodb://foo'); // Make sure we don't actually connect
57
- t.false(await store.delete());
58
- });
59
-
60
- test('.delete() with key as number', async t => {
61
- const store = new KeyvMongo(mongoURL, { collection: 'foo' });
62
- t.false(await store.delete(123));
63
- });
64
-
65
- test.serial('Stores value in GridFS', async t => {
66
- const store = new KeyvMongo(Object.assign({
67
- useGridFS: true,
68
- }, options));
69
- const result = await store.set('key1', 'keyv1', 0);
70
- const get = await store.get('key1');
71
- t.is(result.filename, 'key1');
72
- t.is(get, 'keyv1');
73
- });
74
-
75
- test.serial('Gets value from GridFS', async t => {
76
- const store = new KeyvMongo(Object.assign({
77
- useGridFS: true,
78
- }, options));
79
- const result = await store.get('key1');
80
- t.is(result, 'keyv1');
81
- });
82
-
83
- test.serial('Deletes value from GridFS', async t => {
84
- const store = new KeyvMongo(Object.assign({
85
- useGridFS: true,
86
- }, options));
87
- const result = await store.delete('key1');
88
- t.is(result, true);
89
- });
90
-
91
- test.serial('Deletes non existent value from GridFS', async t => {
92
- const store = new KeyvMongo(Object.assign({
93
- useGridFS: true,
94
- }, options));
95
- const result = await store.delete('no-existent-value');
96
- t.is(result, false);
97
- });
98
-
99
- test.serial('Stores value with TTL in GridFS', async t => {
100
- const store = new KeyvMongo(Object.assign({
101
- useGridFS: true,
102
- }, options));
103
- const result = await store.set('key1', 'keyv1', 0);
104
- t.is(result.filename, 'key1');
105
- });
106
-
107
- test.serial('Clears expired value from GridFS', async t => {
108
- const store = new KeyvMongo(Object.assign({
109
- useGridFS: true,
110
- }, options));
111
- const cleared = await store.clearExpired();
112
- t.is(cleared, true);
113
- });
114
-
115
- test.serial('Clears unused files from GridFS', async t => {
116
- const store = new KeyvMongo(Object.assign({
117
- useGridFS: true,
118
- }, options));
119
- const cleared = await store.clearUnusedFor(5);
120
- t.is(cleared, true);
121
- });
122
-
123
- test.serial('Clears expired value only when GridFS options is true', async t => {
124
- const store = new KeyvMongo(Object.assign(options));
125
- const cleared = await store.clearExpired();
126
- t.is(cleared, false);
127
- });
128
-
129
- test.serial('Clears unused files only when GridFS options is true', async t => {
130
- const store = new KeyvMongo(Object.assign(options));
131
- const cleared = await store.clearUnusedFor(5);
132
- t.is(cleared, false);
133
- });
134
-
135
- test.serial('Gets non-existent file and return should be undefined', async t => {
136
- const store = new KeyvMongo(Object.assign({
137
- useGridFS: true,
138
- }, options));
139
- const result = await store.get('non-existent-file');
140
- t.is(typeof result, 'undefined');
141
- });
142
-
143
- test.serial('Non-string keys are not permitted in delete', async t => {
144
- const store = new KeyvMongo(Object.assign({
145
- useGridFS: true,
146
- }, options));
147
- const result = await store.delete({
148
- ok: true,
149
- });
150
- t.is(result, false);
151
- });
152
-
153
- test.serial('Clears entire cache store', async t => {
154
- const store = new KeyvMongo(Object.assign({
155
- useGridFS: true,
156
- }, options));
157
- const result = await store.clear();
158
- t.is(typeof result, 'undefined');
159
- });