Skip to content

Commit f0a3c8d

Browse files
authored
Merge pull request #183 from gregolsky/v4.1
[4.1] Bug fixes, add missing typescript overloads and properties
2 parents afd4589 + 0b9461e commit f0a3c8d

19 files changed

+175
-73
lines changed

src/Documents/BulkInsertOperation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class BulkInsertOperation {
171171
}
172172

173173
if (!("Raven-Node-Type" as keyof MetadataObject in metadata)) {
174-
const descriptor = this._conventions.getEntityTypeDescriptor(entity);
174+
const descriptor = this._conventions.getTypeDescriptorByEntity(entity);
175175
const jsType = this._requestExecutor.conventions.getJsTypeName(descriptor);
176176
if (jsType) {
177177
metadata["Raven-Node-Type"] = jsType;

src/Documents/Changes/ChangesObservable.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ export class ChangesObservable<T, TConnectionState extends IChangesConnectionSta
4545
this._errorSubscribers.add(handler as (error: Error) => void);
4646
break;
4747
}
48+
49+
return this;
4850
}
4951

5052
public removeListener(event: "data", handler: (value: T) => void);
5153
public removeListener(event: "error", handler: (error: Error) => void);
5254
public removeListener(event: "data" | "error", handler: ((value: T) => void) | ((error: Error) => void)) {
53-
this.off(event as any, handler as any);
55+
return this.off(event as any, handler as any);
5456
}
5557

5658
public off(event: "data", handler: (value: T) => void);
@@ -77,6 +79,8 @@ export class ChangesObservable<T, TConnectionState extends IChangesConnectionSta
7779
}
7880
break;
7981
}
82+
83+
return this;
8084
}
8185

