I would like some kind of Model for my Meteor collections. The two options I could find for this would be to use collection helpers:
Users = new Mongo.Collection('users')
Users.helpers({
fullName() {
return `${this.firstName} ${this.lastName}`
}
})
or to map the returned objects to class instances:
class UserCollection extends Mongo.Collection {
fetch() : User[] {
return super.fetch().map(user => new User(user))
}
}
let users = new UserCollection('users')
class User {
collection: Mongo.Collection
_id: string
firstName: string
lastName: string
constructor({firstName: string, lastName: string) {
this.firstName = firstName
this.lastName = lastName
this.collection = users
}
get fullname () {
return `${this.firstName} ${this.lastName}`
}
save () {
if(this._id) {
this.collection.update({ $set: { ...this }})
}
else {
this.collection.insert({...this})
}
}
}
Which variant is preferable? I like B more, but rarely found any examples using it.