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

Add db #2139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add db #2139

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
6 changes: 4 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from channel import channel_factory
from common import const
from config import load_config
from lib.db.frame import DB
from plugins import *
import threading

Expand Down Expand Up @@ -43,12 +44,13 @@ def start_channel(channel_name: str):
def run():
try:
# load config
load_config()
config = load_config()
# ctrl + c
sigterm_handler_wrap(signal.SIGINT)
# kill signal
sigterm_handler_wrap(signal.SIGTERM)


DB.db_connect(config)
# create channel
channel_name = conf().get("channel_type", "wx")

Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
"Minimax_api_key": "",
"Minimax_group_id": "",
"Minimax_base_url": "",
"SQLALCHEMY_DATABASE_URI": "",
}


Expand Down Expand Up @@ -295,6 +296,7 @@ def load_config():
logger.info("[INIT] load config: {}".format(drag_sensitive(config)))

config.load_user_datas()
return config


def get_root():
Expand Down
53 changes: 53 additions & 0 deletions lib/db/frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
from sqlalchemy import create_engine, Column, Integer, String, Sequence
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

class DB:
session = None
"""
A method to establish a connection to the database, create necessary tables, and initialize a session.
"""
@classmethod
def db_connect(cls, config):
# Create an engine
db_config = config.get("SQLALCHEMY_DATABASE_URI", "sqlite:///wechat_bot_dev.db")
engine = create_engine(db_config)

# Define the base
Base = declarative_base()

# Create the tables
Base.metadata.create_all(engine)

# Create a session
Session = sessionmaker(bind=engine)
cls.session = Session()

@classmethod
def get_session(cls):
return cls.session


# Create a new user
# new_user = User(name='John', fullname='John Doe', nickname='johnny')
# session.add(new_user)
# session.commit()

# # Query all users
# users = session.query(User).all()
# for user in users:
# print(user.name, user.fullname, user.nickname)

# # Update a user
# user = session.query(User).filter_by(name='John').first()
# user.nickname = 'johnny_d'
# session.commit()

# # Delete a user
# user_to_delete = session.query(User).filter_by(name='John').first()
# session.delete(user_to_delete)
# session.commit()

# # Close the session
# session.close()