0

Im experimenting with a few different technologies such as graphql, mysql. fastify and react.

I came up with a fun app where I would have multiple organizations using the same app and the same db. The difference would be the url. Organisation1 would have www.myApp.com/organisation1

Each app would hold a list of employees and each employee would have a reference to one organization at a given time but can also belong to many organizations at separate time periods(many to many relationships).

db model:

enter image description here

Part 1:

Let's say I want to query my db but from app1 then it would look something like this:

SELECT Name from Employee where organisationId=1

In another scenario, I would have application2 and then the query would look like:

SELECT Name from Employee where organisationId=2

How would I maintain which organization I am coming from? At the moment I am reading the url which is www.myApp.com/organisation1

And depending on which organization it is I add it to the header in every request. Then my backend takes the parameter and queries the db. Is this optimal? is there a better way?

Part2:

Is there some way I could cache/set what organization I'm querying for in the mysql db so that I don't have to include the parameter in the request? again is this optimal?

I'm trying to learn here and asking for some guidance so that I'm doing it right.

keikai
  • 14,085
  • 9
  • 49
  • 68
CodingLittle
  • 1,761
  • 2
  • 17
  • 44
  • If the goal is to simply remove the info from the URL, could you put the org id in a cookie? If the user has to authenticate, could the org be inferred from user record (current org value)? – Quint Mar 30 '20 at 21:37
  • Hmm so having one ulr but when the user logs in i would get the orgId from the db and put it in a cookie? I never thought of that – CodingLittle Mar 30 '20 at 21:38
  • 2
    leave alone url - login is the place to enter/choose org, enter login and pass ... you can encode org in JWT token ... but passing as header (like auth token, stored in cookie/localStorage) is simpler (by link, without need to include in query) and faster then decoding ... you can cache token-user-org in redis – xadm Mar 30 '20 at 23:27
  • FYI, what you described is not a many-to-many relationship. A many-to-many would be if a given employee could be a member of multiple organizations _at the same time._ – Bill Karwin Mar 31 '20 at 02:05
  • @BillKarwin "How would I maintain which organization I am coming from?" – xadm Mar 31 '20 at 04:51
  • 1
    you should look for "multi-tenant" software architecture – Manuel Spigolon Mar 31 '20 at 06:36
  • @xadm Thank you! Been googling and ive fount alot of articles but I find it is often hard to know which one is the "good one". If you dont mind me asking you for a link to an article you find is of sufficent quality. Also reading up on the multi-tenant architecture, thanks for this guys! – CodingLittle Mar 31 '20 at 09:16

0 Answers0