diff --git a/src/Documents/Conventions/DocumentConventions.ts b/src/Documents/Conventions/DocumentConventions.ts index 3e98341d6..0603da9f4 100644 --- a/src/Documents/Conventions/DocumentConventions.ts +++ b/src/Documents/Conventions/DocumentConventions.ts @@ -50,10 +50,11 @@ export class DocumentConventions { private _transformClassCollectionNameToDocumentIdPrefix: (maybeClassCollectionName: string) => string; private _documentIdGenerator: IdConvention; - private _findIdentityPropertyNameFromCollectionName: (collectionName: string) => string; private _findCollectionName: (constructorOrTypeChecker: ObjectTypeDescriptor) => string; + private _identityProperty: string; + private _findJsTypeName: (ctorOrTypeChecker: ObjectTypeDescriptor) => string; private _findJsType: (id: string, doc: object) => ObjectTypeDescriptor; @@ -77,8 +78,7 @@ export class DocumentConventions { public constructor() { this._readBalanceBehavior = "None"; this._identityPartsSeparator = "/"; - - this._findIdentityPropertyNameFromCollectionName = () => "id"; + this._identityProperty = CONSTANTS.Documents.Metadata.ID_PROPERTY; this._findJsType = (id: string, doc: object) => { const metadata = doc[CONSTANTS.Documents.Metadata.KEY]; @@ -271,6 +271,15 @@ export class DocumentConventions { this._useOptimisticConcurrency = useOptimisticConcurrency; } + public get identityProperty() { + return this._identityProperty; + } + + public set identityProperty(val) { + this._assertNotFrozen(); + this._identityProperty = val; + } + public get findJsType() { return this._findJsType; } @@ -298,15 +307,6 @@ export class DocumentConventions { this._findCollectionName = value; } - public get findIdentityPropertyNameFromCollectionName() { - return this._findIdentityPropertyNameFromCollectionName; - } - - public set findIdentityPropertyNameFromCollectionName(value) { - this._assertNotFrozen(); - this._findIdentityPropertyNameFromCollectionName = value; - } - public get documentIdGenerator() { return this._documentIdGenerator; } @@ -500,7 +500,7 @@ export class DocumentConventions { public getIdentityProperty(documentType: DocumentType): string { const typeDescriptor = this.getJsTypeByDocumentType(documentType); return this._registeredIdPropertyNames.get(typeDescriptor) - || CONSTANTS.Documents.Metadata.ID_PROPERTY; + || this._identityProperty; } public updateFrom(configuration: ClientConfiguration): void { diff --git a/src/Http/RequestExecutor.ts b/src/Http/RequestExecutor.ts index 87b97232b..d319f4c27 100644 --- a/src/Http/RequestExecutor.ts +++ b/src/Http/RequestExecutor.ts @@ -2,7 +2,7 @@ import * as os from "os"; import * as BluebirdPromise from "bluebird"; import * as semaphore from "semaphore"; import * as stream from "readable-stream"; -import { acquireSemaphore } from "../Utility/SemaphoreUtil"; +import { acquireSemaphore, AcquiredSemaphoreContext } from "../Utility/SemaphoreUtil"; import { getLogger, ILogger } from "../Utility/LogUtil"; import { Timer } from "../Primitives/Timer"; import { ServerNode } from "./ServerNode"; @@ -327,54 +327,53 @@ export class RequestExecutor implements IDisposable { .then(() => this._nodeSelector.getFastestNode()); } - protected _updateClientConfiguration(): PromiseLike { - if (this._disposed) { - return BluebirdPromise.resolve(null); - } + private async _updateClientConfigurationInternal(): Promise { + const oldDisableClientConfigurationUpdates = this._disableClientConfigurationUpdates; + this._disableClientConfigurationUpdates = true; - const updateClientConfigurationInternal = () => { - const oldDisableClientConfigurationUpdates = this._disableClientConfigurationUpdates; - this._disableClientConfigurationUpdates = true; + try { + if (this._disposed) { + return; + } - return BluebirdPromise.resolve() - .then(() => { + const command = new GetClientConfigurationCommand(); + const { currentNode, currentIndex } = this.chooseNodeForRequest(command, null); + await this.execute(command, null, { + chosenNode: currentNode, + nodeIndex: currentIndex, + shouldRetry: false + }); - if (this._disposed) { - return; - } + const clientConfigOpResult = command.result; + if (!clientConfigOpResult) { + return; + } - const command = new GetClientConfigurationCommand(); - const currentIndexAndNode2: CurrentIndexAndNode = this.chooseNodeForRequest(command, null); - return this.execute(command, null, { - chosenNode: currentIndexAndNode2.currentNode, - nodeIndex: currentIndexAndNode2.currentIndex, - shouldRetry: false - }) - .then(() => command.result); - }) - .then((clientConfigOpResult: GetClientConfigurationOperationResult) => { - if (!clientConfigOpResult) { - return; - } + this._conventions.updateFrom(clientConfigOpResult.configuration); + this._clientConfigurationEtag = clientConfigOpResult.etag; + } catch (err) { + this._log.error(err, "Error getting client configuration."); + } finally { + this._disableClientConfigurationUpdates = oldDisableClientConfigurationUpdates; + } + } - this._conventions.updateFrom(clientConfigOpResult.configuration); - this._clientConfigurationEtag = clientConfigOpResult.etag; + protected async _updateClientConfiguration(): Promise { + if (this._disposed) { + return; + } - }) - .tapCatch(err => this._log.error(err, "Error getting client configuration.")) - .finally(() => { - this._disableClientConfigurationUpdates = oldDisableClientConfigurationUpdates; - }); - }; + let semAcquiredContext: AcquiredSemaphoreContext; - const semAcquiredContext = acquireSemaphore(this._updateClientConfigurationSemaphore); - const result = BluebirdPromise.resolve(semAcquiredContext.promise) - .then(() => updateClientConfigurationInternal()) - .finally(() => { + try { + semAcquiredContext = acquireSemaphore(this._updateClientConfigurationSemaphore); + await semAcquiredContext.promise; + await this._updateClientConfigurationInternal(); + } finally { + if (semAcquiredContext) { semAcquiredContext.dispose(); - }); - - return Promise.resolve(result); + } + } } public updateTopology(node: ServerNode, timeout: number, forceUpdate: boolean = false): Promise { diff --git a/test/Documents/CustomKeyCaseConventionsTests.ts b/test/Documents/CustomKeyCaseConventionsTests.ts index fd3d4b474..61703cfd4 100644 --- a/test/Documents/CustomKeyCaseConventionsTests.ts +++ b/test/Documents/CustomKeyCaseConventionsTests.ts @@ -90,7 +90,7 @@ describe("With custom key case conventions set", function () { s.conventions.findCollectionNameForObjectLiteral = (o) => o["collection"]; s.conventions.entityFieldNameConvention = "camel"; s.conventions.remoteEntityFieldNameConvention = "pascal"; - s.conventions.findIdentityPropertyNameFromCollectionName = () => "Id"; + s.conventions.identityProperty = "Id"; s.conventions.registerEntityIdPropertyName(Object, "Id"); }); @@ -129,7 +129,7 @@ describe("With custom key case conventions set", function () { s.conventions.findCollectionNameForObjectLiteral = (o) => o["collection"]; s.conventions.entityFieldNameConvention = "camel"; s.conventions.remoteEntityFieldNameConvention = "pascal"; - s.conventions.findIdentityPropertyNameFromCollectionName = () => "Id"; + s.conventions.identityProperty = "Id"; s.conventions.registerEntityIdPropertyName(Object, "Id"); }); @@ -190,7 +190,7 @@ describe("With custom key case conventions set", function () { s.conventions.findCollectionNameForObjectLiteral = (o) => o["collection"]; s.conventions.entityFieldNameConvention = "camel"; s.conventions.remoteEntityFieldNameConvention = "pascal"; - s.conventions.findIdentityPropertyNameFromCollectionName = () => "Id"; + s.conventions.identityProperty = "Id"; s.conventions.registerEntityIdPropertyName(Object, "Id"); }); @@ -226,7 +226,7 @@ describe("With custom key case conventions set", function () { s.conventions.findCollectionNameForObjectLiteral = (o) => o["collection"]; s.conventions.entityFieldNameConvention = "camel"; s.conventions.remoteEntityFieldNameConvention = "pascal"; - s.conventions.findIdentityPropertyNameFromCollectionName = () => "Id"; + s.conventions.identityProperty = "Id"; s.conventions.registerEntityIdPropertyName(Object, "Id"); });