@keyv/mongo 2.1.6 → 2.2.8

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/README.md CHANGED
@@ -3,8 +3,9 @@
3
3
  > MongoDB storage adapter for Keyv
4
4
 
5
5
  [![build](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml)
6
- [![codecov](https://codecov.io/gh/jaredwray/keyv/branch/master/graph/badge.svg?token=bRzR3RyOXZ)](https://codecov.io/gh/jaredwray/keyv)
6
+ [![codecov](https://codecov.io/gh/jaredwray/keyv/branch/main/graph/badge.svg?token=bRzR3RyOXZ)](https://codecov.io/gh/jaredwray/keyv)
7
7
  [![npm](https://img.shields.io/npm/v/@keyv/mongo.svg)](https://www.npmjs.com/package/@keyv/mongo)
8
+ [![npm](https://img.shields.io/npm/dm/@keyv/mongo)](https://npmjs.com/package/@keyv/mongo)
8
9
 
9
10
  MongoDB storage adapter for [Keyv](https://github.com/jaredwray/keyv).
10
11
 
@@ -35,4 +36,4 @@ const keyv = new Keyv('mongodb://user:pass@localhost:27017/dbname', { collection
35
36
 
36
37
  ## License
37
38
 
38
- MIT © Jared Wray & Luke Childs
39
+ MIT © Jared Wray
@@ -0,0 +1,21 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
3
+ import { type ClearExpiredOutput, type ClearOutput, type ClearUnusedForOutput, type DeleteManyOutput, type DeleteOutput, type GetManyOutput, type GetOutput, type HasOutput, type KeyvMongoConnect, type KeyvMongoOptions, type Options, type SetOutput } from './types';
4
+ declare class KeyvMongo<Value = any> extends EventEmitter {
5
+ ttlSupport: boolean;
6
+ opts: Options;
7
+ connect: Promise<KeyvMongoConnect>;
8
+ namespace?: string;
9
+ constructor(url?: KeyvMongoOptions, options?: Options);
10
+ get(key: string): GetOutput<Value>;
11
+ getMany(keys: string[]): GetManyOutput<Value>;
12
+ set(key: string, value: Value, ttl?: number): SetOutput;
13
+ delete(key: string): DeleteOutput;
14
+ deleteMany(keys: string[]): DeleteManyOutput;
15
+ clear(): ClearOutput;
16
+ clearExpired(): ClearExpiredOutput;
17
+ clearUnusedFor(seconds: number): ClearUnusedForOutput;
18
+ iterator(namespace?: string): AsyncGenerator<any[], void, undefined>;
19
+ has(key: string): HasOutput;
20
+ }
21
+ export = KeyvMongo;
package/dist/index.js ADDED
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const events_1 = __importDefault(require("events"));
6
+ const buffer_1 = require("buffer");
7
+ const mongodb_1 = require("mongodb");
8
+ const pify_1 = __importDefault(require("pify"));
9
+ const keyvMongoKeys = new Set(['url', 'collection', 'namespace', 'serialize', 'deserialize', 'uri', 'useGridFS', 'dialect']);
10
+ class KeyvMongo extends events_1.default {
11
+ constructor(url, options) {
12
+ super();
13
+ this.ttlSupport = false;
14
+ url = url ?? {};
15
+ if (typeof url === 'string') {
16
+ url = { url };
17
+ }
18
+ if (url.uri) {
19
+ url = { url: url.uri, ...url };
20
+ }
21
+ this.opts = {
22
+ url: 'mongodb://127.0.0.1:27017',
23
+ collection: 'keyv',
24
+ ...url,
25
+ ...options,
26
+ };
27
+ const mongoOptions = Object.fromEntries(Object.entries(this.opts).filter(([k]) => !keyvMongoKeys.has(k)));
28
+ this.opts = Object.fromEntries(Object.entries(this.opts).filter(([k]) => keyvMongoKeys.has(k)));
29
+ // Implementation from sql by lukechilds,
30
+ this.connect = new Promise(resolve => {
31
+ mongodb_1.MongoClient.connect(this.opts.url, mongoOptions, (error, client) => {
32
+ if (error) {
33
+ return this.emit('error', error);
34
+ }
35
+ const db = client.db(this.opts.db);
36
+ if (this.opts.useGridFS) {
37
+ const bucket = new mongodb_1.GridFSBucket(db, {
38
+ readPreference: this.opts.readPreference,
39
+ bucketName: this.opts.collection,
40
+ });
41
+ const store = db.collection(`${this.opts.collection}.files`);
42
+ store.createIndex({
43
+ uploadDate: -1,
44
+ });
45
+ store.createIndex({
46
+ 'metadata.expiresAt': 1,
47
+ });
48
+ store.createIndex({
49
+ 'metadata.lastAccessed': 1,
50
+ });
51
+ for (const method of [
52
+ 'updateOne',
53
+ 'count',
54
+ ]) {
55
+ // @ts-expect-error - method needs to be a string
56
+ store[method] = (0, pify_1.default)(store[method].bind(store));
57
+ }
58
+ for (const method of [
59
+ 'find',
60
+ 'drop',
61
+ ]) {
62
+ // @ts-expect-error - method needs to be a string
63
+ bucket[method] = (0, pify_1.default)(bucket[method].bind(bucket));
64
+ }
65
+ resolve({ bucket, store, db });
66
+ }
67
+ else {
68
+ const store = db.collection(this.opts.collection);
69
+ store.createIndex({ key: 1 }, {
70
+ unique: true,
71
+ background: true,
72
+ });
73
+ store.createIndex({ expiresAt: 1 }, {
74
+ expireAfterSeconds: 0,
75
+ background: true,
76
+ });
77
+ for (const method of [
78
+ 'updateOne',
79
+ 'findOne',
80
+ 'deleteOne',
81
+ 'deleteMany',
82
+ 'count',
83
+ ]) {
84
+ // @ts-expect-error - method needs to be a string
85
+ store[method] = (0, pify_1.default)(store[method].bind(store));
86
+ }
87
+ resolve({ store });
88
+ }
89
+ });
90
+ });
91
+ }
92
+ async get(key) {
93
+ if (this.opts.useGridFS) {
94
+ const client = await this.connect;
95
+ await client.store.updateOne({
96
+ filename: key,
97
+ }, {
98
+ $set: {
99
+ 'metadata.lastAccessed': new Date(),
100
+ },
101
+ });
102
+ const stream = client.bucket.openDownloadStreamByName(key);
103
+ return new Promise(resolve => {
104
+ const resp = [];
105
+ stream.on('error', () => {
106
+ resolve(undefined);
107
+ });
108
+ stream.on('end', () => {
109
+ const data = buffer_1.Buffer.concat(resp).toString('utf8');
110
+ resolve(data);
111
+ });
112
+ stream.on('data', chunk => {
113
+ resp.push(chunk);
114
+ });
115
+ });
116
+ }
117
+ const connect = await this.connect;
118
+ const doc = await connect.store.findOne({ key: { $eq: key } });
119
+ if (!doc) {
120
+ return undefined;
121
+ }
122
+ return doc.value;
123
+ }
124
+ async getMany(keys) {
125
+ if (this.opts.useGridFS) {
126
+ const promises = [];
127
+ for (const key of keys) {
128
+ promises.push(this.get(key));
129
+ }
130
+ const values = await Promise.allSettled(promises);
131
+ const data = [];
132
+ for (const value of values) {
133
+ // @ts-expect-error = value is PromiseFulfilledResult<Value>
134
+ data.push(value.value);
135
+ }
136
+ return data;
137
+ }
138
+ const connect = await this.connect;
139
+ // @ts-expect-error eslint-disable-next-line
140
+ const values = await connect.store.s.db.collection(this.opts.collection)
141
+ .find({ key: { $in: keys } })
142
+ .project({ _id: 0, value: 1, key: 1 })
143
+ .toArray();
144
+ const results = [...keys];
145
+ let i = 0;
146
+ for (const key of keys) {
147
+ const rowIndex = values.findIndex((row) => row.key === key);
148
+ results[i] = rowIndex > -1 ? values[rowIndex].value : undefined;
149
+ i++;
150
+ }
151
+ return results;
152
+ }
153
+ async set(key, value, ttl) {
154
+ const expiresAt = typeof ttl === 'number' ? new Date(Date.now() + ttl) : null;
155
+ if (this.opts.useGridFS) {
156
+ const client = await this.connect;
157
+ const stream = client.bucket.openUploadStream(key, {
158
+ metadata: {
159
+ expiresAt,
160
+ lastAccessed: new Date(),
161
+ },
162
+ });
163
+ return new Promise(resolve => {
164
+ stream.on('finish', () => {
165
+ resolve(stream);
166
+ });
167
+ stream.end(value);
168
+ });
169
+ }
170
+ const client = await this.connect;
171
+ return client.store.updateOne({ key: { $eq: key } }, { $set: { key, value, expiresAt } }, { upsert: true });
172
+ }
173
+ async delete(key) {
174
+ if (typeof key !== 'string') {
175
+ return false;
176
+ }
177
+ const client = await this.connect;
178
+ if (this.opts.useGridFS) {
179
+ try {
180
+ const connection = client.db;
181
+ const bucket = new mongodb_1.GridFSBucket(connection, {
182
+ bucketName: this.opts.collection,
183
+ });
184
+ const files = await bucket.find({ filename: key }).toArray();
185
+ await client.bucket.delete(files[0]._id);
186
+ return true;
187
+ }
188
+ catch {
189
+ return false;
190
+ }
191
+ }
192
+ const object = await client.store.deleteOne({ key: { $eq: key } });
193
+ return object.deletedCount > 0;
194
+ }
195
+ async deleteMany(keys) {
196
+ const client = await this.connect;
197
+ if (this.opts.useGridFS) {
198
+ const connection = client.db;
199
+ const bucket = new mongodb_1.GridFSBucket(connection, {
200
+ bucketName: this.opts.collection,
201
+ });
202
+ const files = await bucket.find({ filename: { $in: keys } }).toArray();
203
+ if (files.length === 0) {
204
+ return false;
205
+ }
206
+ await Promise.all(files.map(async (file) => client.bucket.delete(file._id)));
207
+ return true;
208
+ }
209
+ const object = await client.store.deleteMany({ key: { $in: keys } });
210
+ return object.deletedCount > 0;
211
+ }
212
+ async clear() {
213
+ const client = await this.connect;
214
+ if (this.opts.useGridFS) {
215
+ await client.bucket.drop();
216
+ }
217
+ await client.store.deleteMany({
218
+ key: { $regex: this.namespace ? `^${this.namespace}:*` : '' },
219
+ });
220
+ }
221
+ async clearExpired() {
222
+ if (!this.opts.useGridFS) {
223
+ return false;
224
+ }
225
+ return this.connect.then(async (client) => {
226
+ const connection = client.db;
227
+ const bucket = new mongodb_1.GridFSBucket(connection, {
228
+ bucketName: this.opts.collection,
229
+ });
230
+ return bucket.find({
231
+ 'metadata.expiresAt': {
232
+ $lte: new Date(Date.now()),
233
+ },
234
+ }).toArray()
235
+ .then(async (expiredFiles) => Promise.all(expiredFiles.map(async (file) => client.bucket.delete(file._id))).then(() => true));
236
+ });
237
+ }
238
+ async clearUnusedFor(seconds) {
239
+ if (!this.opts.useGridFS) {
240
+ return false;
241
+ }
242
+ const client = await this.connect;
243
+ const connection = client.db;
244
+ const bucket = new mongodb_1.GridFSBucket(connection, {
245
+ bucketName: this.opts.collection,
246
+ });
247
+ const lastAccessedFiles = await bucket.find({
248
+ 'metadata.lastAccessed': {
249
+ $lte: new Date(Date.now() - (seconds * 1000)),
250
+ },
251
+ }).toArray();
252
+ await Promise.all(lastAccessedFiles.map(async (file) => client.bucket.delete(file._id)));
253
+ return true;
254
+ }
255
+ async *iterator(namespace) {
256
+ const client = await this.connect;
257
+ const iterator = client.store
258
+ .find({
259
+ key: new RegExp(`^${namespace ? namespace + ':' : '.*'}`),
260
+ })
261
+ .map((x) => [x.key, x.value]);
262
+ yield* iterator;
263
+ }
264
+ async has(key) {
265
+ const client = await this.connect;
266
+ const filter = { [this.opts.useGridFS ? 'filename' : 'key']: { $eq: key } };
267
+ const doc = await client.store.count(filter);
268
+ return doc !== 0;
269
+ }
270
+ }
271
+ module.exports = KeyvMongo;
272
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,oDAAkC;AAClC,mCAA8B;AAC9B,qCAA6F;AAC7F,gDAAwB;AAexB,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,KAAK,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC,CAAC;AAC7H,MAAM,SAAuB,SAAQ,gBAAY;IAMhD,YAAY,GAAsB,EAAE,OAAiB;QACpD,KAAK,EAAE,CAAC;QANT,eAAU,GAAG,KAAK,CAAC;QAOlB,GAAG,GAAG,GAAG,IAAI,EAAE,CAAC;QAChB,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC5B,GAAG,GAAG,EAAC,GAAG,EAAC,CAAC;SACZ;QAED,IAAI,GAAG,CAAC,GAAG,EAAE;YACZ,GAAG,GAAG,EAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,EAAC,CAAC;SAC7B;QAED,IAAI,CAAC,IAAI,GAAG;YACX,GAAG,EAAE,2BAA2B;YAChC,UAAU,EAAE,MAAM;YAClB,GAAG,GAAG;YACN,GAAG,OAAO;SACV,CAAC;QAEF,MAAM,YAAY,GAAG,MAAM,CAAC,WAAW,CACtC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC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,GAAG,CAAC,KAAK,EAAC,IAAI,EAAC,EAAE,CAAC,MAAM,CAAC,MAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACxF,OAAO,IAAI,CAAC;IACb,CAAC;IAED,KAAK,CAAC,CAAE,QAAQ,CAAC,SAAkB;QAClC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC;QAClC,MAAM,QAAQ,GAAG,MAAM,CAAC,KAAK;aAC3B,IAAI,CAAC;YACL,GAAG,EAAE,IAAI,MAAM,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;SACzD,CAAC;aACD,GAAG,CAAC,CAAC,CAAmB,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;QAEjD,KAAM,CAAC,CAAC,QAAQ,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW;QACpB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC;QAClC,MAAM,MAAM,GAAG,EAAC,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,EAAC,GAAG,EAAE,GAAG,EAAC,EAAC,CAAC;QACxE,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC7C,OAAO,GAAG,KAAK,CAAC,CAAC;IAClB,CAAC;CACD;AAED,iBAAS,SAAS,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { type Collection, type Db, type GridFSBucket, type ReadPreference } from 'mongodb';
2
+ import { type StoredData } from 'keyv';
3
+ export type Options = {
4
+ [key: string]: unknown;
5
+ url?: string | undefined;
6
+ collection?: string;
7
+ namespace?: string;
8
+ serialize?: any;
9
+ deserialize?: any;
10
+ useGridFS?: boolean;
11
+ uri?: string;
12
+ dialect?: string;
13
+ db?: string;
14
+ readPreference?: ReadPreference;
15
+ };
16
+ export type KeyvMongoOptions = Options | string;
17
+ export type KeyvMongoConnect = {
18
+ bucket?: GridFSBucket;
19
+ store: Collection;
20
+ db?: Db;
21
+ };
22
+ export type PifyFunction = (...args: any[]) => any;
23
+ export type GetOutput<Value> = Promise<Value | undefined>;
24
+ export type GetManyOutput<Value> = Promise<Array<StoredData<Value | undefined>>>;
25
+ export type SetOutput = Promise<any>;
26
+ export type DeleteOutput = Promise<boolean>;
27
+ export type DeleteManyOutput = Promise<boolean>;
28
+ export type ClearOutput = Promise<void>;
29
+ export type ClearExpiredOutput = Promise<boolean>;
30
+ export type ClearUnusedForOutput = Promise<boolean>;
31
+ export type HasOutput = Promise<boolean>;
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,24 +1,48 @@
1
1
  {
2
2
  "name": "@keyv/mongo",
3
- "version": "2.1.6",
3
+ "version": "2.2.8",
4
4
  "description": "MongoDB storage adapter for Keyv",
5
- "main": "src/index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "scripts": {
7
- "test": "xo && nyc ava",
8
- "coverage": "nyc report --reporter=text-lcov > coverage.lcov",
9
- "clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov"
8
+ "build": "tsc --project tsconfig.dist.json",
9
+ "prepare": "yarn build",
10
+ "test": "xo && c8 ava --serial",
11
+ "test:ci": "xo && ava --serial",
12
+ "clean": "rm -rf node_modules && rm -rf ./coverage"
10
13
  },
11
14
  "xo": {
12
15
  "rules": {
13
16
  "unicorn/prefer-module": 0,
14
17
  "unicorn/no-array-reduce": 0,
15
18
  "unicorn/prefer-object-from-entries": 0,
16
- "unicorn/prefer-node-protocol": 0
19
+ "unicorn/prefer-node-protocol": 0,
20
+ "@typescript-eslint/no-unsafe-assignment": 0,
21
+ "@typescript-eslint/no-unsafe-call": 0,
22
+ "@typescript-eslint/no-unsafe-return": 0,
23
+ "@typescript-eslint/no-unsafe-argument": 0,
24
+ "import/extensions": 0,
25
+ "@typescript-eslint/consistent-type-imports": 0,
26
+ "ava/no-ignored-test-files": [
27
+ "error",
28
+ {
29
+ "extensions": [
30
+ "js",
31
+ "ts"
32
+ ]
33
+ }
34
+ ],
35
+ "@typescript-eslint/naming-convention": 0,
36
+ "@typescript-eslint/no-floating-promises": 0,
37
+ " @typescript-eslint/no-unsafe-argument": 0
17
38
  }
18
39
  },
19
40
  "ava": {
20
41
  "require": [
21
- "requirable"
42
+ "ts-node/register"
43
+ ],
44
+ "extensions": [
45
+ "ts"
22
46
  ]
23
47
  },
24
48
  "repository": {
@@ -44,25 +68,22 @@
44
68
  },
45
69
  "homepage": "https://github.com/jaredwray/keyv",
46
70
  "dependencies": {
47
- "mongodb": "^4.4.1",
71
+ "mongodb": "^4.5.0",
48
72
  "pify": "^5.0.0"
49
73
  },
50
74
  "devDependencies": {
51
75
  "@keyv/test-suite": "*",
52
- "ava": "^4.1.0",
76
+ "@ava/typescript": "^4.0.0",
77
+ "@types/pify": "^5.0.1",
53
78
  "keyv": "*",
54
- "nyc": "^15.1.0",
55
79
  "requirable": "^1.0.5",
56
80
  "this": "^1.1.0",
57
- "tsd": "^0.20.0",
58
- "typescript": "^4.6.3",
59
- "xo": "^0.48.0"
81
+ "tsd": "^0.28.1"
60
82
  },
61
- "tsd" : {
62
- "directory" : "test"
83
+ "tsd": {
84
+ "directory": "test"
63
85
  },
64
- "types": "./src/index.d.ts",
65
86
  "files": [
66
- "src"
87
+ "dist"
67
88
  ]
68
89
  }
package/src/index.d.ts DELETED
@@ -1,37 +0,0 @@
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 DELETED
@@ -1,359 +0,0 @@
1
- 'use strict';
2
-
3
- const EventEmitter = require('events');
4
- const {Buffer} = require('buffer');
5
- const mongoClient = require('mongodb').MongoClient;
6
- const {GridFSBucket} = require('mongodb');
7
- const pify = require('pify');
8
-
9
- const keyvMongoKeys = new Set(['url', 'collection', 'namespace', 'serialize', 'deserialize', 'uri', 'useGridFS', 'dialect']);
10
- class KeyvMongo extends EventEmitter {
11
- constructor(url, options) {
12
- super();
13
- this.ttlSupport = false;
14
- url = url || {};
15
- if (typeof url === 'string') {
16
- url = {url};
17
- }
18
-
19
- if (url.uri) {
20
- url = {url: url.uri, ...url};
21
- }
22
-
23
- this.opts = {
24
- url: 'mongodb://127.0.0.1:27017',
25
- collection: 'keyv',
26
- ...url,
27
- ...options,
28
- };
29
-
30
- const mongoOptions = Object.fromEntries(
31
- Object.entries(this.opts).filter(
32
- ([k]) => !keyvMongoKeys.has(k),
33
- ),
34
- );
35
-
36
- this.opts = Object.fromEntries(
37
- Object.entries(this.opts).filter(
38
- ([k]) => keyvMongoKeys.has(k),
39
- ),
40
- );
41
-
42
- // Implementation from sql by lukechilds,
43
- this.connect = new Promise(resolve => {
44
- mongoClient.connect(this.opts.url, mongoOptions, (error, client) => {
45
- if (error) {
46
- return this.emit('error', error);
47
- }
48
-
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
- });
68
-
69
- for (const method of [
70
- 'updateOne',
71
- 'count',
72
- ]) {
73
- this.store[method] = pify(this.store[method].bind(this.store));
74
- }
75
-
76
- for (const method of [
77
- 'find',
78
- 'drop',
79
- ]) {
80
- this.bucket[method] = pify(this.bucket[method].bind(this.bucket));
81
- }
82
-
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));
109
- }
110
-
111
- resolve(this.store);
112
- }
113
- });
114
- });
115
- }
116
-
117
- get(key) {
118
- if (this.opts.useGridFS) {
119
- return this.connect.then(client => {
120
- client.store.updateOne({
121
- filename: key,
122
- }, {
123
- $set: {
124
- 'metadata.lastAccessed': new Date(),
125
- },
126
- });
127
-
128
- const stream = client.bucket.openDownloadStreamByName(key);
129
- return new Promise(resolve => {
130
- let resp = [];
131
- stream.on('error', () => resolve());
132
-
133
- stream.on('end', () => {
134
- resp = Buffer.concat(resp).toString('utf-8');
135
- resolve(resp);
136
- });
137
-
138
- stream.on('data', chunk => {
139
- resp.push(chunk);
140
- });
141
- });
142
- });
143
- }
144
-
145
- return this.connect.then(store =>
146
- store.findOne({key: {$eq: key}}).then(doc => {
147
- if (!doc) {
148
- return undefined;
149
- }
150
-
151
- return doc.value;
152
- }),
153
- );
154
- }
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
-
198
- set(key, value, ttl) {
199
- const expiresAt = typeof ttl === 'number' ? new Date(Date.now() + ttl) : null;
200
-
201
- if (this.opts.useGridFS) {
202
- return this.connect.then(client => {
203
- const stream = client.bucket.openUploadStream(key, {
204
- metadata: {
205
- expiresAt,
206
- lastAccessed: new Date(),
207
- },
208
- });
209
-
210
- return new Promise(resolve => {
211
- stream.on('finish', () => {
212
- resolve(stream);
213
- });
214
- stream.end(value);
215
- });
216
- });
217
- }
218
-
219
- return this.connect.then(store =>
220
- store.updateOne(
221
- {key: {$eq: key}},
222
- {$set: {key, value, expiresAt}},
223
- {upsert: true},
224
- ),
225
- );
226
- }
227
-
228
- delete(key) {
229
- if (typeof key !== 'string') {
230
- return Promise.resolve(false);
231
- }
232
-
233
- if (this.opts.useGridFS) {
234
- return this.connect.then(client => {
235
- const connection = client.db;
236
- const bucket = new GridFSBucket(connection, {
237
- bucketName: this.opts.collection,
238
- });
239
- return bucket.find({filename: key}).toArray()
240
- .then(files => client.bucket.delete(files[0]._id).then(() => true))
241
- .catch(() => false);
242
- });
243
- }
244
-
245
- return this.connect.then(store =>
246
- store
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}})
275
- .then(object => object.deletedCount > 0),
276
- );
277
- }
278
-
279
- clear() {
280
- if (this.opts.useGridFS) {
281
- return this.connect.then(client => client.bucket.drop().then(() => undefined));
282
- }
283
-
284
- return this.connect.then(store =>
285
- store
286
- .deleteMany({
287
- key: {$regex: this.namespace ? `^${this.namespace}:*` : ''},
288
- })
289
- .then(() => undefined),
290
- );
291
- }
292
-
293
- clearExpired() {
294
- if (!this.opts.useGridFS) {
295
- return false;
296
- }
297
-
298
- return this.connect.then(client => {
299
- const connection = client.db;
300
- const bucket = new GridFSBucket(connection, {
301
- bucketName: this.opts.collection,
302
- });
303
-
304
- return bucket.find({
305
- 'metadata.expiresAt': {
306
- $lte: new Date(Date.now()),
307
- },
308
- }).toArray()
309
- .then(expiredFiles => Promise.all(expiredFiles.map(file => client.bucket.delete(file._id))).then(() => true));
310
- });
311
- }
312
-
313
- clearUnusedFor(seconds) {
314
- if (!this.opts.useGridFS) {
315
- return false;
316
- }
317
-
318
- return this.connect.then(client => {
319
- const connection = client.db;
320
- const bucket = new GridFSBucket(connection, {
321
- bucketName: this.opts.collection,
322
- });
323
-
324
- return bucket.find({
325
- 'metadata.lastAccessed': {
326
- $lte: new Date(Date.now() - (seconds * 1000)),
327
- },
328
- }).toArray()
329
- .then(lastAccessedFiles => Promise.all(lastAccessedFiles.map(file => client.bucket.delete(file._id))).then(() => true));
330
- });
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
- }
357
- }
358
-
359
- module.exports = KeyvMongo;