Skip to content

Commit de78e88

Browse files
committed
sync with java: edbc5d799a7a960ce40dabf087ca2e97e22f54e2
1 parent 30c22df commit de78e88

File tree

9 files changed

+86
-22
lines changed

9 files changed

+86
-22
lines changed

src/Documents/DocumentStore.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ export class DocumentStore extends DocumentStoreBase {
189189
public getRequestExecutor(database?: string): RequestExecutor {
190190
this.assertInitialized();
191191

192-
if (!database) {
193-
database = this.database;
194-
}
192+
database = this.getEffectiveDatabase(database);
195193

196194
const databaseLower = database.toLowerCase();
197195

@@ -239,7 +237,7 @@ export class DocumentStore extends DocumentStoreBase {
239237
requestTimeout(timeoutInMs: number, database?: string): IDisposable {
240238
this.assertInitialized();
241239

242-
database = database || this.database;
240+
database = this.getEffectiveDatabase(database);
243241

244242
if (!database) {
245243
throwError("InvalidOperationException", "Cannot use requestTimeout without a default database defined " +
@@ -415,6 +413,6 @@ export class DocumentStore extends DocumentStoreBase {
415413
public bulkInsert(database?: string): BulkInsertOperation {
416414
this.assertInitialized();
417415

418-
return new BulkInsertOperation(database || this.database, this);
416+
return new BulkInsertOperation(this.getEffectiveDatabase(database), this);
419417
}
420418
}

src/Documents/DocumentStoreBase.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export abstract class DocumentStoreBase
9191
const indexesToAdd = IndexCreation.createIndexesToAdd(tasks, this.conventions);
9292

9393
await this.maintenance
94-
.forDatabase(database || this.database)
94+
.forDatabase(this.getEffectiveDatabase(database))
9595
.send(new PutIndexesOperation(...indexesToAdd));
9696
}
9797

@@ -343,4 +343,23 @@ export abstract class DocumentStoreBase
343343
protected _assertValidConfiguration(): void {
344344
this.conventions.validate();
345345
}
346+
347+
public getEffectiveDatabase(database: string): string {
348+
return DocumentStoreBase.getEffectiveDatabase(this, database);
349+
}
350+
351+
public static getEffectiveDatabase(store: IDocumentStore, database: string) {
352+
if (!database) {
353+
database = store.database;
354+
}
355+
356+
if (!StringUtil.isNullOrWhitespace(database)) {
357+
return database;
358+
}
359+
360+
throwError("InvalidArgumentException", "Cannot determine database to operate on. " +
361+
"Please either specify 'database' directly as an action parameter " +
362+
"or set the default database to operate on using 'DocumentStore.database' property. " +
363+
"Did you forget to pass 'database' parameter?");
364+
}
346365
}

src/Documents/Indexes/AbstractIndexCreationTaskBase.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IDocumentStore } from "../IDocumentStore";
55
import { PutIndexesOperation } from "../Operations/Indexes/PutIndexesOperation";
66
import { AbstractCommonApiForIndexes } from "./AbstractCommonApiForIndexes";
77
import { IAbstractIndexCreationTask } from "./IAbstractIndexCreationTask";
8+
import { DocumentStoreBase } from "../DocumentStoreBase";
89

910
export abstract class AbstractIndexCreationTaskBase<TIndexDefinition extends IndexDefinition>
1011
extends AbstractCommonApiForIndexes implements IAbstractIndexCreationTask {
@@ -47,8 +48,8 @@ export abstract class AbstractIndexCreationTaskBase<TIndexDefinition extends Ind
4748
const oldConventions = this.conventions;
4849

4950
try {
50-
const databaseForConventions = database || store.database;
51-
this.conventions = conventions || this.conventions || store.getRequestExecutor(databaseForConventions).conventions;
51+
database = DocumentStoreBase.getEffectiveDatabase(store, database);
52+
this.conventions = conventions || this.conventions || store.getRequestExecutor(database).conventions;
5253

5354
const indexDefinition = this.createIndexDefinition();
5455
indexDefinition.name = this.getIndexName();
@@ -61,7 +62,7 @@ export abstract class AbstractIndexCreationTaskBase<TIndexDefinition extends Ind
6162
indexDefinition.priority = this.priority;
6263
}
6364

64-
await store.maintenance.forDatabase(database || store.database)
65+
await store.maintenance.forDatabase(database)
6566
.send(new PutIndexesOperation(indexDefinition));
6667
} finally {
6768
this.conventions = oldConventions;

src/Documents/Operations/OperationExecutor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { DocumentType } from "../DocumentAbstractions";
1515
import { PatchResult } from "./PatchResult";
1616
import { IDocumentStore } from "../IDocumentStore";
1717
import { StatusCodes } from "../../Http/StatusCode";
18+
import { StringUtil } from "../../Utility/StringUtil";
1819

1920
export class OperationExecutor {
2021

@@ -27,7 +28,7 @@ export class OperationExecutor {
2728
public constructor(store: DocumentStoreBase, databaseName?: string) {
2829
this._store = store;
2930
this._databaseName = databaseName ? databaseName : store.database;
30-
if (this._databaseName) {
31+
if (!StringUtil.isNullOrWhitespace(this._databaseName)) {
3132
this._requestExecutor = store.getRequestExecutor(this._databaseName);
3233
} else {
3334
throwError("InvalidOperationException",

src/Documents/Subscriptions/DocumentSubscriptions.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class DocumentSubscriptions implements IDisposable {
7373
throwError("InvalidArgumentException", "Cannot create a subscription if the script is null");
7474
}
7575

76-
const requestExecutor = this._store.getRequestExecutor(database || this._store.database);
76+
const requestExecutor = this._store.getRequestExecutor(this._store.getEffectiveDatabase(database));
7777

7878
const command = new CreateSubscriptionCommand(options);
7979
await requestExecutor.execute(command);
@@ -322,7 +322,7 @@ export class DocumentSubscriptions implements IDisposable {
322322
* It downloads a list of all existing subscriptions in a database.
323323
*/
324324
public async getSubscriptions(start: number, take: number, database?: string): Promise<SubscriptionState[]> {
325-
const requestExecutor = this._store.getRequestExecutor(database || this._store.database);
325+
const requestExecutor = this._store.getRequestExecutor(this._store.getEffectiveDatabase(database));
326326

327327
const command = new GetSubscriptionsCommand(start, take);
328328
await requestExecutor.execute(command);
@@ -344,7 +344,7 @@ export class DocumentSubscriptions implements IDisposable {
344344
* Delete a subscription.
345345
*/
346346
public async delete(name: string, database?: string): Promise<void> {
347-
const requestExecutor = this._store.getRequestExecutor(database || this._store.database);
347+
const requestExecutor = this._store.getRequestExecutor(this._store.getEffectiveDatabase(database));
348348

349349
const command = new DeleteSubscriptionCommand(name);
350350
return requestExecutor.execute(command);
@@ -368,7 +368,7 @@ export class DocumentSubscriptions implements IDisposable {
368368
throwError("InvalidArgumentException", "SubscriptionName cannot be null");
369369
}
370370

371-
const requestExecutor = this._store.getRequestExecutor(database || this._store.database);
371+
const requestExecutor = this._store.getRequestExecutor(this._store.getEffectiveDatabase(database));
372372

373373
const command = new GetSubscriptionStateCommand(subscriptionName);
374374
await requestExecutor.execute(command);
@@ -397,7 +397,7 @@ export class DocumentSubscriptions implements IDisposable {
397397
* Force server to close current client subscription connection to the server
398398
*/
399399
public async dropConnection(name: string, database?: string): Promise<void> {
400-
const requestExecutor = this._store.getRequestExecutor(database || this._store.database);
400+
const requestExecutor = this._store.getRequestExecutor(this._store.getEffectiveDatabase(database));
401401

402402
const command = new DropSubscriptionConnectionCommand(name);
403403
return requestExecutor.execute(command);
@@ -407,15 +407,15 @@ export class DocumentSubscriptions implements IDisposable {
407407
public async enable(name: string, database: string)
408408
public async enable(name: string, database?: string) {
409409
const operation = new ToggleOngoingTaskStateOperation(name, "Subscription", false);
410-
await this._store.maintenance.forDatabase(database || this._store.database)
410+
await this._store.maintenance.forDatabase(this._store.getEffectiveDatabase(database))
411411
.send(operation);
412412
}
413413

414414
public async disable(name: string)
415415
public async disable(name: string, database: string)
416416
public async disable(name: string, database?: string) {
417417
const operation = new ToggleOngoingTaskStateOperation(name, "Subscription", true);
418-
await this._store.maintenance.forDatabase(database || this._store.database)
418+
await this._store.maintenance.forDatabase(this._store.getEffectiveDatabase(database))
419419
.send(operation);
420420
}
421421

src/Documents/Subscriptions/SubscriptionWorker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class SubscriptionWorker<T extends object> implements IDisposable {
6868
}
6969

7070
this._store = documentStore;
71-
this._dbName = dbName || documentStore.database;
71+
this._dbName = documentStore.getEffectiveDatabase(dbName);
7272
}
7373

7474
public dispose(): void {
@@ -151,7 +151,7 @@ export class SubscriptionWorker<T extends object> implements IDisposable {
151151

152152
this._ensureParser();
153153

154-
const databaseName = this._dbName || this._store.database;
154+
const databaseName = this._store.getEffectiveDatabase(this._dbName);
155155

156156
const parameters = {
157157
database: databaseName,

test/Issues/RavenDB_11770.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,32 @@ describe("RavenDB_11770Test", function () {
2929
await session.saveChanges();
3030
}
3131

32+
await delay(2);
33+
3234
const fst = new Date();
33-
await delay(1000);
35+
3436
for (let i = 0; i < 3; i++) {
3537
{
3638
const session = store.openSession();
3739
const company = await session.load<Company>(id);
3840
company.name = "Fitzchak" + i;
3941
await session.saveChanges();
42+
43+
await delay(2);
4044
}
4145
}
4246

4347
const snd = new Date();
44-
await delay(1000);
48+
4549
for (let i = 0; i < 3; i++) {
4650
{
4751
const session = store.openSession();
4852
const company = await session.load<Company>(id);
4953
company.name = "Oren" + i;
5054
await session.saveChanges();
5155
}
56+
57+
await delay(2);
5258
}
5359

5460
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { IDocumentStore } from "../../../src";
2+
import { disposeTestDocumentStore, testContext } from "../../Utils/TestUtil";
3+
import { Company } from "../../Assets/Orders";
4+
import { assertThat } from "../../Utils/AssertExtensions";
5+
6+
describe("RavenDB_15690Test", function () {
7+
8+
let store: IDocumentStore;
9+
10+
beforeEach(async function () {
11+
store = await testContext.getDocumentStore();
12+
});
13+
14+
afterEach(async () =>
15+
await disposeTestDocumentStore(store));
16+
17+
it("hasChanges_ShouldDetectDeletes", async () => {
18+
{
19+
const session = store.openSession();
20+
const company = new Company();
21+
company.name = "HR";
22+
await session.store(company, "companies/1");
23+
await session.saveChanges();
24+
}
25+
26+
{
27+
const session = store.openSession();
28+
29+
const company = await session.load("companies/1", Company);
30+
await session.delete(company);
31+
32+
const changes = session.advanced.whatChanged();
33+
assertThat(changes)
34+
.hasSize(1);
35+
assertThat(session.advanced.hasChanges())
36+
.isTrue();
37+
}
38+
});
39+
});

test/Ported/Issues/RavenDB_15706.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("RavenDB_15706", function () {
2121

2222
await assertThrows(() => store.bulkInsert(), err => {
2323
assertThat(err.name)
24-
.isEqualTo("InvalidOperationException");
24+
.isEqualTo("InvalidArgumentException");
2525
})
2626
} finally {
2727
outerStore.dispose();

0 commit comments

Comments
 (0)