-
Notifications
You must be signed in to change notification settings - Fork 38
feat: Add libSQL adapter (proposal) #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use version |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
| 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(); |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); |
| 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" | ||
| } | ||
| } |
| 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'; |
| 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'; |
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some users have correctly pointed out that Thats also about |
||
|
|
||
| /** | ||
| * @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] }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "module": "CommonJS", | ||
| "outDir": "./dist/cjs" | ||
| }, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "module": "es6", | ||
| "outDir": "./dist/esm" | ||
| }, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "include": [ | ||
| "src" | ||
| ] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
8? It should be9There was a problem hiding this comment.
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