38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
import { MongoClient } from 'mongodb';
|
|
|
|
if (!process.env.MONGODB_URI) {
|
|
throw new Error('MONGODB_URI is not set in the environment variables.');
|
|
}
|
|
|
|
const client = new MongoClient(process.env.MONGODB_URI);
|
|
await client.connect();
|
|
|
|
const database = client.db('butler_db');
|
|
const collection = database.collection('GameFeatures');
|
|
|
|
export async function listGameNames() {
|
|
return collection.find({}, { projection: { game: 1 } }).toArray();
|
|
}
|
|
|
|
export async function getGame(game) {
|
|
return collection.findOne({ game });
|
|
}
|
|
|
|
export async function setGame(game, key, value) {
|
|
// If game is not found, create a new document with game field set to game, and key field set to value.
|
|
// Overwrite the value of the key field if it already exists.
|
|
return collection.updateOne(
|
|
{ game },
|
|
{ $set: { game, [key]: value } },
|
|
{ upsert: true }
|
|
);
|
|
}
|
|
|
|
export async function deleteGame(game) {
|
|
return collection.deleteOne({ game });
|
|
}
|
|
|
|
export async function deleteField(game, key) {
|
|
return collection.updateOne({ game }, { $unset: { [key]: '' } });
|
|
}
|