Skip to content

Commit

Permalink
refactor: Update validateContext__ method in CollectionReference and …
Browse files Browse the repository at this point in the history
…DocumentReference
  • Loading branch information
alimd committed Sep 1, 2024
1 parent 40052d0 commit bb0ae6a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
35 changes: 16 additions & 19 deletions packages/reference/src/collection-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,31 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb

/**
* Validates the collection context and try to migrate it to the latest version.
*
* @param context collection context
*/
private static validateContext__(context: CollectionContext<JsonifiableObject>): void {
logger.logMethodArgs?.('col.validateContext__', {name: context.meta?.name});
private validateContext__(): void {
this.logger__.logMethod?.('validateContext__');

if (context.ok !== true) {
logger.accident?.('col.validateContext__', 'store_not_ok', context);
throw new Error('store_not_ok', {cause: context});
if (this.context__.ok !== true) {
logger.accident?.('col.validateContext__', 'store_not_ok');
throw new Error('store_not_ok', {cause: {context: this.context__}});
}

if (context.meta === undefined) {
logger.accident?.('col.validateContext__', 'store_meta_undefined', context);
throw new Error('store_meta_undefined', {cause: context});
if (this.context__.meta === undefined) {
logger.accident?.('col.validateContext__', 'store_meta_undefined');
throw new Error('store_meta_undefined', {cause: {context: this.context__}});
}

if (context.meta.type !== StoreFileType.Collection) {
logger.accident?.('col.validateContext__', 'collection_type_invalid', context.meta);
throw new Error('collection_type_invalid', {cause: context.meta});
if (this.context__.meta.type !== StoreFileType.Collection) {
logger.accident?.('col.validateContext__', 'collection_type_invalid', this.context__.meta);
throw new Error('collection_type_invalid', {cause: this.context__.meta});
}

if (context.meta.ver !== CollectionReference.version) {
logger.incident?.('col.validateContext__', 'store_version_incompatible', {
fileVersion: context.meta.ver,
currentVersion: CollectionReference.version,
if (this.context__.meta.fv !== CollectionReference.fileFormatVersion) {
this.logger__.incident?.('validateContext__', 'store_file_version_incompatible', {
old: this.context__.meta.fv,
new: CollectionReference.fileFormatVersion,
});

CollectionReference.migrateContext__(context);
this.migrateContext__();
}
}

Expand Down
35 changes: 16 additions & 19 deletions packages/reference/src/document-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,31 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec

/**
* Validates the document context and try to migrate it to the latest version.
*
* @param context document context
*/
private static validateContext__(context: DocumentContext<JsonifiableObject>): void {
logger.logMethodArgs?.('doc.validateContext__', {name: context.meta?.name});
private validateContext__(): void {
this.logger__.logMethod?.('validateContext__');

if (context.ok !== true) {
logger.accident?.('doc.validateContext__', 'store_not_ok', context);
throw new Error('store_not_ok', {cause: context});
if (this.context__.ok !== true) {
this.logger__.accident?.('validateContext__', 'store_not_ok');
throw new Error('store_not_ok', {cause: {context: this.context__}});
}

if (context.meta === undefined) {
logger.accident?.('doc.validateContext__', 'store_meta_undefined', context);
throw new Error('store_meta_undefined', {cause: context});
if (this.context__.meta === undefined) {
this.logger__.accident?.('validateContext__', 'store_meta_undefined');
throw new Error('store_meta_undefined', {cause: {context: this.context__}});
}

if (context.meta.type !== StoreFileType.Document) {
logger.accident?.('doc.validateContext__', 'document_type_invalid', context.meta);
throw new Error('document_type_invalid', {cause: context.meta});
if (this.context__.meta.type !== StoreFileType.Document) {
this.logger__.accident?.('validateContext__', 'document_type_invalid', this.context__.meta);
throw new Error('document_type_invalid', {cause: this.context__.meta});
}

if (context.meta.ver !== DocumentReference.version) {
logger.incident?.('doc.validateContext__', 'store_version_incompatible', {
fileVersion: context.meta.ver,
currentVersion: DocumentReference.version,
if (this.context__.meta.fv !== DocumentReference.fileFormatVersion) {
this.logger__.incident?.('validateContext__', 'store_file_version_incompatible', {
old: this.context__.meta.fv,
new: DocumentReference.fileFormatVersion,
});

DocumentReference.migrateContext__(context);
this.migrateContext__();
}
}

Expand Down

0 comments on commit bb0ae6a

Please sign in to comment.