Skip to content

Commit

Permalink
fix(storage): _last ket
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Sep 19, 2022
1 parent dd82708 commit a1c452a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions demo/storage/big-data-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const db = new AlwatrStorage<User>({
name: 'junk-data',
path: 'db',
saveBeautiful: false,
saveDebounce: 10_000,
debug: false,
});

Expand All @@ -30,8 +29,9 @@ for (let i = 0; i < 100_000; i++) {
}

console.time('get item');
db.get('_latest');
const item = db.get('_last');
console.timeEnd('get item');
console.dir(item);

db.forceSave();
console.log('done');
21 changes: 13 additions & 8 deletions demo/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ interface User extends DocumentObject {
token?: string;
}

const db = new AlwatrStorage<User>({name: 'user-list', path: 'db'});
const db = new AlwatrStorage<User>({
name: 'user-list',
path: 'db',
saveBeautiful: true,
debug: true,
});

console.log('db loaded and ready to access.');

Expand All @@ -27,13 +32,13 @@ if (ali == null) {
} else {
console.log('ali found: %o', ali);
/**
* {
* _id: 'alimd',
* fname: 'Ali',
* lname: 'MM',
* email: 'i@ali.md',
* }
*/
* {
* _id: 'alimd',
* fname: 'Ali',
* lname: 'MM',
* email: 'i@ali.md',
* }
*/

ali.token = Math.random().toString(36).substring(2, 15);
}
Expand Down
12 changes: 6 additions & 6 deletions packages/core/storage/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
/**
* All document ids in array.
*
* Contain `_latest`!
* Contain `_last`!
*/
get keys(): Array<string> {
if (this._keys === null) {
Expand All @@ -73,7 +73,7 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
}

/**
* Size of the storage (count `_latest`).
* Size of the storage (count `_last`).
*/
get length(): number {
return this.keys.length;
Expand All @@ -86,7 +86,7 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {

this.name = config.name;
this.storagePath = resolve(`${config.path ?? './db'}/${config.name}.json`);
this.saveDebounce = config.saveDebounce ?? 100;
this.saveDebounce = config.saveDebounce ?? 1000;
this.saveBeautiful = config.saveBeautiful || false;

exitHook(this.forceSave);
Expand Down Expand Up @@ -129,10 +129,10 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
* ```
*/
get(documentId: string, fastInstance?: boolean): DocumentType | null {
// this._logger.logMethodArgs('get', documentId);
this._logger.logMethodArgs('get', documentId);

const documentObject = this._storage[documentId];
if (typeof documentObject === 'string') { // for example _latest
if (typeof documentObject === 'string') { // for example _last
return this.get(documentObject);
} else if (documentObject == null) {
return null;
Expand Down Expand Up @@ -221,7 +221,7 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
async forAll(callbackfn: (documentObject: DocumentType) => void | false | Promise<void | false>): Promise<void> {
const keys = this.keys;
for (const documentId of keys) {
if (documentId === '_latest') continue; // prevent to duplicate latest key.
if (documentId === '_last') continue; // prevent to duplicate latest key.
const documentObject = this.get(documentId);
if (documentObject != null) {
const retVal = await callbackfn(documentObject);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/storage/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type AlwatrStorageConfig = {
/**
* Save debounce timeout for minimal disk iops usage.
*
* @default 100
* @default 1000
*/
saveDebounce?: number;

Expand All @@ -44,7 +44,7 @@ export type AlwatrStorageConfig = {
/**
* Debug output logs
*
* @default undefined Auto detect base in NODE_ENV
* @default undefined Auto detect base on `NODE_ENV`
*/
debug?: boolean;
};
Expand Down

0 comments on commit a1c452a

Please sign in to comment.