question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using factory attrs in route handlers

See original GitHub issue

When I am creating a new record, from a POST request, the record created does not contain all properties of the factory that is defined. For example.

I have a page for new user, the post request sends only the user password and username. However, the factory has a few additional properties:

  • username
  • password
  • createdAt
  • updatedAt

As I said, the request.requestBody only contains “username” and “password” and when I create a new record, the createdAt and updatedAt properties are not created

var body = { username: 'sam', password: '1234' }
var newUser = db.users.insert(body)
console.log(newUser) // { id: 123, username: 'sam', password: '1234' }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
john-griffincommented, Feb 13, 2017

A constructor like function on the model would be super useful for setting default values such as created at. The model feels like a better place for this rather than injecting default values in at the route level, especially for apps using mirage shorthands and plain JSON API.

1reaction
pzuraqcommented, Aug 20, 2015

@samselikoff We would actually like to be able to have this type of functionality for POST-ing. Some our systems only take a single field and then populate the rest of the data from other sources on the server side. It would be convenient to use Factories and faker.js to be able to achieve a similar effect in our mocks/tests.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using factory attrs in route handlers #231 - GitHub
When I am creating a new record, from a POST request, the record created does not contain all properties of the factory that...
Read more >
Routing to controller actions in ASP.NET Core - Microsoft Learn
ASP.NET Core controllers use the Routing middleware to match the URLs of incoming requests and map them to actions. Route templates:.
Read more >
attrs by Example - attrs 22.2.0 documentation
Attributes can have a converter function specified, which will be called with the attribute's passed-in value to get a new value to use....
Read more >
How can I get ServerRequestInterface with attributes in Factory?
When initializing some services in the factory, I need to get data from the request attributes. How can i do this?
Read more >
attrs - Release 22.2.0.dev0 Hynek Schlawack - Read the Docs
Since they take an attrs instance as their first argument, you can attach them to your classes with one line of code. Performance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found