0
votes

I'm designing a restFUL api with :

  • couchDB as database
  • nodeJS + expressJS for the server
  • BackboneJS : rich-client app


I have 3 entities:

  • user : Can have multiple teams
  • team : Can have multiple users and projects
  • project : Can have at most 1 team and multiple users (this users are bonus users that are not in the team but can work on this project)

This is my actual DB pattern, It is close to the SQL joins syntax and I don't know if it's the best way to do this...

User

 {
   "_id": "233e91578810ee8462b33bd1",
   "type": "user",
   "email": "[email protected]"
   "teams": [b6e9157886462b33bd1f2, a6e9157886462b33bd1f6],
}

Team

 {
   "_id": "b6e9157886462b33bd1f2",
   "type": "team",
   "users": ["233e91578810ee8462b33bd1", "b8a6462b33bd1fb0068ab"],
   "projects": ["456abe54a65b6a5aa5ea63cd", "a5eba546a6edc313avsb068"],
}

Project

 {
   "_id": "ba5c60ee8462b33bd1fb00684c",
   "type": "project",
   "team": "b6e9157886462b33bd1fb0068ab",
   "users": ["aae9157886462b33bd1fb0068a", "abac5d6c7d99a6c005c26"]
}

I'm inexperienced with restFUL api, but with this schema I have to update in a bidirectionnal way.

For instance : A user is added in a team, I have to add this userID in the team entity, and I have to add the teamID in the user entity.

I also give you my API routes :

GET api/user/:id -> return user data
PUT api/user/:id -> update user data

GET api/team/:id -> return team data
PUT api/team/:id -> update team data 

(I have issue with the last one, because there is differents update possibilities, addUser, removeUser, and when the GET ROUTE is called, I have to know which kind of update it is in order to update in a bidirectional way)

1

1 Answers

1
votes

You can use DELETE:

DELETE /api/team/:id -> remove team data

It can be RESTful without necessarily having to 'remove' anything, it can set a delete flag for example.

Also, any GET routes should be idempotent, which means it can't manipulate or be harmful to data if it's accessed one or more times.

As for doing an insert, normally you just want to use POST for that:

POST /api/team -> insert team data

You can use PUT or PATCH to do updates, although I've been leaning towards PUT because PATCH is incompatible with some versions of Internet Explorer.