Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: schema version #259

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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