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

Is it possible to set default values on insert? #339

Closed
aegyed91 opened this issue Aug 4, 2016 · 4 comments
Closed

Is it possible to set default values on insert? #339

aegyed91 opened this issue Aug 4, 2016 · 4 comments

Comments

@aegyed91
Copy link
Contributor

aegyed91 commented Aug 4, 2016

Hello @typicode,

lets says i have a Todo entity, which looks like this:

interface Todo = {
  id: number;
  title: string;
  completed: boolean = false;
}

By default all newly inserted Todo would be un-completed. This is a typical atomic operation that needs to be done on the server. Which means i won't send this completed value over the wire with a POST request.

I found the create method which is responsible for pushing the contents of req.body to the end of the given collection.

Somehow you need to specify a default value, but for that you need a schema.

I wonder how would you solve something like this?

Right now i solve this with a middleware, but it would be awesome to specify default values by design.

export function todosMiddleware(db) {
  return (req, res, next) => {
    const isTodosRoute = req.path.indexOf('todos');

    if (req.method === 'POST' && isTodosRoute) {
      // add default values
      Object.assign(req.body, {
        completed: false,
        userId: _.random(0, 9)
      });
    }

    next();
  };
}

cc @jpbochi

@jpbochi
Copy link

jpbochi commented Aug 4, 2016

since you copied me here, I assume you looked at #322

One way is by adding a middleware that rewrites the request adding completed = false. Something like this.

server.use('/todo', function (req, res, next) {
   if (req.method === 'POST') {
     req.body.completed = false;
   }
   next();
})

Now, to be honest, I'd simply accept whatever comes in the request, and default completed to false when reading it. undefined is a "falsy" value.

@aegyed91
Copy link
Contributor Author

aegyed91 commented Aug 4, 2016

@jpbochi

Now, to be honest, I'd simply accept whatever comes in the request, and default completed to false when reading it. undefined is a "falsy" value.

That would be the easiest way to go. In case the backend team want to do it 'atomic' i can still remove completed from the http body.

I think its impossible to create schemas because this is a JSON database so i will close this. Thanks for the ansver.

@aegyed91 aegyed91 closed this as completed Aug 4, 2016
@jpbochi
Copy link

jpbochi commented Aug 4, 2016

it's actually possible to add middlewares all sorts of crazy validation, but I don't think that's the purpose of this library. It's a fake REST API server.

I used it for some integration tests I'm writing. The code being tested sends requests to the fake API, and the test code will read what was created.

@aegyed91
Copy link
Contributor Author

aegyed91 commented Aug 4, 2016

@jpbochi My usecase is for develpoment, so i dont have to wait for the backend guys to finish the api.

You are right, i overthinked the purpose of the library. I will just send whatever information is needed in the http request. If something needed to be modified later on when i migrate to the real backend api, so be it.

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