From 2c3dc1b69688e47d8ee728b5beaef6bd1811b42a Mon Sep 17 00:00:00 2001 From: Abduvokhid Date: Fri, 25 Nov 2022 23:16:40 +0500 Subject: [PATCH 1/2] Added SequelizeAdapter --- packages/sequelize/README.md | 6 ++++++ packages/sequelize/src/ISession.ts | 5 +++++ packages/sequelize/src/index.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 packages/sequelize/README.md create mode 100644 packages/sequelize/src/ISession.ts create mode 100644 packages/sequelize/src/index.ts diff --git a/packages/sequelize/README.md b/packages/sequelize/README.md new file mode 100644 index 000000000..d517f24af --- /dev/null +++ b/packages/sequelize/README.md @@ -0,0 +1,6 @@ +### Quick info + +1. Recommended to use combination of Sequelize and Sequelize-Typescript packages +2. Session model needs to implement all properties from ISession interface + +Code, docs and examples will be updates ASAP diff --git a/packages/sequelize/src/ISession.ts b/packages/sequelize/src/ISession.ts new file mode 100644 index 000000000..93b827a45 --- /dev/null +++ b/packages/sequelize/src/ISession.ts @@ -0,0 +1,5 @@ +export interface ISession { + id: number; + key: string; + value: string; +} diff --git a/packages/sequelize/src/index.ts b/packages/sequelize/src/index.ts new file mode 100644 index 000000000..fc6c11d5a --- /dev/null +++ b/packages/sequelize/src/index.ts @@ -0,0 +1,28 @@ +import { StorageAdapter } from 'grammy' +import { Repository } from 'sequelize-typescript' + +export class SequelizeAdapter implements StorageAdapter { + private repository: Repository + + constructor (options: { repository: Repository }) { + this.repository = options.repository + } + + async read (key: string) { + const session = await this.repository.findOne({ where: { key: key } }) + if (session === null || session === undefined) { + return undefined + } + return JSON.parse(session.value) as unknown as T + } + + async write (key: string, value: T) { + const jsonValue = JSON.stringify(value) + await this.repository.upsert({key: key, value: jsonValue}) + } + + async delete (key: string) { + await this.repository.destroy({ where: { key: key } }) + } + +} From 9150667acfc40fb5e9a79706881c72b1f64b5735 Mon Sep 17 00:00:00 2001 From: Abduvokhid Date: Fri, 25 Nov 2022 23:25:46 +0500 Subject: [PATCH 2/2] Added SequelizeAdapter --- packages/sequelize/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/sequelize/README.md b/packages/sequelize/README.md index d517f24af..edc03f059 100644 --- a/packages/sequelize/README.md +++ b/packages/sequelize/README.md @@ -2,5 +2,6 @@ 1. Recommended to use combination of Sequelize and Sequelize-Typescript packages 2. Session model needs to implement all properties from ISession interface +3. Mark "key" property of Session model as unique (it's required for Sequelize "upsert" method to work) Code, docs and examples will be updates ASAP