unikvstore
v2.0.15
Published
A utility library for managing storages through a simple key-value pair storage interface, including localStorage, google drive, dropbox, etc.
Downloads
9
Maintainers
Readme
unikvstore
A utility library for managing storages through a simple key-value pair storage interface, including localStorage, google drive, dropbox, etc.
Install
npm install unikvstore
Usage
Use MemoryStorage:
import { MemoryStorage, IStorage } from "unikvstore";
async function main() {
const storage: IStorage = new MemoryStorage();
await storage.set("key", "value");
console.log("get: ", await storage.get("key"));
console.log("contains: ", await storage.contains("key"));
}
main();
Use DropboxStorage:
import { DropboxStorage, IStorage } from "unikvstore";
async function main() {
const accessToken = process.env.DROPBOX_ACCESS_TOKEN;
console.log("dropbox accessToken: ", accessToken);
const storage: IStorage = new DropboxStorage(accessToken);
await storage.set("key", "value");
console.log("get: ", await storage.get("key"));
console.log("contains: ", await storage.contains("key"));
await storage.clear();
console.log("contains after clearing: ", await storage.contains("key"));
}
main();
Use GoogleDriveStorage:
import { GoogleDriveStorage, IStorage } from "unikvstore";
async function main() {
const accessToken = process.env.GOOGLE_DRIVE_ACCESS_TOKEN;
console.log("google drive accessToken: ", accessToken);
const storage: IStorage = new GoogleDriveStorage(accessToken);
await storage.set("key", "value");
console.log("get: ", await storage.get("key"));
console.log("contains: ", await storage.contains("key"));
await storage.clear();
console.log("contains after clearing: ", await storage.contains("key"));
}
main();
Interface
For more details about interface to see this.
Core interface:
interface IStorage {
keys(): Promise<string[]>;
// Returns whether the key exists.
contains(key: string | Promise<string>): Promise<boolean>;
// Returns the value given the key.
// If key not found
// a. not specifying defaultValue, throw `KeyNotFound` error.
// b. otherwise returns defaultValue.
get(
key: string | Promise<string>,
defaultValue?: string | Promise<string>
): Promise<string>;
// Sets the value for given key.
set(
key: string | Promise<string>,
value: string | Promise<string>
): Promise<void>;
delete(key: string | Promise<string>): Promise<void>;
clear(): Promise<void>;
}