Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The New RavenDB 4.0 Attachments Feature

DZone's Guide to

The New RavenDB 4.0 Attachments Feature

RavenDB now allows you to store binary data closely with documents in the database! Read on for more information on this new feature.

· Database Zone ·
Free Resource

RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.  

image

I previously wrote about the new attachments feature in RavenDB 4.0. Now, it is ready to be seen by outside eyes.

As you can see in the image on the right, documents can now have attached attachments (I’m sorry, couldn’t think of a better way to phrase this). This gives you the ability to store binary data inside RavenDB, but not as some free-floating value that has only very loose connection to the rest of the system. Those attachments are strongly tied to their parent document and allow you to store related information easily and right next to the document.

That also means that you can take advantage of RavenDB’s ACID nature and actually make modifications to both attachments and documents at the same time. For example, consider the following code:

public void AddProfilePicture(string userId, Stream profilePictureStream)
{
  var thumbnail = GenerateThumbnail(profilePictureStream);
  profilePictureStream.Position = 0;

  using(var session = store.OpenSession())
  {
        var user = session.Load<User>("users/123");
        session.Advanced.StoreAttachment(user, "profile-pic.jpg", profilePictureStream);
        session.Advanced.StoreAttachment(user, "profile-thumb.jpg", thumbnail);
        user.RegistrationStatus |= UserStatus.HasProfilePicture;
        session.SaveChanges();
  }

Here, we get the user’s profile picture, generate a thumbnail from it, and then associate both picture and thumbnail with the document. We are also updating the status of the user to indicate that they have a profile picture and then submit it all as one single transaction. That means that you don’t have to sync between different sources.

Attachments in RavenDB are also kept consistent through replication, so you won’t see partial results between nodes, and the attachments themselves are using de-duplication techniques to reduce the amount of storage that we take.

I’m really happy with this feature.

Do you pay to use your database? What if your database paid you? Learn more with RavenDB.

Topics:
attachments ,ravendb ,database ,binary data

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}