Elfocrash / Cosmonaut

🌐 A supercharged Azure CosmosDB .NET SDK with ORM support
https://cosmonaut.readthedocs.io
MIT License
342 stars 44 forks source link

Inserting data using AddAsync()? #14

Closed kklan closed 6 years ago

kklan commented 6 years ago

Hi,

Sorry to bother you again. I'm very new to this.

I'm trying to inset data into my cosmosdb collection called AppUsers. Basically for my social net app, a person is able to add friend using userId and friendsUserId. So I want to insert into collection (friendUserId in Friends) where userId="AXYZ963". I'm not able to figure out how to do that using cosmonaut and Linq.

Here is my collection:

screen shot 2018-05-20 at 11 29 24 am
  1. Also is there anyway to return an Id of a newly inserted data? I did await _cosmosStore.AddAsync(newUser); can it return id as well?

Thanks KL

Elfocrash commented 6 years ago

1) What I would do is that instead of having a friends list for the AppUser itself, I would have a Relationships collection or shared collection and I would have firstUserId, secondUserId and an Enum RelationshipType which would represent the relationship between to users. Then you can just query the friends when needed based on the userIds.

2) There are two ways you can do that.

kklan commented 6 years ago

Thanks, can you provide a sample Model.cs example?.

  1. Would it look something like this?
     [CosmosCollection("Relationships")]
     public class Relation
     {
        [JsonProperty(PropertyName = "firstId")]
        public string firstUserId { get; set; }

        [JsonProperty(PropertyName = "secondId")]
        public string secondUserId { get; set; }

        public enum RelationShipStatus { Friends, NotFriends };
     }
  1. I was trying to run SQL query directly instead using var query = await _cosmosStore.QueryMultipleAsync() but it is not found. There is only Query() available. Is there anyway I can use direct SQL syntax to insert data?
Elfocrash commented 6 years ago
  1. Yes
  2. No, CosmosDB does not support any other operation other than select via SQL
kklan commented 6 years ago

Got it.

Just for my knowledge, If I were to follow my previous idea of inserting data into a specific field, for example: insert data into C where userId = "ABC". How would I do that?

Also I really appreciate all the the help.

Elfocrash commented 6 years ago

Assuming you mean adding to the Friends list object that you had. CosmosDB does not support partial document updates yet. That means that you would have query for the user where userId = "ABC" with LINQ, update the object and then use the UpdateAsync method to update the whole document.

You're welcome. A start at the repo is highly appreciated 😄

kklan commented 6 years ago

Oh I see. Thank you.