How do I work with binary data, save it, read it from database, etc?

I can’t find anything in the docs, or searching google with: [site:couchbase.com binary] or searching google generally, on how to work with binary data in Couchbase, preferably using the Couchbase Node.js SDK.

Right now I have a buffer filled with a .JPG image that I send into Couchbase as part of a JSON object with the buffer in it. When I retrieve the item from the Couchbase bucket using N1QL and access where the data is stored like arrMessages[0].parsed_message.attachments[0].content and save this to a file or print it out using util.inspect, I get 44,175,223,215,29,170,120,76,23,86,147,105,109,109,50,89,90,128,62 and so on…

Here’s my code to write it to a file:

var query = N1qlQuery.fromString(
			"SELECT META().id As id, * FROM messages AS message"
			+ " WHERE message.type = \"message\""
			+ " AND META().id = \"test_msg_2\""
			+ " LIMIT 1");

bucketMessages.query(
	query,
	function(err, arrMessages) {

	console.log("checksum: <"
		+ arrMessages[0].parsed_message.attachments[0].checksum+">");
	//console.log("Result: --<"+util.inspect(arrMessages, {depth:null})+">--");
	
	var fs = require('fs');
	
	fs.writeFile("c:/attachments/something.jpg",
		arrMessages[0].parsed_message.attachments[0].content,
		function(err) {
		
		if(err) {
			console.log("Error occurred in callback"
				+ "for fs.writeFile(\"c:/attachments/\", ...)");
			console.log(err);
		} else {
			console.log("The file was saved!");
		}
	}); 

	bucketMessages.disconnect();

});

How do I write the jpg back to its original state on disk?

On the writing side, I do the following:


var myJPGBuffer = new Buffer(...jpg buffer...)
     //psuedo code

var myJSONobject = {};
myJSONobject.title = "Some title";
myJSONobject.content = myJPGBuffer;


bucketMessages.counter('test_msg::next_id', 1, {initial: 1}, function(counterErr, counterResult) {
	if (counterErr) throw counterErr;

	bucketMessages.insert( "test_msg_" + counterResult.value,
		myJSONobject, function(insertErr, insertResult) {

			if (insertErr) throw insertErr;

			console.log("JSONobject added to database "
				+ "with jpeg file in .content")

	});
});

Hey neatcode,

Unfortunately Node.js Buffer objects are not strictly JSON serializable. If you serialize a Buffer object to JSON, you will end up with a big array of bytes (taking up significantly more space than the Buffer would have itself). If you must do this, you can perform a new Buffer(jsonObject.content) to return it to a buffer object from the array object. However, I would honestly suggest you create 2 documents instead, one which stores the contents of the document and another which stores any metadata you wish to include. The Node.js SDK handles Buffer objects directly and can store them as raw bytes instead of JSON encoding them, this will significantly improve your performance and storage overhead.

Cheers, Brett