bot/src/util/Database.ts

33 lines
701 B
TypeScript
Raw Normal View History

2020-02-01 23:23:36 +00:00
import { connect, Db, MongoClientOptions, Collection } from 'mongodb';
import { User } from '../models/User';
import { Guild } from '../models/Guild';
interface DatabaseConfig {
url: string;
name: string;
MongoOptions?: MongoClientOptions;
}
export class Database {
db!: Db;
constructor(protected config: DatabaseConfig) {}
async connect() {
const client = await connect(
this.config.url,
this.config.MongoOptions
).catch(err => {
throw err;
});
this.db = client.db(this.config.name);
}
2020-02-01 23:23:36 +00:00
get guilds(): Collection<Guild> {
return this.db.collection('guilds');
}
2020-02-01 23:23:36 +00:00
get users(): Collection<User> {
return this.db.collection('users');
}
}