import {Repository} from '@orion-js/services'
import {createCollection, typedId, InferSchemaType} from '@orion-js/mongodb'
import {schemaWithName} from '@orion-js/schema'
const UserSchema = schemaWithName('UserSchema', {
_id: {type: typedId('user')},
name: {type: String},
email: {type: String}
})
type UserType = InferSchemaType<typeof UserSchema>
@Repository()
export class UserRepository {
users = createCollection({
name: 'users',
schema: UserSchema,
indexes: [
{
keys: {
email: 1,
},
},
],
})
async findById(id: string): Promise<UserType> {
return await this.users.findOne({_id: id})
}
async create(userData: Omit<UserType, '_id'>): Promise<string> {
return await this.users.insertOne(userData)
}
}