Skip to content

Commit

Permalink
fix(engine5): review
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm authored and alimd committed Dec 20, 2023
1 parent c539712 commit 4acd898
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 35 deletions.
42 changes: 12 additions & 30 deletions packages/engine5/src/alwatr-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,6 @@ export class AlwatrStore {
return exists;
}

/**
* Returns the stats of a store file with the given id from the root store document without loading the store file.
* If the store file does not exist, an error is thrown.
*
* @param id store file id
* @returns file store stat
* @example
* ```typescript
* const stat = alwatrStore.stat('user1/order-list');
* console.log(stat.type); // collection
* ```
*/
// stat(address: StoreFileId): Readonly<StoreFileStat> {
// logger.logMethodArgs?.('stat', address);
// // if (!this.rootDb_.exists(address)) throw new Error('store_file_not_defined', {cause: {address}});
// return this.rootDb_.get(address);
// }

/**
* Defines a document in the store with the given configuration and initial data.
* Document defined immediately and you don't need to await, unless you want to catch writeContext errors.
Expand All @@ -174,7 +156,7 @@ export class AlwatrStore {
defineDocument<TDoc extends Record<string, unknown>>(id: StoreFileId, initialData: TDoc): Promise<void> {
logger.logMethodArgs?.('defineDocument', id);
this.addStoreFile_(id);
return this.writeContext_(id, DocumentReference.newContext_(id, initialData));
return this.writeContext_(id.value, DocumentReference.newContext_(id, initialData));
}

/**
Expand All @@ -194,7 +176,7 @@ export class AlwatrStore {
defineCollection(id: StoreFileId): Promise<void> {
logger.logMethodArgs?.('defineCollection', {id});
this.addStoreFile_(id);
return this.writeContext_(id, CollectionReference.newContext_(id));
return this.writeContext_(id.value, CollectionReference.newContext_(id));
}

/**
Expand Down Expand Up @@ -281,7 +263,7 @@ export class AlwatrStore {
deleteFile(id: StoreFileId): void {
logger.logMethodArgs?.('deleteFile', id);
delete this.memoryContextRecord_[id.toString()]; // direct unload to prevent save
const path = this.storeFilePath_(id);
const path = this.storeFilePath_(id.value);
unlink(path).catch((err) => {
logger.accident?.('deleteFile', 'delete_file_failed', err);
});
Expand All @@ -308,17 +290,17 @@ export class AlwatrStore {
* ```
*/
protected async getContext_(id: StoreFileId): Promise<StoreFileContext> {
logger.logMethodArgs?.('getContext_', id.id);
return this.memoryContextRecord_[id.id] ?? this.readContext_(id);
logger.logMethodArgs?.('getContext_', id);
return this.memoryContextRecord_[id.toString()] ?? this.readContext_(id);
}

protected async readContext_(id: StoreFileId): Promise<StoreFileContext> {
logger.logMethodArgs?.('readContext_', id);
logger.time?.(`readContextTime(${id})`);
const path = this.storeFilePath_(id);
const path = this.storeFilePath_(id.value);
const context = (await readJsonFile(path)) as StoreFileContext;
AlwatrStore.validateStoreFile_(context);
this.memoryContextRecord_[id.id] = context;
this.memoryContextRecord_[id.toString()] = context;
logger.timeEnd?.(`readContextTime(${id})`);
return context;
}
Expand All @@ -334,10 +316,10 @@ export class AlwatrStore {
* await this.writeContext_('user1/profile', {data: {name: 'ali'}});
* ```
*/
protected async writeContext_(id: StoreFileId, context: StoreFileContext, sync = false): Promise<void> {
protected async writeContext_(id: StoreFileAddress, context: StoreFileContext, sync = false): Promise<void> {
logger.logMethodArgs?.('writeContext', id);
logger.time?.(`writeContextTime(${id})`);
id = id.id === AlwatrStore.rootDbId_.id ? AlwatrStore.rootDbId_ : id;
id = id.id === AlwatrStore.rootDbId_.id ? AlwatrStore.rootDbId_.value : id;
const path = this.storeFilePath_(id);
this.memoryContextRecord_[id.toString()] = context;
await writeJsonFile(path, context, WriteFileMode.Rename, sync);
Expand All @@ -364,7 +346,7 @@ export class AlwatrStore {
* console.log(path); // /rootPath/s/use/user1/profile.doc.ajs
* ```
*/
protected storeFilePath_(id: StoreFileId): string {
protected storeFilePath_(id: StoreFileAddress): string {
let regionPath: string = id.region;
if (id.ownerId !== undefined) {
regionPath += `/${id.ownerId.slice(0, 3)}/${id.ownerId}`;
Expand All @@ -377,7 +359,7 @@ export class AlwatrStore {
*/
protected loadRootDb_(): CollectionReference<StoreFileAddress> {
logger.logMethod?.('loadRootDb_');
const path = this.storeFilePath_(AlwatrStore.rootDbId_);
const path = this.storeFilePath_(AlwatrStore.rootDbId_.value);
let context;
if (existsSync(path)) {
context = readJsonFile(path, true) as CollectionContext<StoreFileAddress>;
Expand All @@ -386,7 +368,7 @@ export class AlwatrStore {
else {
logger.banner('Initialize new alwatr-store');
context = CollectionReference.newContext_<StoreFileAddress>(AlwatrStore.rootDbId_);
this.writeContext_(AlwatrStore.rootDbId_, context, true);
this.writeContext_(AlwatrStore.rootDbId_.value, context, true);
}
return new CollectionReference(context, this.writeContext_.bind(this));
}
Expand Down
4 changes: 2 additions & 2 deletions packages/engine5/src/lib/collection-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class CollectionReference<TItem extends Record<string, unknown> = Record<
}
}

protected _logger = createLogger(`coll:${this.context_.meta.id}:${this.context_.meta.ownerId}`.slice(0, 20));
protected _logger = createLogger(`coll:${this.context_.meta.id}/${this.context_.meta.ownerId}`.slice(0, 20));

/**
* Collection reference have methods to get, set, update and save the Alwatr Store Collection.
Expand All @@ -88,7 +88,7 @@ export class CollectionReference<TItem extends Record<string, unknown> = Record<
*/
constructor(
protected context_: CollectionContext<TItem>,
protected updatedCallback_: (id: StoreFileId, context: CollectionContext<TItem>) => void,
protected updatedCallback_: (id: StoreFileAddress, context: CollectionContext<TItem>) => void,
) {
this._logger.logMethodArgs?.('new', context_.meta);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/engine5/src/lib/document-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class DocumentReference<TDoc extends Record<string, unknown> = Record<str
}
}

protected _logger = createLogger(`doc:${this.context_.meta.id}:${this.context_.meta.ownerId}`.slice(0, 20));
protected _logger = createLogger(`doc:${this.context_.meta.id}/${this.context_.meta.ownerId}`.slice(0, 20));

/**
* Create a new document reference.
Expand All @@ -75,7 +75,7 @@ export class DocumentReference<TDoc extends Record<string, unknown> = Record<str
*/
constructor(
protected context_: DocumentContext<TDoc>,
protected updatedCallback_: (id: StoreFileId, context: DocumentContext<TDoc>) => void,
protected updatedCallback_: (id: StoreFileAddress, context: DocumentContext<TDoc>) => void,
) {
this._logger.logMethodArgs?.('new', context_.meta);
}
Expand Down
1 change: 0 additions & 1 deletion packages/node-fs/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './lib/fs.js'
export * from './lib/type.js'

0 comments on commit 4acd898

Please sign in to comment.