Skip to content

Commit 5cda28b

Browse files
committed
conventions: add method aliases to align to naming convention
1 parent 512bd6c commit 5cda28b

17 files changed

+64
-46
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/Conventions/DocumentConventions.ts

Lines changed: 34 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 =
@@ -420,6 +422,10 @@ export class DocumentConventions {
420422
this._findCollectionNameForObjectLiteral = value;
421423
}
422424

425+
public getTypeDescriptorByEntity<T extends object>(entity: T): ObjectTypeDescriptor<T> {
426+
return this.getEntityTypeDescriptor(entity);
427+
}
428+
423429
public getEntityTypeDescriptor<T extends object>(entity: T): ObjectTypeDescriptor<T> {
424430
if (TypeUtil.isClass(entity.constructor)) {
425431
return entity.constructor as ClassConstructor;
@@ -492,7 +498,7 @@ export class DocumentConventions {
492498
* Gets the identity property.
493499
*/
494500
public getIdentityProperty(documentType: DocumentType): string {
495-
const typeDescriptor = this.findEntityType(documentType);
501+
const typeDescriptor = this.getJsTypeByDocumentType(documentType);
496502
return this._registeredIdPropertyNames.get(typeDescriptor)
497503
|| CONSTANTS.Documents.Metadata.ID_PROPERTY;
498504
}
@@ -594,10 +600,6 @@ export class DocumentConventions {
594600
}
595601
}
596602

597-
// TODO:
598-
// public get registeredTypeDescriptors() {
599-
// return this._registeredTypeDescriptors;
600-
// }
601603
public get knownEntityTypesByName() {
602604
return this._knownEntityTypes;
603605
}
@@ -606,6 +608,12 @@ export class DocumentConventions {
606608
return Array.from(this._knownEntityTypes.values());
607609
}
608610

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+
609617
public registerEntityType(entityType: ObjectTypeDescriptor): this;
610618
public registerEntityType(entityType: ObjectTypeDescriptor, name: string): this;
611619
public registerEntityType(entityType: ObjectTypeDescriptor, name?: string): this {
@@ -622,26 +630,36 @@ export class DocumentConventions {
622630
return this;
623631
}
624632

633+
public tryRegisterJsType(docType: DocumentType): this {
634+
return this.tryRegisterEntityType(docType);
635+
}
636+
625637
public tryRegisterEntityType(docType: DocumentType): this {
626638
if (TypeUtil.isObjectTypeDescriptor(docType)) {
627-
this.registerEntityType(docType as ObjectTypeDescriptor);
639+
this.registerJsType(docType as ObjectTypeDescriptor);
628640
}
629641

630642
return this;
631643
}
632644

633-
public findEntityType<T extends object>(documentType: DocumentType<T>): ObjectTypeDescriptor<T>;
634-
public findEntityType<T extends object>(typeName: string): ObjectTypeDescriptor<T>;
635-
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> {
636649
if (!docTypeOrTypeName) {
637650
return null;
638651
}
639652

640-
if (typeof(docTypeOrTypeName) !== "string") {
641-
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;
642660
}
643661

644-
return this._knownEntityTypes.get(docTypeOrTypeName) as ObjectLiteralDescriptor<T>;
662+
return docTypeOrTypeName as ObjectTypeDescriptor<T>;
645663
}
646664

647665
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);

src/Documents/Session/DocumentSession.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
163163

164164
private _prepareLoadInternalOpts<TEntity extends object>(options: LoadOptions<TEntity>) {
165165
const internalOpts: SessionLoadInternalParameters<TEntity> = { documentType: options.documentType };
166-
this.conventions.tryRegisterEntityType(internalOpts.documentType);
166+
this.conventions.tryRegisterJsType(internalOpts.documentType);
167167
if ("includes" in options) {
168168
if (TypeUtil.isFunction(options.includes)) {
169169
const builder = new IncludeBuilder(this.conventions);
@@ -420,7 +420,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
420420
loadOperation.setResult(command.result);
421421
}
422422

423-
const clazz = this.conventions.findEntityType(opts.documentType);
423+
const clazz = this.conventions.getJsTypeByDocumentType(opts.documentType);
424424
return loadOperation.getDocuments(clazz);
425425
}
426426

@@ -539,7 +539,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
539539
public rawQuery<TEntity extends object>(
540540
query: string, documentType?: DocumentType<TEntity>): IRawDocumentQuery<TEntity> {
541541
if (documentType) {
542-
this.conventions.tryRegisterEntityType(documentType);
542+
this.conventions.tryRegisterJsType(documentType);
543543
}
544544

545545
return new RawDocumentQuery(this, query, documentType);
@@ -570,7 +570,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
570570
}
571571

572572
if (opts.documentType) {
573-
this.conventions.tryRegisterEntityType(opts.documentType);
573+
this.conventions.tryRegisterJsType(opts.documentType);
574574
}
575575

576576
const { indexName, collection } = this._processQueryParameters(opts, this.conventions);
@@ -590,7 +590,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
590590
}
591591

592592
if (!isIndex && !isCollection) {
593-
const entityType = this.conventions.findEntityType(opts.documentType);
593+
const entityType = this.conventions.getJsTypeByDocumentType(opts.documentType);
594594
collection = this.conventions.getCollectionNameForType(entityType)
595595
|| CONSTANTS.Documents.Metadata.ALL_DOCUMENTS_COLLECTION;
596596
}
@@ -808,7 +808,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
808808
const docsReadable = streamOperation.setResult(command.result);
809809
let clazz = null;
810810
if (opts && "documentType" in opts) {
811-
clazz = this.conventions.findEntityType(opts.documentType);
811+
clazz = this.conventions.getJsTypeByDocumentType(opts.documentType);
812812
}
813813

814814
const result = this._getStreamResultTransform(this, clazz, null);

0 commit comments

Comments
 (0)