Skip to content

Commit fb69e37

Browse files
committed
conventions: add method aliases to align to naming convention
1 parent 980f5a4 commit fb69e37

17 files changed

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

Lines changed: 34 additions & 17 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 =
@@ -421,6 +422,10 @@ export class DocumentConventions {
421422
this._findCollectionNameForObjectLiteral = value;
422423
}
423424

425+
public getTypeDescriptorByEntity<T extends object>(entity: T): ObjectTypeDescriptor<T> {
426+
return this.getEntityTypeDescriptor(entity);
427+
}
428+
424429
public getEntityTypeDescriptor<T extends object>(entity: T): ObjectTypeDescriptor<T> {
425430
if (TypeUtil.isClass(entity.constructor)) {
426431
return entity.constructor as ClassConstructor;
@@ -497,7 +502,7 @@ export class DocumentConventions {
497502
* Gets the identity property.
498503
*/
499504
public getIdentityProperty(documentType: DocumentType): string {
500-
const typeDescriptor = this.findEntityType(documentType);
505+
const typeDescriptor = this.getJsTypeByDocumentType(documentType);
501506
return this._registeredIdPropertyNames.get(typeDescriptor)
502507
|| CONSTANTS.Documents.Metadata.ID_PROPERTY;
503508
}
@@ -599,10 +604,6 @@ export class DocumentConventions {
599604
}
600605
}
601606

602-
// TODO:
603-
// public get registeredTypeDescriptors() {
604-
// return this._registeredTypeDescriptors;
605-
// }
606607
public get knownEntityTypesByName() {
607608
return this._knownEntityTypes;
608609
}
@@ -611,6 +612,12 @@ export class DocumentConventions {
611612
return Array.from(this._knownEntityTypes.values());
612613
}
613614

615+
public registerJsType(entityType: ObjectTypeDescriptor): this;
616+
public registerJsType(entityType: ObjectTypeDescriptor, name: string): this;
617+
public registerJsType(entityType: ObjectTypeDescriptor, name?: string): this {
618+
return this.registerEntityType(entityType, name);
619+
}
620+
614621
public registerEntityType(entityType: ObjectTypeDescriptor): this;
615622
public registerEntityType(entityType: ObjectTypeDescriptor, name: string): this;
616623
public registerEntityType(entityType: ObjectTypeDescriptor, name?: string): this {
@@ -627,26 +634,36 @@ export class DocumentConventions {
627634
return this;
628635
}
629636

637+
public tryRegisterJsType(docType: DocumentType): this {
638+
return this.tryRegisterEntityType(docType);
639+
}
640+
630641
public tryRegisterEntityType(docType: DocumentType): this {
631642
if (TypeUtil.isObjectTypeDescriptor(docType)) {
632-
this.registerEntityType(docType as ObjectTypeDescriptor);
643+
this.registerJsType(docType as ObjectTypeDescriptor);
633644
}
634645

635646
return this;
636647
}
637648

638-
public findEntityType<T extends object>(documentType: DocumentType<T>): ObjectTypeDescriptor<T>;
639-
public findEntityType<T extends object>(typeName: string): ObjectTypeDescriptor<T>;
640-
public findEntityType<T extends object>(docTypeOrTypeName: string | DocumentType<T>): ObjectTypeDescriptor<T> {
649+
public getJsTypeByDocumentType<T extends object>(documentType: DocumentType<T>): ObjectTypeDescriptor<T>;
650+
public getJsTypeByDocumentType<T extends object>(typeName: string): ObjectTypeDescriptor<T>;
651+
public getJsTypeByDocumentType<T extends object>(
652+
docTypeOrTypeName: string | DocumentType<T>): ObjectTypeDescriptor<T> {
641653
if (!docTypeOrTypeName) {
642654
return null;
643655
}
644656

645-
if (typeof(docTypeOrTypeName) !== "string") {
646-
return docTypeOrTypeName as ObjectTypeDescriptor<T>;
657+
if (typeof(docTypeOrTypeName) === "string") {
658+
return this._knownEntityTypes.get(
659+
docTypeOrTypeName) as ObjectLiteralDescriptor<T> || null;
660+
}
661+
662+
if (docTypeOrTypeName.name === "Object") {
663+
return null;
647664
}
648665

649-
return this._knownEntityTypes.get(docTypeOrTypeName) as ObjectLiteralDescriptor<T>;
666+
return docTypeOrTypeName as ObjectTypeDescriptor<T>;
650667
}
651668

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

src/Documents/Session/DocumentSession.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
138138
callback = callback || TypeUtil.NOOP;
139139
options = options || {};
140140

141-
this.conventions.tryRegisterEntityType(options.documentType);
141+
this.conventions.tryRegisterJsType(options.documentType);
142142
const loadInternalPromise = this.loadInternal(ids, options.includes, options.documentType)
143143
.then((docs: EntitiesCollectionObject<TEntity> | TEntity) => {
144144
if (isLoadingSingle) {
@@ -373,7 +373,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
373373
loadOperation.setResult(command.result);
374374
}
375375

376-
const clazz = this.conventions.findEntityType(documentType);
376+
const clazz = this.conventions.getJsTypeByDocumentType(documentType);
377377
return loadOperation.getDocuments(clazz);
378378
}
379379

@@ -492,7 +492,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
492492
public rawQuery<TEntity extends object>(
493493
query: string, documentType?: DocumentType<TEntity>): IRawDocumentQuery<TEntity> {
494494
if (documentType) {
495-
this.conventions.tryRegisterEntityType(documentType);
495+
this.conventions.tryRegisterJsType(documentType);
496496
}
497497

498498
return new RawDocumentQuery(this, query, documentType);
@@ -523,7 +523,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
523523
}
524524

525525
if (opts.documentType) {
526-
this.conventions.tryRegisterEntityType(opts.documentType);
526+
this.conventions.tryRegisterJsType(opts.documentType);
527527
}
528528

529529
const { indexName, collection } = this._processQueryParameters(opts, this.conventions);
@@ -543,7 +543,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
543543
}
544544

545545
if (!isIndex && !isCollection) {
546-
const entityType = this.conventions.findEntityType(opts.documentType);
546+
const entityType = this.conventions.getJsTypeByDocumentType(opts.documentType);
547547
collection = this.conventions.getCollectionNameForType(entityType)
548548
|| CONSTANTS.Documents.Metadata.ALL_DOCUMENTS_COLLECTION;
549549
}
@@ -740,7 +740,7 @@ export class DocumentSession extends InMemoryDocumentSessionOperations
740740
const docsReadable = streamOperation.setResult(command.result);
741741
let clazz = null;
742742
if (opts && "documentType" in opts) {
743-
clazz = this.conventions.findEntityType(opts.documentType);
743+
clazz = this.conventions.getJsTypeByDocumentType(opts.documentType);
744744
}
745745

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

0 commit comments

Comments
 (0)