import {createCollection} from '@orion-js/mongodb'
import {schemaWithName, InferSchemaType} from '@orion-js/schema'
import {Service} from '@orion-js/services'
export const UserSchema = schemaWithName('UserSchema', {
_id: {type: typedId('user')},
name: {type: String},
email: {type: String},
createdAt: {type: Date}
})
export type UserType = InferSchemaType<typeof UserSchema>
@Service()
export class UserRepository {
private users = createCollection({
name: 'users',
schema: UserSchema
})
async findById(id: string) {
return this.users.findOne({_id: id})
}
async findByEmail(email: string) {
return this.users.findOne({email})
}
async create(user: Omit<UserType, '_id'>) {
return this.users.insertOne(user)
}
async updateName(id: string, name: string) {
await this.users.updateOne({_id: id}, {$set: {name}})
}
}