Skip to content

Instantly share code, notes, and snippets.

@extrimua
Created July 8, 2021 10:01
Show Gist options
  • Save extrimua/a41a452c2c62aca8abb672b1bad991a0 to your computer and use it in GitHub Desktop.
Save extrimua/a41a452c2c62aca8abb672b1bad991a0 to your computer and use it in GitHub Desktop.
// Model
import { User } from '../models/User.js'
// Repository
import { UserRepositoryInterface } from '../../../../domain/contracts/repository/UserRepositoryInterface.js'
export class UserRepository extends UserRepositoryInterface {
async create({ id, auth, name, photo }){
const data = await User.create({ id, auth, name, photo })
return { data }
}
async get(id){
const data = await User.findOne({ id })
.select('-_id -__v -created -modified')
.populate({
path: 'integrations',
populate: {
path: 'provider',
select: '-_id -__v -modified -created'
},
select: '-_id -__v'
})
.populate({
path: 'integrations',
populate: {
path: 'snippets',
select: '-_id -__v -modified -created'
},
select: '-_id -__v'
})
.populate({
path: 'providers',
select: '-_id -__v'
})
.lean()
return { data }
}
async update({ id, auth, name, photo }){
const data = await User.findOneAndUpdate({ id }, { auth, name, photo }, { new: true })
return { data }
}
async addIntegrationField(id, integration){
const data = await User.findOneAndUpdate({ id }, { $push: { "integrations": integration } }, { new: true, upsert: true })
return { data }
}
async removeIntegrationField(id, integration){
const data = await User.findOneAndUpdate({ id }, { $pull: { "integrations": integration } }, { new: true, upsert: true })
return { data }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment