unstorage
Version:
Universal Storage Layer
80 lines (79 loc) • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
module.exports = void 0;
var _utils = require("./utils/index.cjs");
var _mongodb = require("mongodb");
var _default = (0, _utils.defineDriver)(opts => {
let collection;
const getMongoCollection = () => {
if (!collection) {
if (!opts.connectionString) {
throw new Error("[unstorage] MongoDB driver requires a connection string to be provided.");
}
const mongoClient = new _mongodb.MongoClient(opts.connectionString);
const db = mongoClient.db(opts.databaseName || "unstorage");
collection = db.collection(opts.collectionName || "unstorage");
}
return collection;
};
return {
name: "mongodb",
options: opts,
async hasItem(key) {
const result = await getMongoCollection().findOne({
key
});
return !!result;
},
async getItem(key) {
const document = await getMongoCollection().findOne({
key
});
return document?.value ? document.value : null;
},
async setItem(key, value) {
const currentDateTime = /* @__PURE__ */new Date();
await getMongoCollection().findOneAndUpdate({
key
}, {
$set: {
key,
value,
modifiedAt: currentDateTime
},
$setOnInsert: {
createdAt: currentDateTime
}
}, {
upsert: true,
returnDocument: "after"
});
return;
},
async removeItem(key) {
await getMongoCollection().deleteOne({
key
});
},
async getKeys() {
return await getMongoCollection().find().project({
key: true
}).map(d => d.key).toArray();
},
async getMeta(key) {
const document = await getMongoCollection().findOne({
key
});
return {
mtime: document.modifiedAt,
birthtime: document.createdAt
};
},
async clear() {
await getMongoCollection().deleteMany({});
}
};
});
module.exports = _default;