-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 Plugin Type: KVStorage #1208
Comments
Very nice feature design. Let's add some more details.
key=value
Importing the concept of groups is actually
In this way, perhaps we can give the plugin the ability to page through the data under a group. Of course I would consider this an enhancement as well. Maybe for cases where the plugin has a scheduled task to scan all the existing data for processing. |
@LinkinStars
type PluginKVStorage struct {
db *xorm.Engine
session *xorm.Session
pluginSlugName string
}
// ...
func (kv * PluginKVStorage) Tx(ctx context.Context, fn func(ctx context.Context, kv KVOperator) error) error {
if kv.session != nil {
return fn(ctx, *kv)
}
session := kv.db.NewSession()
defer session.Close()
err := fn(ctx, KVOperator{
session: session,
db: kv.db,
pluginSlugName: kv.pluginSlugName,
})
if err != nil {
session.Rollback()
return err
}
return session.Commit()
}
func (kv *PluginKVStorage) Get(ctx context.Context, key, group string) (string, error) {
var data entity. KVStorage
if key == "" && group == "" {
return "", fmt.Errorf("either key or group must be provided")
}
// build query
var query *xorm.Session
if kv.session != nil {
query = kv.session
} else {
query = kv.db.NewSession()
defer query.Close()
}
// ...
} Usage example: err := kv.Tx(ctx, func(ctx context.Context, txKv KVOperator) error {
if err := txKv.Set(ctx, "group1", "key1", "value1"); err != nil {
return err
}
return txKv.Set(ctx, "group1", "key2", "value2")
})
Please feel free to review and provide more feedback! |
By the way, I have a question. After adding new plugin types, how should we handle cases where plugins use plugin types that are only supported after certain versions? |
BTW, you need to call session := engine.NewSession()
defer session.Close()
// call begin
if err := session.Begin(); err != nil {
return nil, err
}
// TODO
if err := session.Commit(); err != nil {
return result, err
}
Don't worry, we only support after a certain version. |
@LinkinStars OK~ Thanks for your feedback, I'll make these changes and submit a PR. |
Is your feature request related to a problem? Please describe
During plugin development, we frequently need persistent data storage capabilities. The current plugin system only supports reading user configurations, which severely limits plugin functionality. For example, when developing Passkey login features, plugins need to store client public key information, but the existing architecture cannot support this requirement.
Describe the solution you'd like
I propose adding a new plugin type called KVStorage to provide key-value storage capabilities for plugins. Here is the specific design:
This design ensures data isolation through the PluginSlugName field, where each plugin can only access its own data. It also provides transaction support to ensure atomic data operations.
Describe alternatives you've considered
Thanks to @LinkinStars for the help. Please provide any feedback - I'm happy to make adjustments.
The text was updated successfully, but these errors were encountered: