jagi / meteor-astronomy-timestamp-behavior

https://atmospherejs.com/jagi/astronomy-timestamp-behavior
MIT License
5 stars 6 forks source link

Overwrite createdAt value on save() #4

Open molinto opened 8 years ago

molinto commented 8 years ago

Hi, thanks for all your hard work with the great astronomy packages!

I'm trying to seed the database with content. Is there a way to overwrite the created value on save()? Eg, populate the value of the created rather than using the behaviour timestamp when its not defined in the controller. Please see below

Model

behaviors: {
        timestamp: {
            hasCreatedField: true,
            createdFieldName: 'created',
            hasUpdatedField: true,
            updatedFieldName: 'last_modified'
        }
    },

Controller

var now = new Date();
var lastMonth = new Date();
lastMonth.setMonth(lastMonth.getMonth()-1);

var response = new Response({
     score: faker.random.number({ 'min': 0, 'max': 10 }),
     created: faker.date.between(lastMonth, now)
});

if (response.validate()) {
      response.save();
}

Thanks Sharry

PS - Works OK with Node & Mongoose with this package: https://github.com/drudge/mongoose-timestamp

lukejagodzinski commented 8 years ago

Hey, thanks :-)

Hmm it may a good improvement. It wouldn't be hard. It would just not override the created field if it's already filled with a value. However it's very important to make it secure. Let's imagine a situation when you're sending a document from the client to the server with the created field already filled by the user on the client. So it should be optional feature.

For now you can just create this field by your self and fill out with a value. You can turn off the createdAt field in behaviors option by setting the hasCreatedField option to false.

molinto commented 8 years ago

Thanks, I've done just that for the time being :+1:

lukejagodzinski commented 8 years ago

Ok good