Skip to content

Commit 2675146

Browse files
committed
RDBC-509 SaveChanges after query modifies the change vector
- We need to avoid changing the metadataInstance if the changes we want to make are already there. This will cause us to save the document even though it wasnt changed.
1 parent ca1af9d commit 2675146

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/Documents/Session/EntityToJson.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,29 @@ export class EntityToJson {
112112
documentInfo.metadata[CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] || typeInfo.typeName;
113113
}
114114

115+
function differentNestedTypes() {
116+
const existing =
117+
documentInfo.metadataInstance[CONSTANTS.Documents.Metadata.NESTED_OBJECT_TYPES];
118+
if(existing == null)
119+
return true;
120+
if(Object.keys(existing).length != Object.keys(typeInfo.nestedTypes).length)
121+
return true;
122+
for (const key in typeInfo.nestedTypes){
123+
if(typeInfo.nestedTypes[key] != existing[key]) {
124+
return true;
125+
}
126+
}
127+
return false;
128+
}
129+
115130
if (documentInfo.metadataInstance) {
116-
documentInfo.metadataInstance[CONSTANTS.Documents.Metadata.NESTED_OBJECT_TYPES] = typeInfo.nestedTypes;
117-
documentInfo.metadataInstance[CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] =
118-
documentInfo.metadataInstance["Raven-Node-Type"] || typeInfo.typeName;
131+
if(differentNestedTypes()){
132+
documentInfo.metadataInstance[CONSTANTS.Documents.Metadata.NESTED_OBJECT_TYPES] = typeInfo.nestedTypes;
133+
}
134+
var nodeType = documentInfo.metadataInstance[CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] || typeInfo.typeName;
135+
if(documentInfo.metadataInstance[CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] !== nodeType){
136+
documentInfo.metadataInstance[CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] = nodeType;
137+
}
119138
}
120139

121140
let setMetadata: boolean = false;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as assert from "assert";
2+
import { testContext, disposeTestDocumentStore } from "../../Utils/TestUtil";
3+
4+
import {
5+
IDocumentStore,
6+
} from "../../../src";
7+
8+
class item{
9+
10+
constructor(v : string){
11+
this.v = v;
12+
}
13+
14+
v : string;
15+
}
16+
describe("SpuriousSaveChanges", function () {
17+
18+
let store: IDocumentStore;
19+
20+
beforeEach(async function () {
21+
store = await testContext.getDocumentStore();
22+
});
23+
24+
afterEach(async () =>
25+
await disposeTestDocumentStore(store)
26+
);
27+
28+
it("willNotSaveUnmodifiedDocuments", async () => {
29+
30+
await createData(store);
31+
{
32+
const session = store.openSession();
33+
const result = await session.query<item>({ collection: "items" })
34+
.all();
35+
assert.ok(!session.advanced.hasChanged(result[0]));
36+
var old = session.advanced.numberOfRequests;
37+
await session.saveChanges();
38+
assert.strictEqual(old, session.advanced.numberOfRequests);
39+
}
40+
});
41+
42+
async function createData(store: IDocumentStore): Promise<void> {
43+
const session = store.openSession();
44+
await session.store(new item("f"), "items/1");
45+
await session.saveChanges();
46+
}
47+
48+
});

0 commit comments

Comments
 (0)