Socket
Book a DemoInstallSign in
Socket

@gramio/storage

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gramio/storage

Storage core for GramIO with in memory built-in

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

@gramio/storage

npm JSR JSR Score

Core of storage adapters.

Read more in documentation.

How to write my own storage adapters

It is very simple to write your adapter!

It is enough to return the object with the required methods and use the methods of the solution you have chosen for the adapter. (for example, ioredis)

import type { Storage } from "@gramio/storage";
import ThirdPartyStorage, { type ThirdPartyStorageOptions } from "some-library";

export interface MyOwnStorageOptions extends ThirdPartyStorageOptions {
    /** add new property to options */
    some?: number;
}

export function myOwnStorage(options: MyOwnStorageOptions = {}): Storage {
    const storage = new ThirdPartyStorage(options);

    return {
        async get(key) {
            const data = await storage.get(key);

            return data ? JSON.parse(data) : undefined;
        },
        async has(key) {
            return storage.has(key);
        },
        async set(key, value) {
            await storage.set(key, JSON.stringify(value));
        },
        async delete(key) {
            return storage.delete(key);
        },
    };
}

[!IMPORTANT] If you want to publish your adapter, we recommend that you follow the convention and name it starting with gramio-storage and add gramio + gramio-storage keywords in your package.json

How to use storage adapters in my own plugin

It is also very easy to work with storage adapters in your plugin!

Everything we need is already in @gramio/storage.

import { Plugin } from "gramio";
import { type Storage, inMemoryStorage } from "@gramio/storage";

export interface MyOwnPluginOptions {
    storage?: Storage;
}

export function myOwnPlugin(options: MyOwnPluginOptions = {}) {
    // use in memory storage by default
    const storage = options.storage ?? inMemoryStorage();

    return new Plugin("gramio-example");
}

[!IMPORTANT] You can scaffold this example by create-gramio-plugin

Built-in In-memory-storage

You can use built-in in-memory storage if you want to store data directly in memory. Be careful, as it is not stateless and data will be lost on reload.

import { type InMemoryStorageMap, inMemoryStorage } from "@gramio/storage";

const map: InMemoryStorageMap = new Map(); // this is optional

const storage = inMemoryStorage(map);

Keywords

gramio

FAQs

Package last updated on 24 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts