@keyv/mongo
Advanced tools
Comparing version
{ | ||
"name": "@keyv/mongo", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"description": "MongoDB storage adapter for Keyv", | ||
@@ -12,7 +12,7 @@ "main": "src/index.js", | ||
"xo": { | ||
"extends": "xo-lukechilds", | ||
"rules": { | ||
"unicorn/prefer-module": 0, | ||
"unicorn/no-array-reduce": 0, | ||
"unicorn/prefer-object-from-entries": 0 | ||
"unicorn/prefer-object-from-entries": 0, | ||
"unicorn/prefer-node-protocol": 0 | ||
} | ||
@@ -54,3 +54,2 @@ }, | ||
"ava": "^4.1.0", | ||
"eslint-config-xo-lukechilds": "^1.0.1", | ||
"keyv": "*", | ||
@@ -57,0 +56,0 @@ "nyc": "^15.1.0", |
@@ -1,2 +0,2 @@ | ||
import EventEmitter from 'node:events'; | ||
import {EventEmitter} from 'events'; | ||
import GridFSBucket from 'mongodb'; | ||
@@ -3,0 +3,0 @@ |
164
src/index.js
@@ -1,8 +0,7 @@ | ||
// @ts-ignore | ||
'use strict'; | ||
const EventEmitter = require('events'); | ||
const { Buffer } = require('buffer'); | ||
const {Buffer} = require('buffer'); | ||
const mongoClient = require('mongodb').MongoClient; | ||
const { GridFSBucket } = require('mongodb'); | ||
const {GridFSBucket} = require('mongodb'); | ||
const pify = require('pify'); | ||
@@ -17,7 +16,7 @@ | ||
if (typeof url === 'string') { | ||
url = { url }; | ||
url = {url}; | ||
} | ||
if (url.uri) { | ||
url = { url: url.uri, ...url }; | ||
url = {url: url.uri, ...url}; | ||
} | ||
@@ -46,73 +45,72 @@ | ||
this.connect = new Promise(resolve => { | ||
mongoClient.connect(this.opts.url, mongoOptions | ||
, (error, client) => { | ||
if (error) { | ||
return this.emit('error', error); | ||
} | ||
mongoClient.connect(this.opts.url, mongoOptions, (error, client) => { | ||
if (error) { | ||
return this.emit('error', error); | ||
} | ||
this.db = client.db(this.opts.db); | ||
if (this.opts.useGridFS) { | ||
this.bucket = new GridFSBucket(this.db, { | ||
readPreference: this.opts.readPreference || 'primary', | ||
bucketName: this.opts.collection, | ||
}); | ||
this.store = this.db.collection(this.opts.collection + '.files'); | ||
this.store.createIndex({ | ||
filename: 'hashed', | ||
}); | ||
this.store.createIndex({ | ||
uploadDate: -1, | ||
}); | ||
this.store.createIndex({ | ||
'metadata.expiresAt': 1, | ||
}); | ||
this.store.createIndex({ | ||
'metadata.lastAccessed': 1, | ||
}); | ||
this.db = client.db(this.opts.db); | ||
if (this.opts.useGridFS) { | ||
this.bucket = new GridFSBucket(this.db, { | ||
readPreference: this.opts.readPreference || 'primary', | ||
bucketName: this.opts.collection, | ||
}); | ||
this.store = this.db.collection(this.opts.collection + '.files'); | ||
this.store.createIndex({ | ||
filename: 'hashed', | ||
}); | ||
this.store.createIndex({ | ||
uploadDate: -1, | ||
}); | ||
this.store.createIndex({ | ||
'metadata.expiresAt': 1, | ||
}); | ||
this.store.createIndex({ | ||
'metadata.lastAccessed': 1, | ||
}); | ||
for (const method of [ | ||
'updateOne', | ||
'count', | ||
]) { | ||
this.store[method] = pify(this.store[method].bind(this.store)); | ||
} | ||
for (const method of [ | ||
'updateOne', | ||
'count', | ||
]) { | ||
this.store[method] = pify(this.store[method].bind(this.store)); | ||
} | ||
for (const method of [ | ||
'find', | ||
'drop', | ||
]) { | ||
this.bucket[method] = pify(this.bucket[method].bind(this.bucket)); | ||
} | ||
for (const method of [ | ||
'find', | ||
'drop', | ||
]) { | ||
this.bucket[method] = pify(this.bucket[method].bind(this.bucket)); | ||
} | ||
resolve({ bucket: this.bucket, store: this.store, db: this.db }); | ||
} else { | ||
this.store = this.db.collection(this.opts.collection); | ||
this.store.createIndex( | ||
{ key: 1 }, | ||
{ | ||
unique: true, | ||
background: true, | ||
}, | ||
); | ||
this.store.createIndex( | ||
{ expiresAt: 1 }, | ||
{ | ||
expireAfterSeconds: 0, | ||
background: true, | ||
}, | ||
); | ||
resolve({bucket: this.bucket, store: this.store, db: this.db}); | ||
} else { | ||
this.store = this.db.collection(this.opts.collection); | ||
this.store.createIndex( | ||
{key: 1}, | ||
{ | ||
unique: true, | ||
background: true, | ||
}, | ||
); | ||
this.store.createIndex( | ||
{expiresAt: 1}, | ||
{ | ||
expireAfterSeconds: 0, | ||
background: true, | ||
}, | ||
); | ||
for (const method of [ | ||
'updateOne', | ||
'findOne', | ||
'deleteOne', | ||
'deleteMany', | ||
'count', | ||
]) { | ||
this.store[method] = pify(this.store[method].bind(this.store)); | ||
} | ||
for (const method of [ | ||
'updateOne', | ||
'findOne', | ||
'deleteOne', | ||
'deleteMany', | ||
'count', | ||
]) { | ||
this.store[method] = pify(this.store[method].bind(this.store)); | ||
} | ||
resolve(this.store); | ||
} | ||
}); | ||
resolve(this.store); | ||
} | ||
}); | ||
}); | ||
@@ -150,3 +148,3 @@ } | ||
return this.connect.then(store => | ||
store.findOne({ key: { $eq: key } }).then(doc => { | ||
store.findOne({key: {$eq: key}}).then(doc => { | ||
if (!doc) { | ||
@@ -182,4 +180,4 @@ return undefined; | ||
store.s.db.collection(this.opts.collection) | ||
.find({ key: { $in: keys } }) | ||
.project({ _id: 0, value: 1, key: 1 }) | ||
.find({key: {$in: keys}}) | ||
.project({_id: 0, value: 1, key: 1}) | ||
.toArray().then(values => { | ||
@@ -227,5 +225,5 @@ let i = 0; | ||
store.updateOne( | ||
{ key: { $eq: key } }, | ||
{ $set: { key, value, expiresAt } }, | ||
{ upsert: true }, | ||
{key: {$eq: key}}, | ||
{$set: {key, value, expiresAt}}, | ||
{upsert: true}, | ||
), | ||
@@ -246,3 +244,3 @@ ); | ||
}); | ||
return bucket.find({ filename: key }).toArray() | ||
return bucket.find({filename: key}).toArray() | ||
.then(files => client.bucket.delete(files[0]._id).then(() => true)) | ||
@@ -255,3 +253,3 @@ .catch(() => false); | ||
store | ||
.deleteOne({ key: { $eq: key } }) | ||
.deleteOne({key: {$eq: key}}) | ||
.then(object => object.deletedCount > 0), | ||
@@ -268,3 +266,3 @@ ); | ||
}); | ||
return bucket.find({ filename: { $in: keys } }).toArray() | ||
return bucket.find({filename: {$in: keys}}).toArray() | ||
.then( | ||
@@ -284,3 +282,3 @@ files => { | ||
store | ||
.deleteMany({ key: { $in: keys } }) | ||
.deleteMany({key: {$in: keys}}) | ||
.then(object => object.deletedCount > 0), | ||
@@ -298,3 +296,3 @@ ); | ||
.deleteMany({ | ||
key: { $regex: this.namespace ? `^${this.namespace}:*` : '' }, | ||
key: {$regex: this.namespace ? `^${this.namespace}:*` : ''}, | ||
}) | ||
@@ -359,3 +357,3 @@ .then(() => undefined), | ||
return this.connect.then(client => client.store.count( | ||
{ filename: { $eq: key } }, | ||
{filename: {$eq: key}}, | ||
).then(doc => doc !== 0)); | ||
@@ -366,3 +364,3 @@ } | ||
store.count( | ||
{ key: { $eq: key } }, | ||
{key: {$eq: key}}, | ||
), | ||
@@ -369,0 +367,0 @@ ).then(doc => doc !== 0); |
9
-10%11773
-1.49%345
-0.58%