Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mongoose from "mongoose";
- import './models/user';
- import './models/follow';
- function DB() {
- const User = mongoose.model('User');
- // const Follower = mongoose.model('Follower');
- // const Follow = mongoose.model('Follow');
- this.setUpConnection = () => {
- return mongoose.connect(`mongodb://localhost/instabot`, {
- useCreateIndex: true,
- useNewUrlParser: true,
- useFindAndModify: false
- });
- };
- this.listUsers = (id) => {
- return User.find();
- };
- this.getFollow = (id, followId) => {
- // return Follow.find({id, followId});
- };
- this.createFollow = async (user, id, followId) => {
- console.log('user in:', user);
- await user.follows.push({
- id: followId,
- createdAt: new Date(),
- updatedAt: new Date(),
- });
- return user;
- };
- this.updateOrCreateFollow = async (id, followId) => {
- console.log('updateOrCreateFollow start');
- let user = await User.findOneAndUpdate({id},
- async (user) => {
- return await new Promise(async (resolve, reject) => {
- console.log('user in:', user);
- await user.follows.findOneAndUpdate({id: followId}, {
- updatedAt: new Date(),
- deletedAt: undefined
- },
- async (err, doc) => {
- if (!(doc == null && err == null)) {
- console.log('doc:', doc);
- console.log('err:', err);
- return doc;
- } else {
- const upuser = await this.createFollow(user, id, followId);
- console.log('updated user:', upuser);
- resolve(upuser);
- }
- });
- resolve(user);
- return user;
- });
- }, {
- returnOriginal: true,
- });
- console.log('user', user);
- console.log('updateOrCreateFollow end');
- return await user.save();
- };
- this.createUser = (data) => {
- const user = new User({
- id: +data.id,
- username: data.username,
- fullName: data.fullName,
- isPrivate: data.isPrivate,
- profilePicUrl: data.profilePicUrl,
- profilePicId: data.profilePicId,
- isVerified: data.isVerified,
- hasAnonymousProfilePicture: data.hasAnonymousProfilePicture,
- mediaCount: data.mediaCount || -1,
- followerCount: data.followerCount || -1,
- followingCount: data.followingCount || -1,
- biography: data.biography || '',
- // hdProfilePicVersions: data.hdProfilePicVersions || '',
- createdAt: new Date(),
- updatedAt: new Date(),
- });
- return user.save();
- };
- this.updateOrCreate = async (data) => {
- return await User.findOneAndUpdate({id: +data.id},
- {
- id: +data.id,
- username: data.username,
- fullName: data.fullName,
- isPrivate: data.isPrivate,
- profilePicUrl: data.profilePicUrl,
- profilePicId: data.profilePicId,
- isVerified: data.isVerified,
- hasAnonymousProfilePicture: data.hasAnonymousProfilePicture,
- mediaCount: data.mediaCount || -1,
- followerCount: data.followerCount || -1,
- followingCount: data.followingCount || -1,
- follows: [],
- biography: data.biography || '',
- // hdProfilePicVersions: data.hdProfilePicVersions || '',
- updatedAt: new Date(),
- },
- async (err, doc) => {
- if (doc == null && err == null) {
- return await this.createUser(data);
- }
- return doc;
- })
- };
- this.deleteUser = (id) => {
- return User.findById(id).remove();
- };
- return this;
- }
- module.exports = DB;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement