@keyv/mongo 3.0.2 → 3.0.4
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.cjs +61 -27
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +60 -26
- package/package.json +12 -29
package/dist/index.cjs
CHANGED
|
@@ -34,11 +34,21 @@ __export(index_exports, {
|
|
|
34
34
|
default: () => index_default
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(index_exports);
|
|
37
|
-
var
|
|
38
|
-
var
|
|
37
|
+
var import_node_buffer = require("buffer");
|
|
38
|
+
var import_node_events = __toESM(require("events"), 1);
|
|
39
39
|
var import_mongodb = require("mongodb");
|
|
40
|
-
var keyvMongoKeys = /* @__PURE__ */ new Set([
|
|
41
|
-
|
|
40
|
+
var keyvMongoKeys = /* @__PURE__ */ new Set([
|
|
41
|
+
"url",
|
|
42
|
+
"collection",
|
|
43
|
+
"namespace",
|
|
44
|
+
"serialize",
|
|
45
|
+
"deserialize",
|
|
46
|
+
"uri",
|
|
47
|
+
"useGridFS",
|
|
48
|
+
"dialect",
|
|
49
|
+
"db"
|
|
50
|
+
]);
|
|
51
|
+
var KeyvMongo = class extends import_node_events.default {
|
|
42
52
|
ttlSupport = false;
|
|
43
53
|
opts;
|
|
44
54
|
connect;
|
|
@@ -60,16 +70,12 @@ var KeyvMongo = class extends import_events.default {
|
|
|
60
70
|
};
|
|
61
71
|
delete this.opts.emitErrors;
|
|
62
72
|
const mongoOptions = Object.fromEntries(
|
|
63
|
-
Object.entries(this.opts).filter(
|
|
64
|
-
([k]) => !keyvMongoKeys.has(k)
|
|
65
|
-
)
|
|
73
|
+
Object.entries(this.opts).filter(([k]) => !keyvMongoKeys.has(k))
|
|
66
74
|
);
|
|
67
75
|
this.opts = Object.fromEntries(
|
|
68
|
-
Object.entries(this.opts).filter(
|
|
69
|
-
([k]) => keyvMongoKeys.has(k)
|
|
70
|
-
)
|
|
76
|
+
Object.entries(this.opts).filter(([k]) => keyvMongoKeys.has(k))
|
|
71
77
|
);
|
|
72
|
-
this.connect = new Promise(async (resolve,
|
|
78
|
+
this.connect = new Promise(async (resolve, _reject) => {
|
|
73
79
|
try {
|
|
74
80
|
let url2 = "";
|
|
75
81
|
if (this.opts.url) {
|
|
@@ -100,26 +106,34 @@ var KeyvMongo = class extends import_events.default {
|
|
|
100
106
|
collection = this.opts.collection;
|
|
101
107
|
}
|
|
102
108
|
const store = database.collection(collection);
|
|
103
|
-
await store.createIndex(
|
|
104
|
-
|
|
109
|
+
await store.createIndex(
|
|
110
|
+
{ key: 1 },
|
|
111
|
+
{ unique: true, background: true }
|
|
112
|
+
);
|
|
113
|
+
await store.createIndex(
|
|
114
|
+
{ expiresAt: 1 },
|
|
115
|
+
{ expireAfterSeconds: 0, background: true }
|
|
116
|
+
);
|
|
105
117
|
resolve({ store, mongoClient: client });
|
|
106
118
|
}
|
|
107
119
|
} catch (error) {
|
|
108
120
|
this.emit("error", error);
|
|
109
|
-
reject(error);
|
|
110
121
|
}
|
|
111
122
|
});
|
|
112
123
|
}
|
|
113
124
|
async get(key) {
|
|
114
125
|
const client = await this.connect;
|
|
115
126
|
if (this.opts.useGridFS) {
|
|
116
|
-
await client.store.updateOne(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
127
|
+
await client.store.updateOne(
|
|
128
|
+
{
|
|
129
|
+
filename: key
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
$set: {
|
|
133
|
+
"metadata.lastAccessed": /* @__PURE__ */ new Date()
|
|
134
|
+
}
|
|
121
135
|
}
|
|
122
|
-
|
|
136
|
+
);
|
|
123
137
|
const stream = client.bucket.openDownloadStreamByName(key);
|
|
124
138
|
return new Promise((resolve) => {
|
|
125
139
|
const resp = [];
|
|
@@ -127,7 +141,7 @@ var KeyvMongo = class extends import_events.default {
|
|
|
127
141
|
resolve(void 0);
|
|
128
142
|
});
|
|
129
143
|
stream.on("end", () => {
|
|
130
|
-
const data =
|
|
144
|
+
const data = import_node_buffer.Buffer.concat(resp).toString("utf8");
|
|
131
145
|
resolve(data);
|
|
132
146
|
});
|
|
133
147
|
stream.on("data", (chunk) => {
|
|
@@ -155,16 +169,22 @@ var KeyvMongo = class extends import_events.default {
|
|
|
155
169
|
return data;
|
|
156
170
|
}
|
|
157
171
|
const connect = await this.connect;
|
|
158
|
-
const values =
|
|
172
|
+
const values = (
|
|
173
|
+
// @ts-expect-error need to fix this `s`
|
|
174
|
+
await connect.store.s.db.collection(this.opts.collection).find({ key: { $in: keys } }).project({ _id: 0, value: 1, key: 1 }).toArray()
|
|
175
|
+
);
|
|
159
176
|
const results = [...keys];
|
|
160
177
|
let i = 0;
|
|
161
178
|
for (const key of keys) {
|
|
162
|
-
const rowIndex = values.findIndex(
|
|
179
|
+
const rowIndex = values.findIndex(
|
|
180
|
+
(row) => row.key === key
|
|
181
|
+
);
|
|
163
182
|
results[i] = rowIndex > -1 ? values[rowIndex].value : void 0;
|
|
164
183
|
i++;
|
|
165
184
|
}
|
|
166
185
|
return results;
|
|
167
186
|
}
|
|
187
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
168
188
|
async set(key, value, ttl) {
|
|
169
189
|
const expiresAt = typeof ttl === "number" ? new Date(Date.now() + ttl) : null;
|
|
170
190
|
if (this.opts.useGridFS) {
|
|
@@ -221,7 +241,10 @@ var KeyvMongo = class extends import_events.default {
|
|
|
221
241
|
if (files.length === 0) {
|
|
222
242
|
return false;
|
|
223
243
|
}
|
|
224
|
-
await Promise.all(
|
|
244
|
+
await Promise.all(
|
|
245
|
+
// biome-ignore lint/style/noNonNullAssertion: need to fix
|
|
246
|
+
files.map(async (file) => client.bucket.delete(file._id))
|
|
247
|
+
);
|
|
225
248
|
return true;
|
|
226
249
|
}
|
|
227
250
|
const object = await client.store.deleteMany({ key: { $in: keys } });
|
|
@@ -255,7 +278,12 @@ var KeyvMongo = class extends import_events.default {
|
|
|
255
278
|
"metadata.expiresAt": {
|
|
256
279
|
$lte: new Date(Date.now())
|
|
257
280
|
}
|
|
258
|
-
}).toArray().then(
|
|
281
|
+
}).toArray().then(
|
|
282
|
+
async (expiredFiles) => Promise.all(
|
|
283
|
+
// biome-ignore lint/style/noNonNullAssertion: need to fix
|
|
284
|
+
expiredFiles.map(async (file) => client.bucket.delete(file._id))
|
|
285
|
+
).then(() => true)
|
|
286
|
+
);
|
|
259
287
|
});
|
|
260
288
|
}
|
|
261
289
|
async clearUnusedFor(seconds) {
|
|
@@ -272,7 +300,10 @@ var KeyvMongo = class extends import_events.default {
|
|
|
272
300
|
$lte: new Date(Date.now() - seconds * 1e3)
|
|
273
301
|
}
|
|
274
302
|
}).toArray();
|
|
275
|
-
await Promise.all(
|
|
303
|
+
await Promise.all(
|
|
304
|
+
// biome-ignore lint/style/noNonNullAssertion: need to fix
|
|
305
|
+
lastAccessedFiles.map(async (file) => client.bucket.delete(file._id))
|
|
306
|
+
);
|
|
276
307
|
return true;
|
|
277
308
|
}
|
|
278
309
|
async *iterator(namespace) {
|
|
@@ -280,7 +311,10 @@ var KeyvMongo = class extends import_events.default {
|
|
|
280
311
|
const regexp = new RegExp(`^${namespace ? namespace + ":" : ".*"}`);
|
|
281
312
|
const iterator = this.opts.useGridFS ? client.store.find({
|
|
282
313
|
filename: regexp
|
|
283
|
-
}).map(async (x) => [
|
|
314
|
+
}).map(async (x) => [
|
|
315
|
+
x.filename,
|
|
316
|
+
await this.get(x.filename)
|
|
317
|
+
]) : client.store.find({
|
|
284
318
|
key: regexp
|
|
285
319
|
}).map((x) => [x.key, x.value]);
|
|
286
320
|
yield* iterator;
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import EventEmitter from "events";
|
|
3
2
|
import { Buffer } from "buffer";
|
|
3
|
+
import EventEmitter from "events";
|
|
4
4
|
import {
|
|
5
|
-
MongoClient as mongoClient,
|
|
6
5
|
GridFSBucket,
|
|
7
|
-
MongoServerError
|
|
6
|
+
MongoServerError,
|
|
7
|
+
MongoClient as mongoClient
|
|
8
8
|
} from "mongodb";
|
|
9
|
-
var keyvMongoKeys = /* @__PURE__ */ new Set([
|
|
9
|
+
var keyvMongoKeys = /* @__PURE__ */ new Set([
|
|
10
|
+
"url",
|
|
11
|
+
"collection",
|
|
12
|
+
"namespace",
|
|
13
|
+
"serialize",
|
|
14
|
+
"deserialize",
|
|
15
|
+
"uri",
|
|
16
|
+
"useGridFS",
|
|
17
|
+
"dialect",
|
|
18
|
+
"db"
|
|
19
|
+
]);
|
|
10
20
|
var KeyvMongo = class extends EventEmitter {
|
|
11
21
|
ttlSupport = false;
|
|
12
22
|
opts;
|
|
@@ -29,16 +39,12 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
29
39
|
};
|
|
30
40
|
delete this.opts.emitErrors;
|
|
31
41
|
const mongoOptions = Object.fromEntries(
|
|
32
|
-
Object.entries(this.opts).filter(
|
|
33
|
-
([k]) => !keyvMongoKeys.has(k)
|
|
34
|
-
)
|
|
42
|
+
Object.entries(this.opts).filter(([k]) => !keyvMongoKeys.has(k))
|
|
35
43
|
);
|
|
36
44
|
this.opts = Object.fromEntries(
|
|
37
|
-
Object.entries(this.opts).filter(
|
|
38
|
-
([k]) => keyvMongoKeys.has(k)
|
|
39
|
-
)
|
|
45
|
+
Object.entries(this.opts).filter(([k]) => keyvMongoKeys.has(k))
|
|
40
46
|
);
|
|
41
|
-
this.connect = new Promise(async (resolve,
|
|
47
|
+
this.connect = new Promise(async (resolve, _reject) => {
|
|
42
48
|
try {
|
|
43
49
|
let url2 = "";
|
|
44
50
|
if (this.opts.url) {
|
|
@@ -69,26 +75,34 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
69
75
|
collection = this.opts.collection;
|
|
70
76
|
}
|
|
71
77
|
const store = database.collection(collection);
|
|
72
|
-
await store.createIndex(
|
|
73
|
-
|
|
78
|
+
await store.createIndex(
|
|
79
|
+
{ key: 1 },
|
|
80
|
+
{ unique: true, background: true }
|
|
81
|
+
);
|
|
82
|
+
await store.createIndex(
|
|
83
|
+
{ expiresAt: 1 },
|
|
84
|
+
{ expireAfterSeconds: 0, background: true }
|
|
85
|
+
);
|
|
74
86
|
resolve({ store, mongoClient: client });
|
|
75
87
|
}
|
|
76
88
|
} catch (error) {
|
|
77
89
|
this.emit("error", error);
|
|
78
|
-
reject(error);
|
|
79
90
|
}
|
|
80
91
|
});
|
|
81
92
|
}
|
|
82
93
|
async get(key) {
|
|
83
94
|
const client = await this.connect;
|
|
84
95
|
if (this.opts.useGridFS) {
|
|
85
|
-
await client.store.updateOne(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
96
|
+
await client.store.updateOne(
|
|
97
|
+
{
|
|
98
|
+
filename: key
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
$set: {
|
|
102
|
+
"metadata.lastAccessed": /* @__PURE__ */ new Date()
|
|
103
|
+
}
|
|
90
104
|
}
|
|
91
|
-
|
|
105
|
+
);
|
|
92
106
|
const stream = client.bucket.openDownloadStreamByName(key);
|
|
93
107
|
return new Promise((resolve) => {
|
|
94
108
|
const resp = [];
|
|
@@ -124,16 +138,22 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
124
138
|
return data;
|
|
125
139
|
}
|
|
126
140
|
const connect = await this.connect;
|
|
127
|
-
const values =
|
|
141
|
+
const values = (
|
|
142
|
+
// @ts-expect-error need to fix this `s`
|
|
143
|
+
await connect.store.s.db.collection(this.opts.collection).find({ key: { $in: keys } }).project({ _id: 0, value: 1, key: 1 }).toArray()
|
|
144
|
+
);
|
|
128
145
|
const results = [...keys];
|
|
129
146
|
let i = 0;
|
|
130
147
|
for (const key of keys) {
|
|
131
|
-
const rowIndex = values.findIndex(
|
|
148
|
+
const rowIndex = values.findIndex(
|
|
149
|
+
(row) => row.key === key
|
|
150
|
+
);
|
|
132
151
|
results[i] = rowIndex > -1 ? values[rowIndex].value : void 0;
|
|
133
152
|
i++;
|
|
134
153
|
}
|
|
135
154
|
return results;
|
|
136
155
|
}
|
|
156
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
137
157
|
async set(key, value, ttl) {
|
|
138
158
|
const expiresAt = typeof ttl === "number" ? new Date(Date.now() + ttl) : null;
|
|
139
159
|
if (this.opts.useGridFS) {
|
|
@@ -190,7 +210,10 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
190
210
|
if (files.length === 0) {
|
|
191
211
|
return false;
|
|
192
212
|
}
|
|
193
|
-
await Promise.all(
|
|
213
|
+
await Promise.all(
|
|
214
|
+
// biome-ignore lint/style/noNonNullAssertion: need to fix
|
|
215
|
+
files.map(async (file) => client.bucket.delete(file._id))
|
|
216
|
+
);
|
|
194
217
|
return true;
|
|
195
218
|
}
|
|
196
219
|
const object = await client.store.deleteMany({ key: { $in: keys } });
|
|
@@ -224,7 +247,12 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
224
247
|
"metadata.expiresAt": {
|
|
225
248
|
$lte: new Date(Date.now())
|
|
226
249
|
}
|
|
227
|
-
}).toArray().then(
|
|
250
|
+
}).toArray().then(
|
|
251
|
+
async (expiredFiles) => Promise.all(
|
|
252
|
+
// biome-ignore lint/style/noNonNullAssertion: need to fix
|
|
253
|
+
expiredFiles.map(async (file) => client.bucket.delete(file._id))
|
|
254
|
+
).then(() => true)
|
|
255
|
+
);
|
|
228
256
|
});
|
|
229
257
|
}
|
|
230
258
|
async clearUnusedFor(seconds) {
|
|
@@ -241,7 +269,10 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
241
269
|
$lte: new Date(Date.now() - seconds * 1e3)
|
|
242
270
|
}
|
|
243
271
|
}).toArray();
|
|
244
|
-
await Promise.all(
|
|
272
|
+
await Promise.all(
|
|
273
|
+
// biome-ignore lint/style/noNonNullAssertion: need to fix
|
|
274
|
+
lastAccessedFiles.map(async (file) => client.bucket.delete(file._id))
|
|
275
|
+
);
|
|
245
276
|
return true;
|
|
246
277
|
}
|
|
247
278
|
async *iterator(namespace) {
|
|
@@ -249,7 +280,10 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
249
280
|
const regexp = new RegExp(`^${namespace ? namespace + ":" : ".*"}`);
|
|
250
281
|
const iterator = this.opts.useGridFS ? client.store.find({
|
|
251
282
|
filename: regexp
|
|
252
|
-
}).map(async (x) => [
|
|
283
|
+
}).map(async (x) => [
|
|
284
|
+
x.filename,
|
|
285
|
+
await this.get(x.filename)
|
|
286
|
+
]) : client.store.find({
|
|
253
287
|
key: regexp
|
|
254
288
|
}).map((x) => [x.key, x.value]);
|
|
255
289
|
yield* iterator;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keyv/mongo",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "MongoDB storage adapter for Keyv",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -12,27 +12,6 @@
|
|
|
12
12
|
"import": "./dist/index.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
"xo": {
|
|
16
|
-
"rules": {
|
|
17
|
-
"import/no-named-as-default": "off",
|
|
18
|
-
"unicorn/prefer-module": "off",
|
|
19
|
-
"unicorn/prefer-event-target": "off",
|
|
20
|
-
"unicorn/no-array-reduce": "off",
|
|
21
|
-
"unicorn/prefer-object-from-entries": "off",
|
|
22
|
-
"unicorn/prefer-node-protocol": "off",
|
|
23
|
-
"@typescript-eslint/no-unsafe-assignment": "off",
|
|
24
|
-
"@typescript-eslint/no-unsafe-call": "off",
|
|
25
|
-
"@typescript-eslint/no-unsafe-return": "off",
|
|
26
|
-
"@typescript-eslint/no-unsafe-argument": "off",
|
|
27
|
-
"import/extensions": "off",
|
|
28
|
-
"@typescript-eslint/consistent-type-imports": "off",
|
|
29
|
-
"@typescript-eslint/naming-convention": "off",
|
|
30
|
-
"@typescript-eslint/no-floating-promises": "off",
|
|
31
|
-
"import/no-extraneous-dependencies": "off",
|
|
32
|
-
"@typescript-eslint/no-confusing-void-expression": "off",
|
|
33
|
-
"@typescript-eslint/no-empty-function": "off"
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
15
|
"repository": {
|
|
37
16
|
"type": "git",
|
|
38
17
|
"url": "git+https://github.com/jaredwray/keyv.git"
|
|
@@ -56,14 +35,16 @@
|
|
|
56
35
|
},
|
|
57
36
|
"homepage": "https://github.com/jaredwray/keyv",
|
|
58
37
|
"dependencies": {
|
|
59
|
-
"mongodb": "^6.
|
|
38
|
+
"mongodb": "^6.20.0"
|
|
60
39
|
},
|
|
61
40
|
"devDependencies": {
|
|
62
|
-
"
|
|
41
|
+
"@biomejs/biome": "^2.2.4",
|
|
63
42
|
"rimraf": "^6.0.1",
|
|
64
|
-
"tsd": "^0.
|
|
65
|
-
"
|
|
66
|
-
|
|
43
|
+
"tsd": "^0.33.0",
|
|
44
|
+
"@keyv/test-suite": "^2.1.1"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"keyv": "^5.5.3"
|
|
67
48
|
},
|
|
68
49
|
"tsd": {
|
|
69
50
|
"directory": "test"
|
|
@@ -74,8 +55,10 @@
|
|
|
74
55
|
],
|
|
75
56
|
"scripts": {
|
|
76
57
|
"build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
|
|
77
|
-
"
|
|
78
|
-
"
|
|
58
|
+
"lint": "biome check --write --error-on-warnings",
|
|
59
|
+
"lint:ci": "biome check --error-on-warnings",
|
|
60
|
+
"test": "pnpm lint && vitest run --coverage",
|
|
61
|
+
"test:ci": "pnpm lint:ci && vitest --run --sequence.setupFiles=list --coverage",
|
|
79
62
|
"clean": "rimraf ./node_modules ./coverage ./dist"
|
|
80
63
|
}
|
|
81
64
|
}
|