-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from AliMD/feat/storage-api-nanoservice
Storage Nanoservice API
- Loading branch information
Showing
20 changed files
with
480 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {createLogger} from '@alwatr/logger'; | ||
|
||
import {AlwatrStorage} from './storage.js'; | ||
|
||
import type {AlwatrStorageConfig, AlwatrStorageProviderConfig, DocumentObject} from './type'; | ||
|
||
// TODO: auto unload base of last usage time and memory limit. | ||
|
||
/** | ||
* Easy access to many storages with auto garbage collector | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* import {AlwatrStorageProvider} from '@alwatr/storage'; | ||
* const storageList = new AlwatrStorageProvider(); | ||
* // ... | ||
* const user = (await storageList.get('user-list')).get('userId1'); | ||
* ``` | ||
*/ | ||
export class AlwatrStorageProvider { | ||
protected _logger = createLogger('alwatr-storage-provider'); | ||
protected _list: Record<string, AlwatrStorage<DocumentObject>> = {}; | ||
protected _config: AlwatrStorageProviderConfig; | ||
|
||
constructor(config: AlwatrStorageProviderConfig) { | ||
this._logger.logMethodArgs('constructor', config); | ||
this._config = config; | ||
} | ||
|
||
// TODO: update all jsdoc and readme. | ||
async get<DocumentType extends DocumentObject = DocumentObject>( | ||
config: AlwatrStorageConfig, | ||
): Promise<AlwatrStorage<DocumentType>> { | ||
if (!this._list[config.name]) { | ||
this._list[config.name] = new AlwatrStorage<DocumentType>({ | ||
...this._config, | ||
...config, | ||
}); | ||
} | ||
if (this._list[config.name].readyState !== true) { | ||
await this._list[config.name].readyPromise; | ||
console.log('Memory usage: %sMB', Math.round(process.memoryUsage.rss() / 100000) / 10); | ||
} | ||
return this._list[config.name] as AlwatrStorage<DocumentType>; | ||
} | ||
|
||
unload(name: string): void { | ||
if (this._list[name] == null) return; | ||
this._list[name].unload(); | ||
delete this._list[name]; | ||
} | ||
|
||
unloadAll(): void { | ||
// eslint-disable-next-line guard-for-in | ||
for (const name in this._list) { | ||
this._list[name].unload(); | ||
} | ||
this._list = {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.