Skip to content

Commit 804b733

Browse files
committed
Ensure document attribute index is cleared when not present.
1 parent abd779c commit 804b733

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# bedrock-edv-storage ChangeLog
22

3+
## 21.0.1 - 2025-mm-dd
4+
5+
### Fixed
6+
- Ensure document attribute index is cleared when unique attribute list is
7+
also cleared.
8+
39
## 21.0.0 - 2025-04-23
410

511
### Removed

lib/storage/docs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export async function update({edvId, doc, explain = false} = {}) {
359359
if(uniqueAttributes.length > 0) {
360360
update.$set.uniqueAttributes = uniqueAttributes;
361361
} else {
362-
update.$unset = {uniqueAttributes: true};
362+
update.$unset = {...update.$unset, uniqueAttributes: true};
363363
}
364364

365365
// add insert-only fields

test/mocha/16-update.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright (c) 2018-2022 Digital Bazaar, Inc. All rights reserved.
2+
* Copyright (c) 2018-2025 Digital Bazaar, Inc. All rights reserved.
33
*/
44
import * as brEdvStorage from '@bedrock/edv-storage';
55
import * as database from '@bedrock/mongodb';
@@ -39,6 +39,40 @@ describe('docs.update API', () => {
3939
});
4040
record.doc.should.eql(doc);
4141
});
42+
it('should update a document and remove its attributes', async () => {
43+
// add doc
44+
await brEdvStorage.docs.update({
45+
edvId: mockEdvId, doc: mockData.docWithUniqueAttributes
46+
});
47+
48+
// ensure attributes exist
49+
{
50+
const record = await collection.findOne({
51+
localEdvId: localMockEdvId,
52+
'doc.id': mockData.docWithUniqueAttributes.id
53+
});
54+
record.doc.sequence.should.equal(0);
55+
should.exist(record.attributes);
56+
should.exist(record.uniqueAttributes);
57+
}
58+
59+
// update doc to remove attributes
60+
const doc = {...mockData.docWithUniqueAttributes, sequence: 1};
61+
doc.indexed = structuredClone(doc.indexed);
62+
doc.indexed[0].attributes = [];
63+
await brEdvStorage.docs.update({edvId: mockEdvId, doc});
64+
65+
// ensure attributes have been cleared
66+
{
67+
const record = await collection.findOne({
68+
localEdvId: localMockEdvId,
69+
'doc.id': mockData.docWithUniqueAttributes.id
70+
});
71+
record.doc.sequence.should.equal(1);
72+
should.not.exist(record.attributes);
73+
should.not.exist(record.uniqueAttributes);
74+
}
75+
});
4276
it('should fail to update a document with max safe sequence', async () => {
4377
let error;
4478
const doc = {...mockData.doc1, sequence: Number.MAX_SAFE_INTEGER};

0 commit comments

Comments
 (0)