8286
public send(msg: T): void {

src/Documents/Conventions/DocumentConventions.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ export class DocumentConventions {
7979
this._identityPartsSeparator = "/";
8080

8181
this._findIdentityPropertyNameFromCollectionName = () => "id";
82+
8283
this._findJsType = (id: string, doc: object) => {
8384
const metadata = doc[CONSTANTS.Documents.Metadata.KEY];
8485
if (metadata) {
8586
const jsType = metadata[CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] as string;
86-
return this._knownEntityTypes.get(jsType) || null;
87+
return this.getJsTypeByDocumentType(jsType);
8788
}
8889

8990
return null;
@@ -94,11 +95,12 @@ export class DocumentConventions {
9495
return null;
9596
}
9697

97-
if (TypeUtil.isFunction(ctorOrTypeChecker["isType"])) {
98-
return (ctorOrTypeChecker as ObjectLiteralDescriptor).name;
98+
const name = (ctorOrTypeChecker as ClassConstructor).name;
99+
if (name === "Object") {
100+
return null;
99101
}
100102

101-
return (ctorOrTypeChecker as ClassConstructor).name;
103+
return name;
102104
};
103105

104106
this._transformClassCollectionNameToDocumentIdPrefix =
@@ -161,6 +163,15 @@ export class DocumentConventions {
161163
this._remoteEntityFieldNameConvention = val;
162164
}
163165

166+
public set useOptimisticConcurrency(val) {
167+
this._assertNotFrozen();
168+
this._useOptimisticConcurrency = val;
169+
}
170+
171+
public get useOptimisticConcurrency() {
172+
return this._useOptimisticConcurrency;
173+
}
174+
164175
public deserializeEntityFromJson(documentType: ObjectTypeDescriptor, document: object): object {
165176
try {
166177
const typeName = documentType ? documentType.name : null;
@@ -411,6 +422,10 @@ export class DocumentConventions {
411422
this._findCollectionNameForObjectLiteral = value;
412423
}
413424

425+
public getTypeDescriptorByEntity<T extends object>(entity: T): ObjectTypeDescriptor<T> {
426+
return this.getEntityTypeDescriptor(entity);
427+
}
428+
414429
public getEntityTypeDescriptor<T extends object>(entity: T): ObjectTypeDescriptor<T> {
415430
if (TypeUtil.isClass(entity.constructor)) {
416431
return entity.constructor as ClassConstructor;
@@ -483,7 +498,7 @@ export class DocumentConventions {
483498
* Gets the identity property.
484499
*/
485500
public getIdentityProperty(documentType: DocumentType): string {
486-
const typeDescriptor = this.findEntityType(documentType);
501+
const typeDescriptor = this.getJsTypeByDocumentType(documentType);
487502
return this._registeredIdPropertyNames.get(typeDescriptor)
488503
|| CONSTANTS.Documents.Metadata.ID_PROPERTY;
489504
}
@@ -585,10 +600,6 @@ export class DocumentConventions {
585600
}
586601
}
587602

588-
// TODO:
589-
// public get registeredTypeDescriptors() {
590-
// return this._registeredTypeDescriptors;
591-
// }
592603
public get knownEntityTypesByName() {
593604
return this._knownEntityTypes;
594605
}
@@ -597,6 +608,12 @@ export class DocumentConventions {
597608
return Array.from(this._knownEntityTypes.values());
598609
}
599610

611+
public registerJsType(entityType: ObjectTypeDescriptor): this;
612+
public registerJsType(entityType: ObjectTypeDescriptor, name: string): this;
613+
public registerJsType(entityType: ObjectTypeDescriptor, name?: string): this {
614+
return this.registerEntityType(entityType, name);
615+
}
616+
600617
public registerEntityType(entityType: ObjectTypeDescriptor): this;
601618
public registerEntityType(entityType: ObjectTypeDescriptor, name: string): this;
602619
public registerEntityType(entityType: ObjectTypeDescriptor, name?: string): this {
@@ -613,26 +630,36 @@ export class DocumentConventions {
613630
return this;
614631
}
615632

633+
public tryRegisterJsType(docType: DocumentType): this {
634+
return this.tryRegisterEntityType(docType);
635+
}
636+
616637
public tryRegisterEntityType(docType: DocumentType): this {
617638
if (TypeUtil.isObjectTypeDescriptor(docType)) {
618-
this.registerEntityType(docType as ObjectTypeDescriptor);
639+
this.registerJsType(docType as ObjectTypeDescriptor);
619640
}
620641

621642
return this;
622643
}
623644

624-
public findEntityType<T extends object>(documentType: DocumentType<T>): ObjectTypeDescriptor<T>;
625-
public findEntityType<T extends object>(typeName: string): ObjectTypeDescriptor<T>;
626-
public findEntityType<T extends object>(docTypeOrTypeName: string | DocumentType<T>): ObjectTypeDescriptor<T> {
645+
public getJsTypeByDocumentType<T extends object>(documentType: DocumentType<T>): ObjectTypeDescriptor<T>;
646+
public getJsTypeByDocumentType<T extends object>(typeName: string): ObjectTypeDescriptor<T>;
647+
public getJsTypeByDocumentType<T extends object>(
648+
docTypeOrTypeName: string | DocumentType<T>): ObjectTypeDescriptor<T> {
627649
if (!docTypeOrTypeName) {
628650
return null;
629651
}
630652

631-
if (typeof(docTypeOrTypeName) !== "string") {
632-
return docTypeOrTypeName as ObjectTypeDescriptor<T>;
653+
if (typeof(docTypeOrTypeName) === "string") {
654+
return this._knownEntityTypes.get(
655+
docTypeOrTypeName) as ObjectLiteralDescriptor<T> || null;
656+
}
657+
658+
if (docTypeOrTypeName.name === "Object") {
659+
return null;
633660
}
634661

635-
return this._knownEntityTypes.get(docTypeOrTypeName) as ObjectLiteralDescriptor<T>;
662+
return docTypeOrTypeName as ObjectTypeDescriptor<T>;
636663
}
637664

638665
public transformObjectKeysToRemoteFieldNameConvention(obj: object, opts?: ObjectChangeCaseOptions) {

src/Documents/Identity/HiloMultiTypeIdGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class HiloMultiTypeIdGenerator extends AbstractHiloIdGenerator implements
1515
}
1616

1717
public generateDocumentId(entity: object, documentType?: string): Promise<string> {
18-
const entityType = this._conventions.findEntityType(documentType);
18+
const entityType = this._conventions.getJsTypeByDocumentType(documentType);
1919
const typeTagName: string = entityType
2020
? this._conventions.getCollectionNameForType(entityType)
2121
: this._conventions.getCollectionNameForEntity(entity);

src/Documents/Operations/CompareExchange/CompareExchangeResult.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class CompareExchangeResult<T> {
6060
conventions: DocumentConventions,
6161
clazz?: ClassConstructor<T>): CompareExchangeResult<T> {
6262

63-
conventions.tryRegisterEntityType(clazz);
63+
conventions.tryRegisterJsType(clazz);
6464

6565
if (!val) {
6666
const emptyExchangeResult = new CompareExchangeResult<T>();
@@ -75,7 +75,7 @@ export class CompareExchangeResult<T> {
7575
result = val as any as T;
7676
} else {
7777
// val comes here with proper key case already
78-
const entityType = conventions.findEntityType(clazz);
78+
const entityType = conventions.getJsTypeByDocumentType(clazz);
7979
result = conventions.deserializeEntityFromJson(entityType, val) as any as T;
8080
}
8181

src/Documents/Operations/CompareExchange/CompareExchangeValueResultParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class CompareExchangeValueResultParser {
5353
if (!rawValue) {
5454
results[key] = new CompareExchangeValue(key, index, null);
5555
} else {
56-
const entityType = conventions.findEntityType(clazz);
56+
const entityType = conventions.getJsTypeByDocumentType(clazz);
5757
if (conventions.entityFieldNameConvention) {
5858
rawValue = ObjectUtil.transformObjectKeys(
5959
rawValue, {

src/Documents/Operations/CompareExchange/PutCompareExchangeValueOperation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class PutCompareExchangeValueCommand<T> extends RavenCommand<CompareExcha
9797
.process(bodyStream);
9898

9999
const type = !TypeUtil.isPrimitive(this._value)
100-
? this._conventions.getEntityTypeDescriptor(this._value as any) as ObjectTypeDescriptor
100+
? this._conventions.getTypeDescriptorByEntity(this._value as any) as ObjectTypeDescriptor
101101
: null;
102102
const clazz: ClassConstructor<T> = TypeUtil.isClass(type) ? type as any : null;
103103
this.result = CompareExchangeResult.parseFromObject(resObj, this._conventions, clazz);

src/Documents/Operations/OperationExecutor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ export class OperationExecutor {
9595
const patchResult = command.result as any as PatchResult;
9696
patchOperationResult.status = patchResult.status;
9797
const { conventions } = this._requestExecutor;
98-
conventions.tryRegisterEntityType(documentType);
99-
const entityType = conventions.findEntityType(documentType);
98+
conventions.tryRegisterJsType(documentType);
99+
const entityType = conventions.getJsTypeByDocumentType(documentType);
100100
patchOperationResult.document = conventions.deserializeEntityFromJson(
101101
entityType, patchResult.modifiedDocument) as TResult;
102102
return patchOperationResult;

src/Documents/Session/AbstractDocumentQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ export abstract class AbstractDocumentQuery<T extends object, TSelf extends Abst
20022002
this._queryOperation = this._initializeQueryOperation();
20032003
}
20042004

2005-
const clazz = this._conventions.findEntityType(this._clazz);
2005+
const clazz = this._conventions.getJsTypeByDocumentType(this._clazz);
20062006
const lazyQueryOperation = new LazyQueryOperation<T>(
20072007
this._theSession.conventions,
20082008
this._queryOperation,
@@ -2018,7 +2018,7 @@ export abstract class AbstractDocumentQuery<T extends object, TSelf extends Abst
20182018
this._queryOperation = this._initializeQueryOperation();
20192019
}
20202020

2021-
const clazz = this._conventions.findEntityType(this._clazz);
2021+
const clazz = this._conventions.getJsTypeByDocumentType(this._clazz);
20222022
const lazyQueryOperation =
20232023
new LazyQueryOperation<T>(
20242024
this._theSession.conventions,

src/Documents/Session/DocumentQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class DocumentQuery<T extends object>
102102
propertiesOrQueryData: string | string[] | QueryData,
103103
projectionType?: DocumentType<TProjection>): IDocumentQuery<TProjection> {
104104
if (projectionType) {
105-
this._theSession.conventions.tryRegisterEntityType(projectionType);
105+
this._theSession.conventions.tryRegisterJsType(projectionType);
106106
}
107107

108108
if (TypeUtil.isString(propertiesOrQueryData)) {
@@ -466,7 +466,7 @@ export class DocumentQuery<T extends object>
466466

467467
public ofType<TResult extends object>(tResultClass: DocumentType<TResult>): IDocumentQuery<TResult> {
468468
if (tResultClass) {
469-
this._theSession.conventions.tryRegisterEntityType(tResultClass);
469+
this._theSession.conventions.tryRegisterJsType(tResultClass);
470470
}
471471

472472
return this._createDocumentQueryInternal(tResultClass);

0 commit comments

Comments
 (0)