Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Collection, Filter, FindCursor, MongoClient, OptionalId, WithId } from 'mongodb';
- const uri = 'mongodb://127.1:27017/';
- const client = new MongoClient(uri);
- client.connect();
- const db = client.db('SMokeBot');
- export class DB {
- collection: Collection<Document>
- constructor(collName: string) {
- this.collection = db.collection(collName);
- }
- async addOne(obj: OptionalId<Document>): Promise<void> {
- await this.collection.insertOne(obj);
- };
- async deleteOne(findParams: Filter<Document>): Promise<void> {
- const find: WithId<Document> = (await this.collection.findOne(findParams))!;
- await this.collection.deleteOne(find);
- };
- async editOne(findParam: Filter<Document>, pValue: any, pName: string): Promise<void> {
- const user: WithId<Document> | null = await this.collection.findOne(findParam);
- const param = {};
- param[pName] = pValue;
- await this.collection.updateOne({ _id: user!._id }, { $set: param });
- };
- async find(findParams: Filter<Document>): Promise<WithId<Document>> {
- return new Promise(async(resolve: (value: WithId<Document> | PromiseLike<WithId<Document>>) => void , reject: (reason?: any) => void): Promise<void> => {
- const find: WithId<Document> | null = await this.collection.findOne(findParams);
- resolve(JSON.parse(String(find)));
- });
- };
- async getAll(): Promise<WithId<Document>> {
- return new Promise(async (resolve: (value: WithId<Document> | PromiseLike<WithId<Document>>) => void, reject: (reason?: any) => void): Promise<void> => {
- const cursor: FindCursor<WithId<Document>> = await this.collection.find();
- const find: WithId<Document>[] = await cursor.toArray();
- resolve(JSON.parse(String(find)));
- });
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement