scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

Timestamps fields #40

Open vfreitas- opened 8 years ago

vfreitas- commented 8 years ago

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

michaeljota commented 8 years ago

:+1: +1

scottwrobinson commented 8 years ago

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" notifications@github.com wrote:

[image: :+1:] +1

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

vfreitas- commented 8 years ago

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

        //...
    }
}
michaeljota commented 8 years ago

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

scottwrobinson commented 8 years ago

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?

samirbr commented 8 years ago

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 {
}
michaeljota commented 8 years ago

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 notifications@github.com:

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 .

scottwrobinson commented 8 years ago

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" notifications@github.com 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 notifications@github.com:

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 .

devdebonair commented 8 years ago

Was a pull request submitted for this yet?