diff --git a/src/jio.storage/indexstorage.js b/src/jio.storage/indexstorage.js index 9a9b133cbede1a9c18a73b7d6f369a871a8abef5..b476c06b9b14bca769e46f1ece6b8ad7d6ff1c0a 100644 --- a/src/jio.storage/indexstorage.js +++ b/src/jio.storage/indexstorage.js @@ -509,13 +509,18 @@ }); } function createDatabaseAndPutAttachmentIfPossible(err) { + var metadata; if (err.status === 404) { + metadata = {"_id": index.id}; + if (typeof index.metadata === 'object' && + // adding metadata + index.metadata !== null && + !Array.isArray(index.metadata)) { + metadata = jIO.util.dictUpdate(metadata, index.metadata); + } return command.storage( index.sub_storage || that._sub_storage - ).post({ - "_id": index.id - // XXX add metadata to document if necessary - }).then(putAttachment, null, function () { + ).post(metadata).then(putAttachment, null, function () { throw null; // stop post progress propagation }); } diff --git a/test/jio.storage/indexstorage.tests.js b/test/jio.storage/indexstorage.tests.js index 1eefe0ebc87038034e688f5701e7e88787b7d77c..9334bfe9416e6aa4e9b4d147b6bdfb03be7fb6f4 100644 --- a/test/jio.storage/indexstorage.tests.js +++ b/test/jio.storage/indexstorage.tests.js @@ -69,7 +69,7 @@ module("IndexStorage"); - test("Scenario", 38, function () { + test("Scenario", 39, function () { var LOCAL_STORAGE_SPEC = local_storage.createDescription( 'indexstorage tests', @@ -78,7 +78,9 @@ ), INDEX_STORAGE_SPEC = { "type": "indexed", "indices": [ - {"id": "A", "index": ["contributor"]}, + {"id": "A", "index": ["contributor"], "metadata": { + "title": "Database - A" + }}, {"id": "B", "index": ["author"]}, {"id": "C", "index": ["title"]}, {"id": "D", "index": ["title", "year"]} @@ -378,6 +380,32 @@ }, "List 8 documents from local (4 document + 4 databases)"); } + function getDatabaseMetadata() { + return jio_local.get({"_id": "A"}); + } + + function getDatabaseMetadataTest(answer) { + deepEqual(answer, { + "data": { + "_attachments": { + "body": { + "content_type": "application/json", + "digest": "sha256-365910ba219365b68e3431f9762eef21f" + + "77cd390dbcc55d827d42555c66340a6", + "length": 105 + } + }, + "_id": "A", + "title": "Database - A" + }, + "id": "A", + "method": "get", + "result": "success", + "status": 200, + "statusText": "Ok" + }, "Check one index database metadata"); + } + // function removeCreatedDocuments() { // return success(RSVP.all([ // jio_index.remove({"_id": shared.created_document_id}), @@ -914,6 +942,8 @@ then(listDocumentsFromNothingTest). // allD 200 8 documents from local then(listDocumentsFromLocal).then(listDocumentsFromLocalTest). + // get 200 database to check metadatas from local + then(getDatabaseMetadata).then(getDatabaseMetadataTest). // remove a b ce dee 204 then(removeCreatedDocuments).then(removeCreatedDocumentsTest). // allD 200 empty indexes