| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import Datastore from "@seald-io/nedb";
- import path from 'node:path';
- import { fileURLToPath } from 'node:url';
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- export default class DB {
- constructor(dbName) {
- this.dbName = dbName;
- }
- init() {
- this.database = {
- users: new Datastore({
- filename: path.join(__dirname, this.dbName.toLowerCase()+'.db'),
- autoload: true
- })
- };
- }
- async count(name) {
- // Return the count as a Promise to align with typical async patterns
- // If your collection API uses callbacks, you can adapt accordingly.
- const collection = this.database[name];
- return collection.count();
- }
- async get(name) {
- return this.database[name].find({});
- }
- async add(name, doc) {
- return this.database[name].insert(doc);
- }
- async remove(name, id) {
- return this.database[name].remove({ _id: id });
- }
- async find(name, query) {
- return this.database[name].findOne(query);
- }
- }
|