storage/src/main.ts

64 lines
1.3 KiB
TypeScript

import {
KeyValueStorage,
MemoryStorage,
RefScopedStorage,
ScopedStorage,
} from "./basic.ts";
import { LocalStorage } from "./local.ts";
import { RestRemoteStorage } from "./remote.ts";
/**
* Base type for storage usage
*/
export type TKStorage = KeyValueStorage;
/**
* Exposed classes
*/
export {
LocalStorage,
MemoryStorage,
RefScopedStorage,
RestRemoteStorage,
ScopedStorage,
};
/**
* Get the best "local" storage available
*/
export function getLocalStorage(appname: string): TKStorage {
try {
return new ScopedStorage(new LocalStorage(), appname);
} catch {
console.warn(
"No persistent storage available, using in-memory volatile storage",
);
return new MemoryStorage();
}
}
/**
* Get a remote storage, based on an URI
*
* If *shared* is set to true, a namespace will be used to avoid collisions, using getLocalStorage to persist it
*/
export function getRemoteStorage(
appname: string,
uri: string,
options = { shared: false },
): TKStorage {
let storage: TKStorage = new RestRemoteStorage(appname, uri);
storage = new ScopedStorage(storage, appname);
if (options.shared) {
storage = new RefScopedStorage(getLocalStorage(appname), storage);
}
return storage;
}
/**
* Get a in-memory volatile storage
*/
export function getMemoryStorage(): TKStorage {
return new MemoryStorage();
}