Example #1
0
        public IEnumerable <DataEntryCreatedReturn> LoadDataEntriesFromJson(string tenant, string entityName,
                                                                            string jsonData, ApplicationUser ownerUser)
        {
            var jsonArray = JArray.Parse(jsonData);
            var maxId     = 0;

            using (var bulkInsert = _documentStore.BulkInsert(tenant))
            {
                foreach (var d in jsonArray.Children <JObject>())
                {
                    UserIdentityHelper.SetUserIdentity(d, ownerUser);
                    var entity = RavenJObject.Parse(d.ToString());
                    var meta   = new RavenJObject {
                        { "Raven-Entity-Name", entityName }
                    };
                    var id       = (string)d["Id"];
                    var identity =
                        int.Parse(
                            id.Substring(
                                id.IndexOf(_documentStore.Conventions.IdentityPartsSeparator, StringComparison.Ordinal) +
                                1));
                    maxId = Math.Max(identity, maxId);
                    bulkInsert.Store(entity, meta, id);
                    yield return(new DataEntryCreatedReturn
                    {
                        Key = id,
                        Entity = d
                    });
                }
            }

            _documentStore.DatabaseCommands.SeedIdentityFor(entityName, maxId);
        }
Example #2
0
        private async Task <PutResult> SaveEntryAsync(string tenant, string entityName, ApplicationUser ownerUser,
                                                      JObject jobject, string id)
        {
            UserIdentityHelper.SetUserIdentity(jobject, ownerUser);

            var dataEntry = JsonConvert.SerializeObject(jobject);
            var commands  = _documentStore.AsyncDatabaseCommands.ForDatabase(tenant);

            // NOTE: The PutAsync database command allows us to set the `Raven-Entity-Name` metadata which tells RavenDB to put this entity in it's own entity collection.
            //  Using the `session.Store` or `asyncSession.StoreAsync` methods will cause RavenDB to reflect on the parameter object causing all entites to be stored in
            //  a single entity collection of `JObject` which is not what we want.
            var result = await commands.PutAsync(
                id,
                null,
                RavenJObject.Parse(dataEntry),
                new RavenJObject
            {
                { "Raven-Entity-Name", entityName },
            });

            return(result);
        }