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

Added SequelizeAdapter #132

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions packages/sequelize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Quick info

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
5 changes: 5 additions & 0 deletions packages/sequelize/src/ISession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ISession {
id: number;
key: string;
value: string;
}
28 changes: 28 additions & 0 deletions packages/sequelize/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { StorageAdapter } from 'grammy'
import { Repository } from 'sequelize-typescript'

export class SequelizeAdapter<T> implements StorageAdapter<T> {
private repository: Repository<any>

constructor (options: { repository: Repository<any> }) {
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 } })
}

}