neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

Virtual field with current rows id #577

Closed nodesocket closed 7 years ago

nodesocket commented 7 years ago

I'm trying to add a virtual field to all models that returns the url to the row itself.

const Account = module.exports = orm.createModel('accounts', {
    id: type.string().uuid(4).default(() => {
        return uuid.v4();
    }),
    created: type.date().default(orm.r.now()),
    url: type.virtual().default(() => {
        return `/accounts/${this.id}`;
    })
}, {
    enforce_extra: 'strict' // jshint ignore:line
});

The field url should be /accounts/79c852a1-37cf-4193-a5af-4ac0145569ae but getting /accounts/undefined.

nodesocket commented 7 years ago

Argg figured it out. Caused by using ES6 () => {} syntax.

It would be awesome if instead of magically using this, if instead the document was passing into .default((doc) => { console.log(doc); }.

neumino commented 7 years ago

Hum, this is a pretty standard use of this. There's another bug to track document for that.