From e3e151692e6833b5eac4607edf59cff1b5062516 Mon Sep 17 00:00:00 2001 From: Nikolay Date: Mon, 8 Jul 2024 16:32:01 +0700 Subject: [PATCH] feature: added config pg schema chore: bump version to 2.0.0-beta.8 --- defaults.ts | 2 +- kysely.ts | 8 ++++++++ package-lock.json | 18 +++++++++--------- package.json | 2 +- pg.ts | 5 +++++ 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/defaults.ts b/defaults.ts index ab4aac9..f8baf7b 100644 --- a/defaults.ts +++ b/defaults.ts @@ -1 +1 @@ -export const defaults = { table: "telegraf-sessions" }; +export const defaults = { table: "telegraf-sessions", schema: "public" }; diff --git a/kysely.ts b/kysely.ts index d7ae545..30ea64f 100644 --- a/kysely.ts +++ b/kysely.ts @@ -23,6 +23,8 @@ interface NewClientOpts { config: KyselyConfig; /** Table name to use for sessions. Defaults to "telegraf-sessions". */ table?: string; + /** Schema name to use for sessions. Defaults to "public". */ + schema?: string; /** Called on fatal connection or setup errors */ onInitError?: (err: unknown) => void; } @@ -31,10 +33,12 @@ interface NewClientOpts { export const KyselyStore = (opts: NewClientOpts): SessionStore => { // this assertion is a hack to make the Database type work const table = (opts.table ?? defaults.table) as "telegraf-sessions"; + const schema = (opts.schema ?? defaults.schema) as "public"; const client: Kysely = new Kysely(opts.config); const create = client.schema + .withSchema(schema) .createTable(table) .ifNotExists() .addColumn("key", "varchar(32)", col => col.primaryKey().notNull()) @@ -50,6 +54,7 @@ export const KyselyStore = (opts: NewClientOpts): SessionStore const value = ( await client // + .withSchema(schema) .selectFrom(table) .select("session") .where("key", "=", key) @@ -66,11 +71,13 @@ export const KyselyStore = (opts: NewClientOpts): SessionStore const res = await (opts.config.dialect instanceof MysqlDialect ? client + .withSchema(schema) .insertInto(table) .values({ key, session }) // MySQL has ON DUPLICATE KEY UPDATE .onDuplicateKeyUpdate({ session }) : client + .withSchema(schema) .insertInto(table) .values({ key, session }) // Postgres and SQLITE have ON CONFLICT DO UPDATE SET @@ -81,6 +88,7 @@ export const KyselyStore = (opts: NewClientOpts): SessionStore await create; await client // + .withSchema(schema) .deleteFrom(table) .where("key", "=", key) .executeTakeFirst(); diff --git a/package-lock.json b/package-lock.json index 3991a88..1a355c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@telegraf/session", - "version": "2.0.0-beta.6", + "version": "2.0.0-beta.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@telegraf/session", - "version": "2.0.0-beta.6", + "version": "2.0.0-beta.8", "license": "MIT", "devDependencies": { "@types/better-sqlite3": "^7.6.9", @@ -22,14 +22,14 @@ "typescript": "^5.3.3" }, "peerDependencies": { - "@types/better-sqlite3": "^7.6.3", - "@types/pg": "^8.6.6", + "@types/better-sqlite3": "^7.6.9", + "@types/pg": "^8.11.0", "better-sqlite3": "^9.3.0", - "kysely": "0.23.4 <1", - "mongodb": "^5.0.1", - "mysql2": "^3.1.2", - "pg": "^8.9.0", - "redis": "^4.6.4", + "kysely": "0.27.2 <1", + "mongodb": "^6.3.0", + "mysql2": "^3.9.0", + "pg": "^8.11.3", + "redis": "^4.6.12", "telegraf": ">=4.12.0" }, "peerDependenciesMeta": { diff --git a/package.json b/package.json index aecf6e9..1af532f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@telegraf/session", - "version": "2.0.0-beta.7", + "version": "2.0.0-beta.8", "description": "Session store adapters for Telegraf", "main": "./memory.js", "homepage": "https://github.com/telegraf/session", diff --git a/pg.ts b/pg.ts index 6da5e7f..7f55de3 100644 --- a/pg.ts +++ b/pg.ts @@ -19,6 +19,8 @@ interface NewPoolOpts { config?: Omit; /** Table name to use for sessions. Defaults to "telegraf-sessions". */ table?: string; + /** Schema name to use for sessions. Defaults to "public". */ + schema?: string; /** Called on fatal connection or setup errors */ onInitError?: (err: unknown) => void; } @@ -28,6 +30,8 @@ interface ExistingPoolOpts { pool: Pool; /** Table name to use for sessions. Defaults to "telegraf-sessions". */ table?: string; + /** Schema name to use for sessions. Defaults to "public". */ + schema?: string; /** Called on fatal connection or setup errors */ onInitError?: (err: unknown) => void; } @@ -53,6 +57,7 @@ export function Postgres(opts: NewPoolOpts | ExistingPoolOpts) { }), }, table: opts.table, + schema: opts.schema, onInitError: opts.onInitError, }); }