kvstore

package
v3.0.0-alpha.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2024 License: MIT Imports: 8 Imported by: 0

README

KVStore Plugin

This plugin provides a simple key/value store for your Wails applications.

Installation

Add the plugin to the Plugins option in the Applications options:

package main

import (
	"github.com/wailsapp/wails/v3/pkg/application"
	"github.com/wailsapp/wails/v3/plugins/kvstore"
)

func main() {
	kvstorePlugin := kvstore.NewPlugin(&kvstore.Config{
		Filename: "myapp.db",
	})
	app := application.New(application.Options{
		// ...
		Plugins: map[string]application.Plugin{
			"kvstore": kvstorePlugin,
		},
	})

Options
type Config struct {
    Filename string
    AutoSave bool
}
  • Filename - The name of the file to store the key/value pairs in. This file will be created in the application's data directory.
  • AutoSave - If true, the store will be saved to disk after every change. If false, you will need to call Save() to persist the changes.

Usage

Go

You can call the methods exported by the plugin directly:

    // Set a key
    err := kvstore.Set("url", "https://www.google.com")
    if err != nil {
        // handle error
    }
	// Get a key
    url := kvstore.Get("url").(string)
	
	// Delete a key
    err = kvstore.Delete("url")
    if err != nil {
        // handle error
    }
    
	// If you have not enables AutoSave, you will need to call Save() to persist the changes
    err = kvstore.Save()
    if err != nil {
        // handle error
    }
Javascript

You can call the methods from the frontend using the Plugin method:

    wails.Plugin("kvstore","Set", "url", "https://www.google.com")
    wails.Plugin("kvstore","Get", "url").then((url) => {
    
    })
    wails.Plugin("kvstore","Delete", "url").then((url) => {
    
    })
    
    // or
    wails.Plugin("browser","OpenFile","/path/to/file")

Support

If you find a bug in this plugin, please raise a ticket on the Wails Issue Tracker.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Filename string
	AutoSave bool
}

type KeyValueStore

type KeyValueStore struct {
	// contains filtered or unexported fields
}

func NewPlugin

func NewPlugin(config *Config) *KeyValueStore

func (*KeyValueStore) Assets

func (kvs *KeyValueStore) Assets() fs.FS

func (*KeyValueStore) CallableByJS

func (kvs *KeyValueStore) CallableByJS() []string

func (*KeyValueStore) Delete

func (kvs *KeyValueStore) Delete(key string) error

Delete deletes the key from the store. If AutoSave is true, the store is saved to disk.

func (*KeyValueStore) Get

func (kvs *KeyValueStore) Get(key string) any

Get returns the value for the given key. If key is empty, the entire store is returned.

func (*KeyValueStore) Init

func (kvs *KeyValueStore) Init(api application.PluginAPI) error

Init is called when the plugin is loaded. It is passed the application.App instance. This is where you should do any setup.

func (*KeyValueStore) Name

func (kvs *KeyValueStore) Name() string

Name returns the name of the plugin.

func (*KeyValueStore) Save

func (kvs *KeyValueStore) Save() error

Save saves the store to disk

func (*KeyValueStore) Set

func (kvs *KeyValueStore) Set(key string, value any) error

Set sets the value for the given key. If AutoSave is true, the store is saved to disk.

func (*KeyValueStore) Shutdown

func (kvs *KeyValueStore) Shutdown() error

Shutdown will save the store to disk if there are unsaved changes.

type Plugin

type Plugin struct{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL