Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage-client, server): touch method #1122

Merged
merged 3 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions core/storage-client/src/storage-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
documentObject: T,
storage: string | undefined = this.config.name,
): Promise<T> {
this._logger.logMethodArgs?.('set', {documentId: documentObject.id});
this._logger.logMethodArgs?.('set', {storage, documentId: documentObject.id});
if (storage == null) throw new Error('storage_not_defined');

const responseJson = await serviceRequest<AlwatrServiceResponseSuccessWithMeta<T>>({
Expand All @@ -201,6 +201,29 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
return responseJson.data;
}

/**
* Touch the storage to make sure storage file exist.
*
* Example:
*
* ```ts
* await userStorage.touch();
* ```
*/
async touch(storage: string | undefined = this.config.name): Promise<void> {
this._logger.logMethodArgs?.('touch', {storage});
if (storage == null) throw new Error('storage_not_defined');

await serviceRequest({
...this.fetchOption,
method: 'GET',
url: this.fetchOption.url + 'touch',
queryParameters: {
storage,
},
});
}

/**
* Delete a document object from the storage.
*
Expand Down Expand Up @@ -236,7 +259,7 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
async getStorage<T extends DocumentType = DocumentType>(
name: string | undefined = this.config.name,
): Promise<AlwatrDocumentStorage<T>> {
this._logger.logMethod?.('getStorage');
this._logger.logMethodArgs?.('getStorage', {name});
if (name == null) throw new Error('storage_not_defined');

const responseJson = (await serviceRequest({
Expand Down Expand Up @@ -269,7 +292,7 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
* ```
*/
async keys(storage: string | undefined = this.config.name): Promise<Array<string>> {
this._logger.logMethod?.('keys');
this._logger.logMethodArgs?.('keys', {storage});
if (storage == null) throw new Error('storage_not_defined');

const responseJson = await serviceRequest({
Expand Down
1 change: 1 addition & 0 deletions core/storage-engine/src/storage-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export class AlwatrStorageEngine<DocumentType extends AlwatrDocumentObject = Alw
this._logger.incident?.('load', 'file_not_found', 'Storage path not found, empty storage loaded', {
path: this.storagePath,
});
this.save();
return this._newStorage;
}

Expand Down
18 changes: 18 additions & 0 deletions services/storage-server/src/route/touch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {config, logger} from '../config.js';
import {nanoServer} from '../lib/nano-server.js';
import {storageProvider} from '../lib/storage-provider.js';

nanoServer.route('GET', '/touch', (connection) => {
logger.logMethod?.('touch');

connection.requireToken(config.nanoServer.accessToken);

const params = connection.requireQueryParams<{storage: string; id: string}>({storage: 'string', id: 'string'});

storageProvider.get({name: params.storage});

return {
ok: true,
data: {},
};
});