Usage
SQLiteStorage¶
Creating an instance¶
Reading¶
To read a single entry from the database, use the read
method.
To read multiple entries from the database, use the readMany
method.
You can also read entries as
a Flow
/AsyncSequence
using the readAsFlow
method. A change in any of provided entries will emit an updated list of
entries.
Writing¶
To write a single entry to the database, use the write
method.
To write multiple entries to the database, use the writeMany method.
Removing¶
To remove a single entry from the database, use the remove
method.
To remove multiple entries from the database, use the removeMany
method.
Merging¶
Info
Merging rules:
- if either value is null, return new value
- if either value is not Json object or Json array, return new value
- if both values are valid JSON array, append new one to old one
- if both values are valid JSON object, merge them deeply following merging rules
To merge a single entry in the database, use the merge
method.
func singleMerge() async throws {
let entry = Entry(key: "key", value: #"{"name":"jerry","friends":[]}"#)
try await storage.write(entry: entry)
let updated = Entry(key: "key", value: #"{"friends": ["thomas"], "age": 30}"#)
let result = try await storage.merge(entry: updated)
// result: {"name": "jerry", friends": ["thomas"], "age": 30}
}
To merge multiple entries in the database, use the mergeMany
method.
suspend fun manyMerge() {
val entry1 = Entry("key", """{"name":"henry","friends":[]}""")
val entry2 = Entry("key", """{"friends":["julia"]}""")
val entry3 = Entry("key", """{"age": 40}""")
storage.write(entry1)
val result = storage.mergeMany(listOf(entry2, entry3))
// result: {"name": "henry", friends": ["julia"], "age": 40}
}
func manyMerge() async throws {
let entry1 = Entry(key: "key", value: #"{"name":"henry","friends":[]}"#)
let entry2 = Entry(key: "key", value: #"{"friends":["julia"]}"#)
let entry3 = Entry(key: "key", value: #"{"age": 40}"#)
try await storage.write(entry: entry1)
let result = try await storage.mergeMany(entries: [entry2, entry3])
// result: {"name": "henry", friends": ["julia"], "age": 40}
}
Reading keys¶
To retrieve all keys used to store entries in the database, use the readKeys
method.
You can also read keys as
a Flow
/AsyncSequence
using the readKeysAsFlow
method. Any change in the entries within the provided key list will
trigger an emit of the key list again.
Clearing¶
To clear all entries from the database, use the clear
method.
Warning
This action wipes out whole database.
Close connection¶
To close the connection to the storage, use closeConnection()
method. Before closing,
the PRAGMA optimize;
is executed.
Warning
Once connection is closed, the storage instance is no longer usable for database reads/writes and will throw an error when done so
DatabaseFiles¶
The DatabaseFiles
interface provides methods to access information about database files for
given SQLiteStorage
instance.
Path to a storage file¶
Returns an absolute path to the selected database file. Default type is DatabaseFileType.Main
.
Path to containing directory¶
Returns an absolute path to a directory containing database files.
Delete database file¶
Removes the database file and all related Write-Ahead Logging (WAL) files (-wal
and -shm
).
Warning
Make sure the connection to database is closed first!
File size¶
Returns the size of a selected database file in bytes. If the file does not exist or cannot be read for some
reason, returns null
.
Info
Close the database connection first, to get the most accurate results.
DatabaseFileType¶
The DatabaseFileType
enum defines the types of database files. Refer
to SQLite WAL Format for more information on the WAL
file format.
Main
: The main database file.Wal
: Write-Ahead Logging (WAL) file.Index
: Index file.