Question

The scenario is as follows.

The model has a company. A company can have many users. Users login with their specific email and pwd.

Should i just have a company document with the users inside. If so how do i query the users? Or, should i make a separate document for each user. They have very few properties other than login credentials, as the main data hangs off company. When would it make sense to split this into a separate document?

In general there seems to be very little information in the web about real data model modeling considerations. Would be glad of any links to articles or videos on this.

Was it helpful?

Solution 2

The short answer is - it depends. Depends on use cases, types of queries you are going to execute, document sizes etc.

I'd most probably have a document for each user, a document for each company, and then link between them using document references in one of them - with RavenDB its a simple string property that will hold the ID of the other document you are referencing.

The question is then - which document should reference which? User document to reference Company or the other way around?

This is where the answer is - it depends.

I have a post in my blog that discusses this to some length using a partial use case that you may find helpful: http://code972.com/blog/2013/12/610-many-to-many-relationships-and-ravendb-models

OTHER TIPS

I usually create 3 collections:

  • UserInfo (Id, CompanyId, FullName, Avatar...). Id is created by UserInfos-{UserName}

  • LoginIndentity(Id, UserId, ProviderName, Key). Where Id= {Prefix}-{ProviderName}-{Key}

(i.e: "LoginIndentities-Email-abc@xyz.com" or "LoginIndentities-Facebook-56789..."). You can remove ProviderName and Key properties if you use this pattern.

  • Company (Id, Name,...)

Usage:

//Load user by provider name (email, Google, Facebook...) and key
var loginId = GenerateLoginId(providerName,key);
var userLogin = session.Include<LoginIndentity>(x=>x.UserId).Load<LoginIndentity>(loginId);
//RavenDB will create only one query. Cool! 
//http://ravendb.net/docs/2.5/client-api/querying/handling-document-relationships
var user = userLogin==null?null:session.Load<UserInfo>(userLogin.UserId);

//Load User and Company when you know the user name
var userId= GenerateUserId(userName);
var user = session.Include<UserInfo>(x=>x.CompanyId).Load<UserInfo>(userId);
var company = user==null?null: session.Load<Company>(user.CompanyId);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top