Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import { QueryResolvers } from "@models";
import { UsersProvider } from "../providers/users.provider";
export const Query: QueryResolvers = {
allUsers: (root, args, { injector }) => injector.get(UsersProvider).getAllUsers(),
userById: (_, { id }, { injector }) => injector.get(UsersProvider).getUserById(id),
userByUsername: (_, { username }, { injector }) => injector.get(UsersProvider).getUserByUsername(username),
getUserByToken: (_, { id }, { injector }) => injector.get(UsersProvider).getUserByToken(id)
}
import { Injectable } from "@graphql-modules/di";
import { Db, Collection, ObjectID } from "mongodb";
import { UserDbObject, CreateUserInput } from '@models';
import { AccountsServer, ServerHooks } from '@accounts/server';
@Injectable()
export class UsersProvider {
collection: Collection<UserDbObject>;
constructor(db: Db) {
this.collection = db.collection('users');
}
getAllUsers() {
return this.collection.find().toArray();
}
getUserById(id: string) {
return this.collection.findOne({ _id: new ObjectID(id) });
}
getUserByToken(id: string) {
return this.collection.findOne({ _id: new ObjectID(id) });
}
getUserByUsername(username: string) {
return this.collection.findOne({ username: username });
}
getUsersCount() {
return this.collection.countDocuments();
}
async updateUser(id: string, user: CreateUserInput): Promise<UserDbObject> {
const _id = new ObjectID(id);
await this.collection.updateOne({ _id }, { $set: user });
return {
_id,
...user
} as UserDbObject;
}
async deleteUser(id: string, user: CreateUserInput): Promise<UserDbObject> {
const _id = new ObjectID(id);
await this.collection.deleteOne({ _id });
return {
_id,
...user
} as UserDbObject;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment