Skip to content

Commit e6d9d84

Browse files
authored
Merge pull request #168 from gregolsky/v4.0
fix passing includes via session.load()
2 parents 9cfa425 + fcfa28f commit e6d9d84

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/Documents/Session/DocumentSession.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
139139
options = options || {};
140140

141141
this.conventions.tryRegisterEntityType(options.documentType);
142-
const objType = this.conventions.findEntityType(options.documentType);
143-
144-
const loadOperation = new LoadOperation(this);
145-
const loadInternalPromise = this._loadInternal<TEntity>(ids, loadOperation, null)
146-
.then(() => loadOperation.getDocuments<TEntity>(objType))
142+
const loadInternalPromise = this.loadInternal(ids, options.includes, options.documentType)
147143
.then((docs: EntitiesCollectionObject<TEntity> | TEntity) => {
148144
if (isLoadingSingle) {
149145
return docs[Object.keys(docs)[0]];
@@ -157,13 +153,11 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
157153
return loadInternalPromise;
158154
}
159155

160-
private async _loadInternal<T>( //TODO: unused!
161-
ids: string[],
162-
operation: LoadOperation): Promise<void>;
163-
private async _loadInternal<T>(
156+
private async _loadInternal(
164157
ids: string[],
165-
operation: LoadOperation, writable: stream.Writable): Promise<void>;
166-
private async _loadInternal<T>(
158+
operation: LoadOperation,
159+
writable: stream.Writable): Promise<void>;
160+
private async _loadInternal(
167161
ids: string[],
168162
operation: LoadOperation, writable?: stream.Writable)
169163
: Promise<void> {
@@ -361,7 +355,9 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
361355
}
362356

363357
public async loadInternal<TResult extends object>(
364-
ids: string[], includes: string[], documentType: DocumentType<TResult>):
358+
ids: string[],
359+
includes: string[],
360+
documentType: DocumentType<TResult>):
365361
Promise<EntitiesCollectionObject<TResult>> {
366362
if (!ids) {
367363
throwError("InvalidArgumentException", "Ids cannot be null");

test/Documents/ReadmeSamples.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
QueryStatistics,
1313
StreamQueryStatistics,
1414
AbstractIndexCreationTask,
15+
LoadOptions,
1516
} from "../../src";
1617
import { TypeUtil } from "../../src/Utility/TypeUtil";
1718

@@ -98,6 +99,27 @@ describe("Readme query samples", function () {
9899
assert.strictEqual(session.advanced.numberOfRequests, 1);
99100
});
100101

102+
it("loading data with passing includes", async () => {
103+
// tslint:disable-next-line:no-shadowed-variable
104+
const session = store.openSession();
105+
// users/1
106+
// {
107+
// "name": "John",
108+
// "kids": ["users/2", "users/3"]
109+
// }
110+
111+
const user1 = await session
112+
.load("users/1", { includes: [ "kids" ] } as LoadOptions<any>);
113+
114+
const user2 = await session.load("users/2"); // this won't call server again
115+
// Document users/1 is going to be pulled along
116+
// with docs referenced in "kids" field within a single request
117+
118+
assert.ok(user1);
119+
assert.ok(user2);
120+
assert.strictEqual(session.advanced.numberOfRequests, 1);
121+
});
122+
101123
});
102124

103125
describe("attachments", () => {

0 commit comments

Comments
 (0)