@keyv/mongo 2.1.8 → 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/dist/index.d.ts +21 -0
- package/dist/index.js +272 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +24 -19
- package/src/index.d.ts +0 -40
- package/src/index.js +0 -359
package/dist/index.d.ts
ADDED
|
@@ -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;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C,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"}
|
package/dist/types.d.ts
ADDED
|
@@ -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 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keyv/mongo",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.8",
|
|
4
4
|
"description": "MongoDB storage adapter for Keyv",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
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": {
|
|
@@ -14,6 +17,12 @@
|
|
|
14
17
|
"unicorn/no-array-reduce": 0,
|
|
15
18
|
"unicorn/prefer-object-from-entries": 0,
|
|
16
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,
|
|
17
26
|
"ava/no-ignored-test-files": [
|
|
18
27
|
"error",
|
|
19
28
|
{
|
|
@@ -22,16 +31,17 @@
|
|
|
22
31
|
"ts"
|
|
23
32
|
]
|
|
24
33
|
}
|
|
25
|
-
]
|
|
34
|
+
],
|
|
35
|
+
"@typescript-eslint/naming-convention": 0,
|
|
36
|
+
"@typescript-eslint/no-floating-promises": 0,
|
|
37
|
+
" @typescript-eslint/no-unsafe-argument": 0
|
|
26
38
|
}
|
|
27
39
|
},
|
|
28
40
|
"ava": {
|
|
29
41
|
"require": [
|
|
30
|
-
"requirable",
|
|
31
42
|
"ts-node/register"
|
|
32
43
|
],
|
|
33
44
|
"extensions": [
|
|
34
|
-
"js",
|
|
35
45
|
"ts"
|
|
36
46
|
]
|
|
37
47
|
},
|
|
@@ -63,22 +73,17 @@
|
|
|
63
73
|
},
|
|
64
74
|
"devDependencies": {
|
|
65
75
|
"@keyv/test-suite": "*",
|
|
66
|
-
"ava": "^4.
|
|
76
|
+
"@ava/typescript": "^4.0.0",
|
|
77
|
+
"@types/pify": "^5.0.1",
|
|
67
78
|
"keyv": "*",
|
|
68
|
-
"nyc": "^15.1.0",
|
|
69
79
|
"requirable": "^1.0.5",
|
|
70
80
|
"this": "^1.1.0",
|
|
71
|
-
"
|
|
72
|
-
"@types/keyv": "^3.1.4",
|
|
73
|
-
"tsd": "^0.20.0",
|
|
74
|
-
"typescript": "^4.6.4",
|
|
75
|
-
"xo": "^0.48.0"
|
|
81
|
+
"tsd": "^0.28.1"
|
|
76
82
|
},
|
|
77
|
-
"tsd"
|
|
78
|
-
"directory"
|
|
83
|
+
"tsd": {
|
|
84
|
+
"directory": "test"
|
|
79
85
|
},
|
|
80
|
-
"types": "./src/index.d.ts",
|
|
81
86
|
"files": [
|
|
82
|
-
"
|
|
87
|
+
"dist"
|
|
83
88
|
]
|
|
84
89
|
}
|
package/src/index.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import {EventEmitter} from 'events';
|
|
2
|
-
import GridFSBucket from 'mongodb';
|
|
3
|
-
import {Store, StoredData} from 'keyv';
|
|
4
|
-
|
|
5
|
-
declare class KeyvMongo<Value=any> extends EventEmitter implements Store<Value> {
|
|
6
|
-
readonly ttlSupport: false;
|
|
7
|
-
opts: Record<string, any>;
|
|
8
|
-
connect: Promise<any>;
|
|
9
|
-
db: import('mongodb').Db;
|
|
10
|
-
bucket: GridFSBucket;
|
|
11
|
-
store: import('mongodb').Collection<import('bson').Document>;
|
|
12
|
-
constructor(options?: string | KeyvMongo.Options);
|
|
13
|
-
get(key: string): Promise<Value>;
|
|
14
|
-
getMany?(
|
|
15
|
-
keys: string[]
|
|
16
|
-
): Array<StoredData<Value>> | Promise<Array<StoredData<Value>>> | undefined;
|
|
17
|
-
set(key: string, value: Value, ttl?: number): any;
|
|
18
|
-
delete(key: string): boolean | Promise<boolean>;
|
|
19
|
-
deleteMany(keys: string[]): boolean;
|
|
20
|
-
clear(): void | Promise<void>;
|
|
21
|
-
iterator(namespace: string | undefined): AsyncGenerator<any, void, any>;
|
|
22
|
-
has?(key: string): boolean | Promise<boolean>;
|
|
23
|
-
clearExpired(): boolean | Promise<boolean>;
|
|
24
|
-
clearUnusedFor(seconds: any): boolean | Promise<boolean>;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export = KeyvMongo;
|
|
28
|
-
|
|
29
|
-
declare namespace KeyvMongo {
|
|
30
|
-
interface Options {
|
|
31
|
-
url?: string | undefined;
|
|
32
|
-
collection?: string | undefined;
|
|
33
|
-
namespace?: string | undefined;
|
|
34
|
-
serialize?: any;
|
|
35
|
-
deserialize?: any;
|
|
36
|
-
useGridFS?: boolean | undefined;
|
|
37
|
-
uri?: string | undefined;
|
|
38
|
-
dialect?: string | undefined;
|
|
39
|
-
}
|
|
40
|
-
}
|
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;
|
|
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;
|
|
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;
|