camo icon indicating copy to clipboard operation
camo copied to clipboard

Timestamps fields

Open vfreitas- opened this issue 9 years ago • 9 comments

Maybe add the timestamps fields by default?(created_at, updated_at).

vfreitas- avatar Jan 27 '16 01:01 vfreitas-

:+1: +1

michaeljota avatar Jan 29 '16 22:01 michaeljota

I'll consider it. I hesitate to add data by default to people's models, but maybe it can be an option.

I'll leave this open for discussion for a bit to gauge interest.

Thanks!

Scott On Jan 29, 2016 4:01 PM, "Michael De Abreu" [email protected] wrote:

[image: :+1:] +1

— Reply to this email directly or view it on GitHub https://github.com/scottwrobinson/camo/issues/40#issuecomment-176988995.

scottwrobinson avatar Jan 30 '16 19:01 scottwrobinson

You can have a boolean variable, that will decide if the timestamp fields will be added.

class User extends Document {
    constructor() {
        //... 

        this.timestamps = false; //default = true

        //...
    }
}

vfreitas- avatar Jan 30 '16 20:01 vfreitas-

Maybe not to be enable by default, but that it's a way to go.

michaeljota avatar Jan 31 '16 05:01 michaeljota

Okay so I'm finally getting back to this. Thinking about it some more, I think it would be a nice inclusion, just not enabled by default. I believe features like this should be opt-in instead of opt-out.

The timestamps property would likely have to be set using a static method. Like this:

class User extends Document {
    static timestamps() {
        return true;
    }
}

You could also return names of the created_at/updated_at fields for more customization:

class User extends Document {
    static timestamps() {
        return {
            created_at: 'createdTime',
            updated_at: 'updatedTime'
        };
    }
}

Doing it in the constructor, as shown by @vfreitas-, wouldn't work because then we couldn't access the timestamps property in any static methods (like User.findOneAndUpdate()).

Thoughts?

scottwrobinson avatar Mar 25 '16 15:03 scottwrobinson

What about use a timestamp decorator?

@timestamp
class User extends Document {
}

You could pass timestamp field names too:

@timestamp({
  created_at: 'createdTime',
  updated_at: 'updatedTime'
})
class Use extends Document {
}

samirbr avatar May 31 '16 23:05 samirbr

I don't think ES6 have decorators. For what I understand after googling, decorators are suppose to come in 2016 with ES2016(ES7).

However, for a long proposal that would be a good idea.

2016-05-31 19:51 GMT-04:00 Samir El Aouar [email protected]:

What about use a timestamp decorator?

@timestamp class User extends Document { }

You could pass timestamp field names too:

@timestamp({ created_at: 'createdTime', updated_at: 'updatedTime' }) class Use extends Document { }

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/scottwrobinson/camo/issues/40#issuecomment-222855050, or mute the thread https://github.com/notifications/unsubscribe/AKBWABMfZIaNHx8kkcIla_N6dQQvoOE6ks5qHMmNgaJpZM4HNCJn .

michaeljota avatar Jun 01 '16 05:06 michaeljota

Yep, ES6 does not have decorators. As nice as this feature would be, we'd then have to require application code to use a transpiler like Babel, which does not work with Camo's goal.

I'd like to implement this feature in ES6 soon, but if anyone would like to submit a PR for it sooner, I'd be happy to accept. On May 31, 2016 7:50 PM, "Michael De Abreu" [email protected] wrote:

I don't think ES6 have decorators. For what I understand after googling, decorators are suppose to come in 2016 with ES2016(ES7).

However, for a long proposal that would be a good idea.

2016-05-31 19:51 GMT-04:00 Samir El Aouar [email protected]:

What about use a timestamp decorator?

@timestamp class User extends Document { }

You could pass timestamp field names too:

@timestamp({ created_at: 'createdTime', updated_at: 'updatedTime' }) class Use extends Document { }

— You are receiving this because you commented. Reply to this email directly, view it on GitHub <https://github.com/scottwrobinson/camo/issues/40#issuecomment-222855050 , or mute the thread < https://github.com/notifications/unsubscribe/AKBWABMfZIaNHx8kkcIla_N6dQQvoOE6ks5qHMmNgaJpZM4HNCJn

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/scottwrobinson/camo/issues/40#issuecomment-222899166, or mute the thread https://github.com/notifications/unsubscribe/ACz-bzEpoiZANjb7GPf5SLMXJkeVOpiQks5qHR2ngaJpZM4HNCJn .

scottwrobinson avatar Jun 01 '16 07:06 scottwrobinson

Was a pull request submitted for this yet?

devdebonair avatar Aug 06 '16 13:08 devdebonair