Storage Adapters
Just as any Telegram client, MTKruto requires persistent storage to work
properly. Storage adapters define an interface to persist data. There are
multiple built-in storage adapters, and others can always be implemented by
extending and implementing the interface
Storage
.
This page lists the built-in storage adapters and shows how they can be used.
Memory
This storage adapter stores data in the memory. It is ideal for bots.
To use it, just pass null
to the client constructor’s storage parameter:
const client = new Client({
storage: null,
/* ... */
});
This storage adapter is supported on all runtimes.
sessionStorage
This storage adapter is not recommended for general use. It stores data using sessionStorage.
To use it, import the StorageSessionStorage
class, construct it with a valid
prefix, and pass it to the client’s constructor:
const client = new Client({
storage: new StorageSessionStorage("myclient"),
/* ... */
});
This storage adapter is only supported inside browsers and on Deno.
localStorage
This storage adapter persista data into localStorage.
To use it, import the StorageLocalStorage
class, construct it with a valid
prefix, and pass it to the client’s constructor:
const client = new Client({
storage: new StorageLocalStorage("myclient"),
/* ... */
});
This storage adapter is natively supported inside browsers and on Deno. It uses
node-localstorage
in Node.js.
Deno KV
This storage adapter persists data inside a Deno KV database.
To use it, import the StorageDenoKV
class from the path specified below,
construct it (optionally with a path provided), and pass it to the client’s
constructor:
import { StorageDenoKV } from "@mtkruto/mtkruto/storage/1_storage_deno_kv.ts";
const client = new Client({
storage: new StorageDenoKV(),
/* ... */
});
This module is natively supported on Deno, and unsupported for browsers. To use it on Node.js, you first need to install the following required packages:
$ pnpm add @deno/kv @mtkruto/storage-denokv
$ yarn add @deno/kv @mtkruto/storage-denokv
$ npm install @deno/kv @mtkruto/storage-denokv
Then, import the StorageDenoKV
class from @mtkruto/storage-denokv
:
const { StorageDenoKV } = require("@mtkruto/storage-denokv");
const client = new Client({
storage: new StorageDenoKV(),
/* ... */
});
import { StorageDenoKV } from "@mtkruto/storage-denokv";
const client = new Client({
storage: new StorageDenoKV(),
/* ... */
});
IndexedDB
This storage adapter uses the browser API IndexedDB to persist data. It also supports file storage by default, meaning that file chunks can automatically be persisted as soon as downloaded.
To use it, import the StorageIndexedDB
class, construct it with a database
name to use, and pass it to the client’s constructor:
import { StorageIndexedDB } from "@mtkruto/browser";
const client = new Client({
storage: new StorageIndexedDB("client"),
/\* ... \*/
});
import { StorageIndexedDB } from "https://esm.sh/jsr/@mtkruto/[email protected]";
const client = new Client({
storage: new StorageIndexedDB("client"),
/\* ... \*/
});
import { StorageIndexedDB } from "@mtkruto/mtkruto/mod.ts";
const client = new Client({
storage: new StorageIndexedDB("client"),
/\* ... \*/
});
This storage adapter is only supported in browsers.