Database
Docs: https://docs.api.kunkun.sh/interfaces/ui.IDb
Database API is for extensions that need to store data in a database.
Please avoid using browser storage like localStorage
to avoid data collisions with other extensions and data leaks.
Database API doesn’t require any permissions, but one extension can only access its own data.
This database is based on sqlite thus doesn’t have a key-value style storage. (I have plan to provide key-value style API in the future for convenience.)
The following APIs are now available:
add
delete
search
retrieveAll
retrieveAllByType
deleteAll
update
CRUD are the basic operations provided by the database API. Let’s talk about them one by one.
Add
There are 3 parameters for each data entry:
data
- Any data you want to store.
dataType
- An arbitrary string to categorize the data.
searchText
- A string to search the data. You can use this to store preview data as well.
- This field is searchable via sqlite full-text search.
Only data
is required. dataType
and searchText
are optional, and everything is stored as a string.
You need to use JSON.stringify
to store objects, and JSON.parse
to deserialize them.
Later I may provide a way to store objects directly, but for now, you need to serialize them.
Read/Search
There are multiple APIs to read data from the database.
db.retrieveAll
returns all data entries for the extension. Sometimes the extension may store large amount of data in thedata
field, while you may only need thesearchText
. So afields
parameter is provided to specify which fields you want to retrieve.db.retrieveAllByType
returns all data entries bydataType
.db.search
run search query within the database instead of retrieving all data and filtering them with JS.
Search
db.search
API supports searching on multiple fields. Use your IDE to go to the definition of search()
to see the full type definition.
dataId
is the unique ID of the data entry.fullTextSearch
is a boolean to enable full-text search on thesearchText
field.limit
is the maximum number of results to return.fields
is an array of fields to return.
Update
You need to provide the dataId
to update the data.
Delete
You need to provide the dataId
to delete the data.