import {Repository} from '@orion-js/mongodb'
import {schemaWithName, InferSchemaType} from '@orion-js/schema'
import {createCollection, typedId} from '@orion-js/mongodb'
// Define your schema with types
const UserSchema = schemaWithName('User', {
_id: {
type: typedId('user')
},
name: {
type: String
},
email: {
type: String
}
})
// Infer the type from the schema
type UserType = InferSchemaType<typeof UserSchema>
@Repository()
export class UserRepository {
// Define your collection
private users = createCollection({
name: 'users',
schema: UserSchema
})
async findById(id: string): Promise<UserType | null> {
return await this.users.findOne({_id: id})
}
async create(userData: Omit<UserType, '_id'>) {
return await this.users.insertAndFind(userData)
}
}