Skip to content

Commit

Permalink
Merge pull request #410 from AliMD/feat/storage
Browse files Browse the repository at this point in the history
feat(storage-engine): storage meta and auto_increment id
  • Loading branch information
alimd authored Nov 19, 2022
2 parents 56e137b + 3726947 commit 0c2b247
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
47 changes: 44 additions & 3 deletions packages/core/storage-engine/src/storage-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ alwatrRegisteredList.push({
* ```
*/
export class AlwatrStorageEngine<DocumentType extends DocumentObject> {
static readonly formatVersion = 3;

/**
* Storage name like database table name.
*/
Expand Down Expand Up @@ -117,10 +119,29 @@ export class AlwatrStorageEngine<DocumentType extends DocumentObject> {
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<DocumentType> {
return {ok: true, data: {}};
}
Expand Down Expand Up @@ -226,19 +247,37 @@ export class AlwatrStorageEngine<DocumentType extends DocumentObject> {
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();
Expand Down Expand Up @@ -266,6 +305,8 @@ export class AlwatrStorageEngine<DocumentType extends DocumentObject> {
// Clear cached keys
this._keys = null;

if (this._storage.meta) this._storage.meta.reversion++;

this.save();
return true;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core/storage-engine/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export type JSON = Record<string, unknown>;

export interface DocumentObject {
[key: string]: unknown;
/**
* Document uniq ID or `auto_increment`.
*/
_id: string;
_rev?: number;
_createdAt?: number;
Expand All @@ -12,6 +15,13 @@ export interface DocumentObject {

export type DataStorage<T extends DocumentObject> = {
ok: true;
meta?: {
formatVersion: number;
reversion: number;
lastUpdatedAt: number;
lastUpdatedId: string;
lastCreatedId: string;
};
data: Record<string, T | undefined>;
}

Expand Down

0 comments on commit 0c2b247

Please sign in to comment.