流星收集助手vs orm

时间:2017-11-26 19:40:07

标签: javascript typescript meteor

我想要一些我的Meteor系列模型。我能找到的两个选项是使用集合助手:

Users = new Mongo.Collection('users')
Users.helpers({
  fullName() {
    return `${this.firstName} ${this.lastName}`
  }
})

或将返回的对象映射到类实例:

 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})
        }
    }
}

哪种变体更可取?我更喜欢B,但很少发现任何使用它的例子。

0 个答案:

没有答案