sided-db
Version:
Minimal wrapper of MongoDB
159 lines • 5.58 kB
JavaScript
import { MongoClient } from 'mongodb';
export class Module {
constructor(module, collection) {
this.moduleName = module;
this.name = module.module_name;
this.collection = collection;
}
async set(key, value) {
await this.collection.updateOne({ module_name: this.name }, { $set: { [key]: value } });
return value;
}
async get(key) {
return (await this.collection.findOne({ module_name: this.name }))[key];
}
async keys() {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.keys(res);
}
async values() {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.values(res);
}
async entries() {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.entries(res);
}
async delete(key) {
return await this.collection.updateOne({ module_name: this.name }, { $unset: { [key]: '' } });
}
async clear() {
return await this.collection.deleteOne({ module_name: this.name });
}
async has(key) {
const res = await this.collection.findOne({ module_name: this.name });
return res[key] !== undefined;
}
async size() {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.keys(res).length;
}
async empty() {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.keys(res).length === 0;
}
async forEach(callback) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
for (const key in res) {
callback(res[key], key);
}
}
async forEachAsync(callback) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
for (const key in res) {
await callback(res[key], key);
}
}
async map(callback) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.values(res).map(callback);
}
async filter(callback) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.values(res).filter(callback);
}
async some(callback) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.values(res).some(callback);
}
async every(callback) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.values(res).every(callback);
}
async reduce(callback, initialValue) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.values(res).reduce(callback, initialValue);
}
async obj() {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return res;
}
async increment(key, value) {
const res = (await this.collection.findOne({ module_name: this.name }));
const num = (typeof res[key] === 'number' ? res[key] : 0) + value;
return await this.collection.updateOne({ module_name: this.name }, { $set: { [key]: num } });
}
async at(index) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return Object.keys(res).length - 1 >= index
? undefined
: Object.values(res).at(index);
}
async hasAll(...keys) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return keys.every((key) => res[key] !== undefined);
}
async hasAny(...keys) {
const res = await this.collection.findOne({ module_name: this.name });
delete res['_id'];
return keys.some((key) => res[key] !== undefined);
}
}
export class User {
constructor(client, userid, options) {
this.client = client;
this.userid = userid;
this.collection = this.client.db(options.db).collection(userid);
}
async listModules() {
return (await this.collection.find({}).toArray()).map((m) => m.module_name);
}
async getAllModules() {
return await this.collection.find({}).toArray();
}
async module(module_name) {
const module = await this.collection.findOne({ module_name });
if (!module)
this.collection.insertOne({ module_name });
return new Module((await this.collection.findOne({ module_name })), this.collection);
}
}
export class UserDB {
constructor(url, options) {
this.connected = false;
this.url = url;
this.client = new MongoClient(url);
this.db = options.db;
}
static async connect(url, options) {
const db = new UserDB(url, options);
await db.connect();
return db;
}
async connect() {
await this.client.connect();
this.connected = true;
}
getUser(userid) {
if (!this.connected) {
throw new Error('Not connected');
}
return new User(this.client, userid, { db: this.db });
}
}
//# sourceMappingURL=index.js.map