Skip to content
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

feature: added config pg schema #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 defaults.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const defaults = { table: "telegraf-sessions" };
export const defaults = { table: "telegraf-sessions", schema: "public" };
8 changes: 8 additions & 0 deletions kysely.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -31,10 +33,12 @@ interface NewClientOpts {
export const KyselyStore = <Session>(opts: NewClientOpts): SessionStore<Session> => {
// 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<Database> = new Kysely(opts.config);

const create = client.schema
.withSchema(schema)
Comment on lines 40 to +41
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't this error when used with anything other than Postgres? Perhaps instead of defaulting to "public", we can call withSchema only if schema is explicitly passed. Then we don't need to worry about Pg/others.

Suggested change
const create = client.schema
.withSchema(schema)
const create = (schema ? client.withSchema(schema) : client.schema)

.createTable(table)
.ifNotExists()
.addColumn("key", "varchar(32)", col => col.primaryKey().notNull())
Expand All @@ -50,6 +54,7 @@ export const KyselyStore = <Session>(opts: NewClientOpts): SessionStore<Session>
const value = (
await client
//
.withSchema(schema)
.selectFrom(table)
.select("session")
.where("key", "=", key)
Expand All @@ -66,11 +71,13 @@ export const KyselyStore = <Session>(opts: NewClientOpts): SessionStore<Session>

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
Expand All @@ -81,6 +88,7 @@ export const KyselyStore = <Session>(opts: NewClientOpts): SessionStore<Session>
await create;

await client //
.withSchema(schema)
.deleteFrom(table)
.where("key", "=", key)
.executeTakeFirst();
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 5 additions & 0 deletions pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ interface NewPoolOpts {
config?: Omit<PoolConfig, "host" | "port" | "database" | "user" | "password">;
/** 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;
}
Expand All @@ -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;
}
Expand All @@ -53,6 +57,7 @@ export function Postgres<Session>(opts: NewPoolOpts | ExistingPoolOpts) {
}),
},
table: opts.table,
schema: opts.schema,
onInitError: opts.onInitError,
});
}