NyaaCat / NyaaCore

Code infrastructure for all NyaaCat plugins
MIT License
22 stars 12 forks source link

数据库中间件 #26

Closed phoenixlzx closed 5 years ago

phoenixlzx commented 6 years ago

NC 作为中间件提供一个抽象的数据库操作接口,对多种数据库提供支持。

插件在调用时通过读取插件自身的数据库类型和配置建立一个数据库连接实例,使用 NC 的抽象方法对数据库连接实例进行 CRUD 操作。

例如插件的配置中:

database:
  provider: redis
  connection: username:password@host/db_name

其他需要注意的?

Librazy commented 6 years ago

Available at maven-repo-dev and 4.2 releases

Librazy commented 6 years ago

Relational

Mysql

Mysql database from DriverManager. Jdbc driver com.mysql.jdbc.Driver is bundled with spigot by default.

database:
  provider: mysql
  connection:
    autoscan: true
    url: jdbc:MySQL://localhost:3306/heh
    username: username
    password: password
# 'package' and 'jdbc' are optional
    package: cat.nyaa.HamsterEcoHelper.utils.database.tables 
    jdbc: com.mysql.jdbc.Driver

or select table classes manually

database:
  provider: mysql
  connection:
    autoscan: false
    url: jdbc:MySQL://localhost:3306/heh
    username: root
    password: root
    tables:
    - cat.nyaa.HamsterEcoHelper.utils.database.tables.TempStorageRepo
    - cat.nyaa.HamsterEcoHelper.utils.database.tables.MarketItem
    - cat.nyaa.HamsterEcoHelper.utils.database.tables.ItemLog
    - cat.nyaa.HamsterEcoHelper.utils.database.tables.signshop.SignShop
    - cat.nyaa.HamsterEcoHelper.utils.database.tables.signshop.Sign
    - cat.nyaa.HamsterEcoHelper.utils.database.tables.signshop.ShopStorageLocation
    - cat.nyaa.HamsterEcoHelper.utils.database.tables.signshop.LottoStorageLocation
# 'jdbc' is optional
    jdbc: com.mysql.jdbc.Driver

Sqlite

Sqlite database from DriverManager. Always requires org.sqlite.JDBC which is also came with spigot. Also support autoscan: false with tables section.

database:
    provider: sqlite
    connection:
        autoscan: true
        file: HamsterEcoHelper.db
# 'package' is optional
        package: cat.nyaa.HamsterEcoHelper.utils.database.tables

Hibernate

https://github.com/Librazy/HibernateProvider Based on Hibernate ORM to support various SQL database.

database:
  provider: hibernate
  connection:
    autoscan: true
    url: jdbc:MySQL://localhost:3306/heh
    username: root
    password: root
# 'jdbc' is optional
    jdbc: com.mysql.jdbc.Driver
# 'dialect' coming soooon
    dialect: org.hibernate.dialect.MySQL57Dialect

Key-Value

Map

Java collection Map. No configuration is available. Not persisted and will be cleared when restart/reload.

database:
    provider: map
    connection: []

Redis

https://github.com/NyaaCat/RedisProvider Based on lettuce, a powerful redis client for Java.

database:
  provider: redis
  connection:
    url: redis://localhost/
# or use 'host' 'port' 'database' and 'database' instead of 'url'
# 'key' and 'value' should match the types where requires it. Default to String
    key: java.util.UUID
    value: cat.nyaa.nyaautils.lootprotect.LootProtectListener$VanillaStrategy
phoenixlzx commented 6 years ago

有一个额外的需求支持:

per-player configuration,在数据库中单独创建一个表,使用 k-v 方式存储。 例如 NyaaUtils 的 lootprotect 设置,key 是 ncppc:nyaautils:lp:<playeruuid> value 是 reject 类似。

这些配置不需要分插件,也不需要强制持久化,共用一张表或直接用单独的前缀可以省去各单独插件对于这些玩家单独配置的存储。可以看作是 NC 的 kv 扩充。

Librazy commented 6 years ago

hmm目前redis的设计是要求每一处使用新开一个redis数据库……还没想好前缀这个要怎么做(

lwl12 commented 5 years ago

redis 看起来还没 merge 进 NC6 的样子?

Librazy commented 5 years ago

独立的,在 RedisProvider 这个 repo