Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/release_node.yml
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 8? It should be 9

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "why" is because pnpm lock version was 6 at storages@2.4.2. Now that lockfile is version 9, will update

Ref: https://github.com/pnpm/spec/tree/fd3238639af86c09b7032cc942bab3438b497036/lockfile#relation-of-pnpm-version-to-lockfile-version

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
with:
node-version: 18
registry-url: https://registry.npmjs.org/
- run: npm i -g pnpm
- run: npm i -g pnpm@8
- name: setup npmrc
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
- name: setup pnpm config
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use version 9.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: 18.x
- run: npm i -g pnpm
- run: npm i -g pnpm@8
- name: install dependencies
run: pnpm install
- name: build
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: 18.x
- run: npm i -g pnpm
- run: npm i -g pnpm@8
- run: pnpm install
- run: pnpm build
- name: tests
Expand Down
36 changes: 36 additions & 0 deletions packages/libsql/examples/deno.ts
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why example not using sessions?

Those packages suposed to be used mostly in sessions. Yea, not only sessions, but certainly not for such use in bare code.

Also for deno i'd like to see env usage for bot token, tursor url and token.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const BOT_TOKEN = "";
const TURSO_URL = "";
const TURSO_TOKEN = "";

import { Bot } from "https://lib.deno.dev/x/grammy@1.x/mod.ts";
import { LibSQLAdapter } from "https://deno.land/x/grammy_storages/libsql/src/mod.ts";
import { createClient } from "npm:@libsql/client";

const bot = new Bot(BOT_TOKEN);
const tursoClient = createClient({
url: TURSO_URL,
authToken: TURSO_TOKEN,
});

const libsqlAdapter = await LibSQLAdapter.create<Record<string, any>>({ table: "a-b.c$d", client: tursoClient });

bot.command('up', async ctx => {
const item = ctx.match;
const key = "x-y.z$w";
const value = await libsqlAdapter.read(key) ?? {};
const itemValue = (value[item] ?? 0) + 1;
value[item] = itemValue;
await libsqlAdapter.write(key, value);
await ctx.reply(`Incremented ${item} to ${itemValue}`);
});
bot.command('down', async ctx => {
const item = ctx.match;
const key = "x-y.z$w";
const value = await libsqlAdapter.read(key) ?? {};
const itemValue = (value[item] ?? 0) - 1;
value[item] = itemValue;
await libsqlAdapter.write(key, value);
await ctx.reply(`Decremented ${item} to ${itemValue}`);
});

bot.start();
36 changes: 36 additions & 0 deletions packages/libsql/examples/node.ts
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for nodejs as for deno.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const BOT_TOKEN = "";
const TURSO_URL = "";
const TURSO_TOKEN = "";

import { Bot } from "grammy";
import { LibSQLAdapter } from "@grammyjs/storage-libsql";
import { createClient } from "@libsql/client";

const bot = new Bot(BOT_TOKEN);
const tursoClient = createClient({
url: TURSO_URL,
authToken: TURSO_TOKEN,
});

const libsqlAdapter = await LibSQLAdapter.create<Record<string, any>>({ table: "a-b.c$d", client: tursoClient });

bot.command('up', async ctx => {
const item = ctx.match;
const key = "x-y.z$w";
const value = await libsqlAdapter.read(key) ?? {};
const itemValue = (value[item] ?? 0) + 1;
value[item] = itemValue;
await libsqlAdapter.write(key, value);
await ctx.reply(`Incremented ${item} to ${itemValue}`);
});
bot.command('down', async ctx => {
const item = ctx.match;
const key = "x-y.z$w";
const value = await libsqlAdapter.read(key) ?? {};
const itemValue = (value[item] ?? 0) - 1;
value[item] = itemValue;
await libsqlAdapter.write(key, value);
await ctx.reply(`Decremented ${item} to ${itemValue}`);
});

