From 37269479e483e3108f3b8ef6706ea1d701290222 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 19 Nov 2022 18:32:11 +0330 Subject: [PATCH] feat(storage-engine): storage meta and auto_increment id --- .../core/storage-engine/src/storage-engine.ts | 47 +++++++++++++++++-- packages/core/storage-engine/src/type.ts | 10 ++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/packages/core/storage-engine/src/storage-engine.ts b/packages/core/storage-engine/src/storage-engine.ts index 2db141cf5..8bb38396b 100644 --- a/packages/core/storage-engine/src/storage-engine.ts +++ b/packages/core/storage-engine/src/storage-engine.ts @@ -71,6 +71,8 @@ alwatrRegisteredList.push({ * ``` */ export class AlwatrStorageEngine { + static readonly formatVersion = 3; + /** * Storage name like database table name. */ @@ -117,10 +119,29 @@ export class AlwatrStorageEngine { return this.keys.length; } + /** + * Get all data. + */ get _data(): typeof this._storage.data { return this._storage.data; } + /** + * Get storage meta. + */ + get meta(): typeof this._storage.meta { + return this._storage.meta; + } + + /** + * Get next auto increment id for numerical document id. + */ + get nextAutoIncrementId(): string { + const id = this._storage.meta ? +this._storage.meta.lastUpdatedId : 0; + if (isNaN(id)) throw new Error('doc_id_is_nan'); + return (id + 1).toString(); + } + protected get _newStorage(): DataStorage { return {ok: true, data: {}}; } @@ -226,19 +247,37 @@ export class AlwatrStorageEngine { set(documentObject: DocumentType, fastInstance?: boolean): DocumentType { this._logger.logMethodArgs('set', documentObject._id); - const oldData = this._storage.data[documentObject._id]; - if (oldData == null) this._keys = null; // Clear cached keys - if (fastInstance !== true) { documentObject = JSON.parse(JSON.stringify(documentObject)); } + if (documentObject._id === 'auto_increment') { + documentObject._id = this.nextAutoIncrementId; + } + + const oldData = this._storage.data[documentObject._id]; + // update meta documentObject._updatedAt = Date.now(); documentObject._createdAt = oldData?._createdAt ?? documentObject._updatedAt; documentObject._createdBy = oldData?._createdBy ?? documentObject._updatedBy; documentObject._rev = (oldData?._rev ?? 0) + 1; + this._storage.meta ??= { + formatVersion: AlwatrStorageEngine.formatVersion, + reversion: 0, + lastCreatedId: documentObject._id, + lastUpdatedId: '', + lastUpdatedAt: 0, + }; + this._storage.meta.reversion++; + this._storage.meta.lastUpdatedId = documentObject._id; + this._storage.meta.lastUpdatedAt = documentObject._updatedAt; + if (oldData == null) { + this._keys = null; // Clear cached keys + this._storage.meta.lastCreatedId = documentObject._id; + } + this._storage.data[documentObject._id] = documentObject; this.save(); @@ -266,6 +305,8 @@ export class AlwatrStorageEngine { // Clear cached keys this._keys = null; + if (this._storage.meta) this._storage.meta.reversion++; + this.save(); return true; } diff --git a/packages/core/storage-engine/src/type.ts b/packages/core/storage-engine/src/type.ts index a56b5e55c..b1ea745bd 100644 --- a/packages/core/storage-engine/src/type.ts +++ b/packages/core/storage-engine/src/type.ts @@ -2,6 +2,9 @@ export type JSON = Record; export interface DocumentObject { [key: string]: unknown; + /** + * Document uniq ID or `auto_increment`. + */ _id: string; _rev?: number; _createdAt?: number; @@ -12,6 +15,13 @@ export interface DocumentObject { export type DataStorage = { ok: true; + meta?: { + formatVersion: number; + reversion: number; + lastUpdatedAt: number; + lastUpdatedId: string; + lastCreatedId: string; + }; data: Record; }