Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite createdAt value on save() #4

Open
molinto opened this issue Nov 13, 2015 · 3 comments
Open

Overwrite createdAt value on save() #4

molinto opened this issue Nov 13, 2015 · 3 comments

Comments

@molinto
Copy link

molinto commented Nov 13, 2015

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
Copy link
Member

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
Copy link
Author

molinto commented Nov 13, 2015

Thanks, I've done just that for the time being 👍

@lukejagodzinski
Copy link
Member

Ok good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants