Skip to content

Commit e29e2c6

Browse files
authored
Merge pull request #182 from gregolsky/v4.0
Bug fixes, add missing typescript overloads and properties
2 parents 71e5c32 + c706aa7 commit e29e2c6

21 files changed

+203
-82
lines changed

src/Documents/BulkInsertOperation.ts

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

172172
if (!(CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE in metadata)) {
173-
const descriptor = this._conventions.getEntityTypeDescriptor(entity);
173+
const descriptor = this._conventions.getTypeDescriptorByEntity(entity);
174174
const jsType = this._requestExecutor.conventions.getJsTypeName(descriptor);
175175
if (jsType) {
176176
metadata[CONSTANTS.Documents.Metadata.RAVEN_JS_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 & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export class DocumentConventions {
4545
private _frozen: boolean;
4646
private _originalConfiguration: ClientConfiguration;
4747
private _idPropertyCache: Map<ObjectTypeDescriptor, string> = new Map(); //TODO: is it used?
48-
// TODO: private _saveEnumsAsIntegers: number;
4948
private _identityPartsSeparator: string;
5049
private _disableTopologyUpdates: boolean;
5150

@@ -80,11 +79,12 @@ export class DocumentConventions {
8079
this._identityPartsSeparator = "/";
8180

8281
this._findIdentityPropertyNameFromCollectionName = () => "id";
82+
8383
this._findJsType = (id: string, doc: object) => {
8484
const metadata = doc[CONSTANTS.Documents.Metadata.KEY];
8585
if (metadata) {
8686
const jsType = metadata[CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] as string;
87-
return this._knownEntityTypes.get(jsType) || null;
87+
return this.getJsTypeByDocumentType(jsType);
8888
}
8989

9090
return null;
@@ -95,11 +95,12 @@ export class DocumentConventions {
9595
return null;
9696
}
9797

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

102-
return (ctorOrTypeChecker as ClassConstructor).name;
103+
return name;
103104
};
104105

105106
this._transformClassCollectionNameToDocumentIdPrefix =
@@ -162,6 +163,15 @@ export class DocumentConventions {
162163
this._remoteEntityFieldNameConvention = val;
163164
}
164165

166+
public set useOptimisticConcurrency(val) {
167+
this._assertNotFrozen();
168+
this._useOptimisticConcurrency = val;
169+
}
170+
171+
public get useOptimisticConcurrency() {
172+
return this._useOptimisticConcurrency;
173+
}
174+
165175
public deserializeEntityFromJson(documentType: ObjectTypeDescriptor, document: object): object {
166176
try {
167177
const typeName = documentType ? documentType.name : null;
@@ -412,6 +422,10 @@ export class DocumentConventions {
412422
this._findCollectionNameForObjectLiteral = value;
413423
}
414424

425+
public getTypeDescriptorByEntity<T extends object>(entity: T): ObjectTypeDescriptor<T> {
426+
return this.getEntityTypeDescriptor(entity);
427+
}
428+
415429
public getEntityTypeDescriptor<T extends object>(entity: T): ObjectTypeDescriptor<T> {
416430
if (TypeUtil.isClass(entity.constructor)) {
417431
return entity.constructor as ClassConstructor;
@@ -457,10 +471,6 @@ export class DocumentConventions {
457471
return this;
458472
}
459473

460-
//TODO public registerEntityTypeChecker(typeChecker: ObjectLiteralDescriptor) {
461-
// this._registeredTypeDescriptors.push(typeChecker);
462-
// }
463-
464474
public registerEntityIdPropertyName(ctorOrTypeChecker: ObjectTypeDescriptor, idProperty: string) {
465475
this._registeredIdPropertyNames.set(ctorOrTypeChecker, idProperty);
466476
}
@@ -488,7 +498,7 @@ export class DocumentConventions {
488498
* Gets the identity property.
489499
*/
490500
public getIdentityProperty(documentType: DocumentType): string {
491-
const typeDescriptor = this.findEntityType(documentType);
501+
const typeDescriptor = this.getJsTypeByDocumentType(documentType);
492502
return this._registeredIdPropertyNames.get(typeDescriptor)
493503
|| CONSTANTS.Documents.Metadata.ID_PROPERTY;
494504
}
@@ -590,10 +600,6 @@ export class DocumentConventions {
590600
}
591601
}
592602

593-
// TODO:
594-
// public get registeredTypeDescriptors() {
595-
// return this._registeredTypeDescriptors;
596-
// }
597603
public get knownEntityTypesByName() {
598604
return this._knownEntityTypes;
599605
}
@@ -602,6 +608,12 @@ export class DocumentConventions {
602608
return Array.from(this._knownEntityTypes.values());
603609
}
604610

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+
605617
public registerEntityType(entityType: ObjectTypeDescriptor): this;
606618
public registerEntityType(entityType: ObjectTypeDescriptor, name: string): this;
607619
public registerEntityType(entityType: ObjectTypeDescriptor, name?: string): this {
@@ -618,26 +630,36 @@ export class DocumentConventions {
618630
return this;
619631
}
620632

633+
public tryRegisterJsType(docType: DocumentType): this {
634+
return this.tryRegisterEntityType(docType);
635+
}
636+
621637
public tryRegisterEntityType(docType: DocumentType): this {
622638
if (TypeUtil.isObjectTypeDescriptor(docType)) {
623-
this.registerEntityType(docType as ObjectTypeDescriptor);
639+
this.registerJsType(docType as ObjectTypeDescriptor);
624640
}
625641

626642
return this;
627643
}
628644

629-
public findEntityType<T extends object>(documentType: DocumentType<T>): ObjectTypeDescriptor<T>;
630-
public findEntityType<T extends object>(typeName: string): ObjectTypeDescriptor<T>;
631-
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> {
632649
if (!docTypeOrTypeName) {
633650
return null;
634651
}
635652

636-
if (typeof(docTypeOrTypeName) !== "string") {
637-
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;
638660
}
639661

640-
return this._knownEntityTypes.get(docTypeOrTypeName) as ObjectLiteralDescriptor<T>;
662+
return docTypeOrTypeName as ObjectTypeDescriptor<T>;
641663
}
642664

643665
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
@@ -90,8 +90,8 @@ export class OperationExecutor {
9090
const patchResult = command.result as any as PatchResult;
9191
patchOperationResult.status = patchResult.status;
9292
const { conventions } = this._requestExecutor;
93-
conventions.tryRegisterEntityType(documentType);
94-
const entityType = conventions.findEntityType(documentType);
93+
conventions.tryRegisterJsType(documentType);
94+
const entityType = conventions.getJsTypeByDocumentType(documentType);
9595
patchOperationResult.document = conventions.deserializeEntityFromJson(
9696
entityType, patchResult.modifiedDocument) as TResult;
9797
return patchOperationResult;

src/Documents/Session/AbstractDocumentQuery.ts

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

1910-
const clazz = this._conventions.findEntityType(this._clazz);
1910+
const clazz = this._conventions.getJsTypeByDocumentType(this._clazz);
19111911
const lazyQueryOperation = new LazyQueryOperation<T>(
19121912
this._theSession.conventions,
19131913
this._queryOperation,
@@ -1923,7 +1923,7 @@ export abstract class AbstractDocumentQuery<T extends object, TSelf extends Abst
19231923
this._queryOperation = this._initializeQueryOperation();
19241924
}
19251925

1926-
const clazz = this._conventions.findEntityType(this._clazz);
1926+
const clazz = this._conventions.getJsTypeByDocumentType(this._clazz);
19271927
const lazyQueryOperation =
19281928
new LazyQueryOperation<T>(
19291929
this._theSession.conventions,

src/Documents/Session/DocumentQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class DocumentQuery<T extends object>
9494
propertiesOrQueryData: string | string[] | QueryData,
9595
projectionType?: DocumentType<TProjection>): IDocumentQuery<TProjection> {
9696
if (projectionType) {
97-
this._theSession.conventions.tryRegisterEntityType(projectionType);
97+
this._theSession.conventions.tryRegisterJsType(projectionType);
9898
}
9999

100100
if (TypeUtil.isString(propertiesOrQueryData)) {
@@ -428,7 +428,7 @@ export class DocumentQuery<T extends object>
428428

429429
public ofType<TResult extends object>(tResultClass: DocumentType<TResult>): IDocumentQuery<TResult> {
430430
if (tResultClass) {
431-
this._theSession.conventions.tryRegisterEntityType(tResultClass);
431+
this._theSession.conventions.tryRegisterJsType(tResultClass);
432432
}
433433

434434
return this._createDocumentQueryInternal(tResultClass);

0 commit comments

Comments
 (0)