@keyv/mongo 3.0.3 → 3.0.5
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 -26
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +60 -25
- 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,8 +106,14 @@ 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) {
|
|
@@ -112,13 +124,16 @@ var KeyvMongo = class extends import_events.default {
|
|
|
112
124
|
async get(key) {
|
|
113
125
|
const client = await this.connect;
|
|
114
126
|
if (this.opts.useGridFS) {
|
|
115
|
-
await client.store.updateOne(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
127
|
+
await client.store.updateOne(
|
|
128
|
+
{
|
|
129
|
+
filename: String(key)
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
$set: {
|
|
133
|
+
"metadata.lastAccessed": /* @__PURE__ */ new Date()
|
|
134
|
+
}
|
|
120
135
|
}
|
|
121
|
-
|
|
136
|
+
);
|
|
122
137
|
const stream = client.bucket.openDownloadStreamByName(key);
|
|
123
138
|
return new Promise((resolve) => {
|
|
124
139
|
const resp = [];
|
|
@@ -126,7 +141,7 @@ var KeyvMongo = class extends import_events.default {
|
|
|
126
141
|
resolve(void 0);
|
|
127
142
|
});
|
|
128
143
|
stream.on("end", () => {
|
|
129
|
-
const data =
|
|
144
|
+
const data = import_node_buffer.Buffer.concat(resp).toString("utf8");
|
|
130
145
|
resolve(data);
|
|
131
146
|
});
|
|
132
147
|
stream.on("data", (chunk) => {
|
|
@@ -154,16 +169,22 @@ var KeyvMongo = class extends import_events.default {
|
|
|
154
169
|
return data;
|
|
155
170
|
}
|
|
156
171
|
const connect = await this.connect;
|
|
157
|
-
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
|
+
);
|
|
158
176
|
const results = [...keys];
|
|
159
177
|
let i = 0;
|
|
160
178
|
for (const key of keys) {
|
|
161
|
-
const rowIndex = values.findIndex(
|
|
179
|
+
const rowIndex = values.findIndex(
|
|
180
|
+
(row) => row.key === key
|
|
181
|
+
);
|
|
162
182
|
results[i] = rowIndex > -1 ? values[rowIndex].value : void 0;
|
|
163
183
|
i++;
|
|
164
184
|
}
|
|
165
185
|
return results;
|
|
166
186
|
}
|
|
187
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
167
188
|
async set(key, value, ttl) {
|
|
168
189
|
const expiresAt = typeof ttl === "number" ? new Date(Date.now() + ttl) : null;
|
|
169
190
|
if (this.opts.useGridFS) {
|
|
@@ -220,7 +241,10 @@ var KeyvMongo = class extends import_events.default {
|
|
|
220
241
|
if (files.length === 0) {
|
|
221
242
|
return false;
|
|
222
243
|
}
|
|
223
|
-
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
|
+
);
|
|
224
248
|
return true;
|
|
225
249
|
}
|
|
226
250
|
const object = await client.store.deleteMany({ key: { $in: keys } });
|
|
@@ -254,7 +278,12 @@ var KeyvMongo = class extends import_events.default {
|
|
|
254
278
|
"metadata.expiresAt": {
|
|
255
279
|
$lte: new Date(Date.now())
|
|
256
280
|
}
|
|
257
|
-
}).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
|
+
);
|
|
258
287
|
});
|
|
259
288
|
}
|
|
260
289
|
async clearUnusedFor(seconds) {
|
|
@@ -271,7 +300,10 @@ var KeyvMongo = class extends import_events.default {
|
|
|
271
300
|
$lte: new Date(Date.now() - seconds * 1e3)
|
|
272
301
|
}
|
|
273
302
|
}).toArray();
|
|
274
|
-
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
|
+
);
|
|
275
307
|
return true;
|
|
276
308
|
}
|
|
277
309
|
async *iterator(namespace) {
|
|
@@ -279,7 +311,10 @@ var KeyvMongo = class extends import_events.default {
|
|
|
279
311
|
const regexp = new RegExp(`^${namespace ? namespace + ":" : ".*"}`);
|
|
280
312
|
const iterator = this.opts.useGridFS ? client.store.find({
|
|
281
313
|
filename: regexp
|
|
282
|
-
}).map(async (x) => [
|
|
314
|
+
}).map(async (x) => [
|
|
315
|
+
x.filename,
|
|
316
|
+
await this.get(x.filename)
|
|
317
|
+
]) : client.store.find({
|
|
283
318
|
key: regexp
|
|
284
319
|
}).map((x) => [x.key, x.value]);
|
|
285
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,8 +75,14 @@ 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) {
|
|
@@ -81,13 +93,16 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
81
93
|
async get(key) {
|
|
82
94
|
const client = await this.connect;
|
|
83
95
|
if (this.opts.useGridFS) {
|
|
84
|
-
await client.store.updateOne(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
96
|
+
await client.store.updateOne(
|
|
97
|
+
{
|
|
98
|
+
filename: String(key)
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
$set: {
|
|
102
|
+
"metadata.lastAccessed": /* @__PURE__ */ new Date()
|
|
103
|
+
}
|
|
89
104
|
}
|
|
90
|
-
|
|
105
|
+
);
|
|
91
106
|
const stream = client.bucket.openDownloadStreamByName(key);
|
|
92
107
|
return new Promise((resolve) => {
|
|
93
108
|
const resp = [];
|
|
@@ -123,16 +138,22 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
123
138
|
return data;
|
|
124
139
|
}
|
|
125
140
|
const connect = await this.connect;
|
|
126
|
-
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
|
+
);
|
|
127
145
|
const results = [...keys];
|
|
128
146
|
let i = 0;
|
|
129
147
|
for (const key of keys) {
|
|
130
|
-
const rowIndex = values.findIndex(
|
|
148
|
+
const rowIndex = values.findIndex(
|
|
149
|
+
(row) => row.key === key
|
|
150
|
+
);
|
|
131
151
|
results[i] = rowIndex > -1 ? values[rowIndex].value : void 0;
|
|
132
152
|
i++;
|
|
133
153
|
}
|
|
134
154
|
return results;
|
|
135
155
|
}
|
|
156
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
136
157
|
async set(key, value, ttl) {
|
|
137
158
|
const expiresAt = typeof ttl === "number" ? new Date(Date.now() + ttl) : null;
|
|
138
159
|
if (this.opts.useGridFS) {
|
|
@@ -189,7 +210,10 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
189
210
|
if (files.length === 0) {
|
|
190
211
|
return false;
|
|
191
212
|
}
|
|
192
|
-
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
|
+
);
|
|
193
217
|
return true;
|
|
194
218
|
}
|
|
195
219
|
const object = await client.store.deleteMany({ key: { $in: keys } });
|
|
@@ -223,7 +247,12 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
223
247
|
"metadata.expiresAt": {
|
|
224
248
|
$lte: new Date(Date.now())
|
|
225
249
|
}
|
|
226
|
-
}).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
|
+
);
|
|
227
256
|
});
|
|
228
257
|
}
|
|
229
258
|
async clearUnusedFor(seconds) {
|
|
@@ -240,7 +269,10 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
240
269
|
$lte: new Date(Date.now() - seconds * 1e3)
|
|
241
270
|
}
|
|
242
271
|
}).toArray();
|
|
243
|
-
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
|
+
);
|
|
244
276
|
return true;
|
|
245
277
|
}
|
|
246
278
|
async *iterator(namespace) {
|
|
@@ -248,7 +280,10 @@ var KeyvMongo = class extends EventEmitter {
|
|
|
248
280
|
const regexp = new RegExp(`^${namespace ? namespace + ":" : ".*"}`);
|
|
249
281
|
const iterator = this.opts.useGridFS ? client.store.find({
|
|
250
282
|
filename: regexp
|
|
251
|
-
}).map(async (x) => [
|
|
283
|
+
}).map(async (x) => [
|
|
284
|
+
x.filename,
|
|
285
|
+
await this.get(x.filename)
|
|
286
|
+
]) : client.store.find({
|
|
252
287
|
key: regexp
|
|
253
288
|
}).map((x) => [x.key, x.value]);
|
|
254
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.5",
|
|
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.5",
|
|
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
|
}
|