Skip to content

Commit

Permalink
feat: schema version
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Jul 3, 2024
1 parent 2bc7ab8 commit 2590674
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
25 changes: 23 additions & 2 deletions packages/reference/src/collection-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
/**
* Alwatr store engine file format version number.
*/
static readonly fileFormatVersion = 1;
static readonly fileFormatVersion = 2;

/**
* Creates new CollectionReference instance from stat.
Expand Down Expand Up @@ -64,6 +64,7 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
extension: StoreFileExtension.Json,
ver: CollectionReference.version,
fv: CollectionReference.fileFormatVersion,
schemaVer: 1
},
data: initialData ?? {},
};
Expand Down Expand Up @@ -140,7 +141,11 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
throw new Error('store_version_incompatible', {cause: context.meta});
}

// if (context.meta.fv === 1) migrate_to_2
if (context.meta.fv === 1) {
// migrate from v1 to v2
context.meta.schemaVer = 0;
context.meta.fv = 2;
}

context.meta.ver = CollectionReference.version;
}
Expand Down Expand Up @@ -194,6 +199,22 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
this.logger__.logMethodArgs?.('new', {id: this.id});
}

/**
* get store schema version
*
* @returns store schema version
*/
get schemaVer(): number {
return this.context__.meta.schemaVer;
}

/**
* set store schema version for migrate
*/
set schemaVer(ver: number) {
this.context__.meta.schemaVer = ver;
}

/**
* Checks if an item exists in the collection.
*
Expand Down
27 changes: 26 additions & 1 deletion packages/reference/src/document-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
/**
* Alwatr store engine file format version number.
*/
static readonly fileFormatVersion = 1;
static readonly fileFormatVersion = 2;

/**
* Creates new DocumentReference instance from stat and initial data.
Expand Down Expand Up @@ -53,6 +53,7 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
extension: StoreFileExtension.Json,
ver: DocumentReference.version,
fv: DocumentReference.fileFormatVersion,
schemaVer: 1
},
data: initialData,
};
Expand Down Expand Up @@ -128,6 +129,14 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
logger.accident('doc.migrateContext__', 'store_version_incompatible', context.meta);
throw new Error('store_version_incompatible', {cause: context.meta});
}

if (context.meta.fv === 1) {
// migrate from v1 to v2
context.meta.schemaVer = 0;
context.meta.fv = 2;
}

context.meta.ver = DocumentReference.version;
}

/**
Expand Down Expand Up @@ -174,6 +183,22 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
this.logger__.logMethodArgs?.('new', {path: this.path});
}

/**
* get store schema version
*
* @returns store schema version
*/
get schemaVer(): number {
return this.context__.meta.schemaVer;
}

/**
* set store schema version for migrate
*/
set schemaVer(ver: number) {
this.context__.meta.schemaVer = ver;
}

/**
* Retrieves the document's data.
*
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ export type StoreFileMeta = StoreFileStat & {
*/
ver: string;

/**
* The schema version for easy migration by user.
* default is `0` for old migrate data an `1` for new define storage.
*/
schemaVer: number;

/**
* Store file format version.
*/
Expand Down

0 comments on commit 2590674

Please sign in to comment.