bot.start();
42 changes: 42 additions & 0 deletions packages/libsql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@grammyjs/storage-libsql",
"version": "2.4.2",
"private": false,
"description": "Turso's libSQL storage for grammY library.",
"main": "./dist/cjs/mod.js",
"module": "./dist/esm/mod.js",
"exports": {
".": {
"import": "./dist/esm/mod.js",
"require": "./dist/cjs/mod.js"
}
},
"files": [
"README.md",
"dist",
"package.json",
"LICENSE"
],
"scripts": {
"test": "echo \"Error: no tests found\"",
"test:deno": "echo \"Error: no tests found\"",
"prebuild": "rimraf dist",
"build": "deno2node tsconfig.cjs.json && deno2node tsconfig.esm.json && pnpm postbuild",
"postbuild": "tsx ../../tools/postBuildFixup.ts --path=dist",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path ."
},
"repository": {
"type": "git",
"url": "git+https://github.com/grammyjs/storages.git"
},
"author": "KnightNiwrem <grammyjs@knightniwrem.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/grammyjs/storages/issues"
},
"homepage": "https://github.com/grammyjs/storages/tree/main/packages/libsql#readme",
"devDependencies": {
"@libsql/client": "^0.14.0",
"grammy": "^1.21.1"
}
}
2 changes: 2 additions & 0 deletions packages/libsql/src/deps.deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { StorageAdapter } from 'https://lib.deno.dev/x/grammy@1.x/mod.ts';
export type { Client } from 'npm:@libsql/client';
2 changes: 2 additions & 0 deletions packages/libsql/src/deps.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { StorageAdapter } from 'grammy';
export type { Client } from '@libsql/client';
70 changes: 70 additions & 0 deletions packages/libsql/src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { StorageAdapter } from './deps.deno.ts';
import type { Client } from './deps.deno.ts';

/**
* Storage adapter for Turso's libSQL.
*/
export class LibSQLAdapter<T> implements StorageAdapter<T> {
protected client: Client;
protected table: string;

private constructor(opts: { client: Client, table: string }) {
this.client = opts.client;
this.table = opts.table;
}

/**
* @param opts options
* @param opts.table - Name of table where data should be stored
* @param opts.client - Turos' libSQL client
* @returns A libSQL storage adapter
*
*/
static async create<T>(opts: { client: Client, table: string }) {
const createTableStatement = `
CREATE TABLE IF NOT EXISTS "${opts.table}" (
key TEXT NOT NULL,
value TEXT
);`;
await opts.client.execute(createTableStatement);

const createIndexStatement = `CREATE UNIQUE INDEX IF NOT EXISTS "IDX_${opts.table}" ON "${opts.table}" (key);`;
await opts.client.execute(createIndexStatement);

return new LibSQLAdapter<T>(opts);
}
Comment on lines +23 to +35
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some users have correctly pointed out that await create() to be called on every application start is bad, because sometimes bots can be used in serverless environments where this call takes a decent amount of time. Instead, I'd like to instruct them in the readme that they should create a table in their database. Then we will accept the table name, client, as an option. And we wouldn't need a static create method

Thats also about psql adapter, which i didnt refactored yet.


/**
* @param key The unique index
* @returns The value for the given key, or undefined if not found
*/
async read(key: string) {
const readStatement = `SELECT value from "${this.table}" WHERE key = ? LIMIT 1;`;
const readResult = await this.client.execute({ sql: readStatement, args: [key] });

const value = readResult.rows[0]?.value as string;
if (!value) {
return undefined;
}

return JSON.parse(value) as T;
}

/**
* @param key The unique index
* @param value The JSON-stringifiable value for the given key
*/
async write(key: string, value: T) {
const jsonValue = JSON.stringify(value);
const writeStatement = `INSERT OR REPLACE INTO "${this.table}" (key, value) values (?, ?);`;
await this.client.execute({ sql: writeStatement, args: [key, jsonValue] });
}

/**
* @param key The unique index
*/
async delete(key: string) {
const deleteStatement = `DELETE FROM "${this.table}" WHERE key = ?;`;
await this.client.execute({ sql: deleteStatement, args: [key] });
}
}
7 changes: 7 additions & 0 deletions packages/libsql/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "./dist/cjs"
},
}
7 changes: 7 additions & 0 deletions packages/libsql/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "es6",
"outDir": "./dist/esm"
},
}
6 changes: 6 additions & 0 deletions packages/libsql/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.base.json",
"include": [
"src"
]
}
Loading