A modular object storage framework for Kotlin multiplatform initiatives.
Utilization
ObjectStore supplies a easy key/worth storage interface which by default makes use of Sort particulars to derive the Key mechanically. To create an ObjectStore you want two issues:
ObjectStoreWriter: Suppliers the persistence mechanism to retailer information for later entry. ObjectStoreSerializer: Supplies the serialization mechanism to rework objects for storage.
storeWriter = SharedPreferencesStoreWriter(“prefs“, context),
storeSerializer = JsonStoreSerializer()
)
// Retailer an object
retailer.put(Person(“username“, “electronic mail“, ...))
// Get an object or null
val person: Person? = retailer.getOrNull<Person>()
// Get an object or throw
val person: Person = retailer.get<Person>()
// Get an object or default
val person: Person = retailer.get(default = Person(...))
// Get a StateFlow
val userFlow: StateFlow<Person?> = retailer.getFlow<Person>()
// Calls to `put` new person objects might be emitted
userFlow.acquire { println(it) }
// Get all keys
retailer.keys()
// Take away an object
retailer.take away<Person>()
// Take away all objects
retailer.clear()
When storing fundamental sorts equivalent to String, Boolean, and many others. you will need to present a key for the file.
retailer.get<Boolean>(default = false, key = “my_key“)
NOTE: When focusing on Javascript, all lessons used with ObjectStore should be annotated with @Serializable. That is used to derive class and parameter title primarily based keys, different platforms don’t use the Kotlinx.serialization library in objectstore-core.
Serializers
Turning objects into information appropriate for storage requires a ObjectStoreSerializer implementation. The next modules present serialization capabilities utilizing the matching Kotlinx.serialization module.
objectstore-cbor: CborStoreSerializer() objectstore-json: JsonStoreSerializer() objectstore-protobuf: ProtoBufStoreSerializer()
Writers
Storing object information requires a ObjectStoreWriter implementation. The next Writers are offered within the objectstore-core module:
Android: SharedPreferencesStoreWriter(“prefs_name”, context) iOS/macOS/tvOS/watchOS: UserDefaultsStoreWriter() Browser JS: LocalStorageStoreWriter() All: InMemoryStoreWriter()
File Author
The objectstore-fs supplies file primarily based storage utilizing okio. All targets are supported besides iosArm32 and jsBrowser.
storeWriter = FileStoreWriter(“/storage-directory“)
)
The offered path should not exist or be an present listing the place information might be saved. Every worth might be saved in a separate file utilizing the hex encoded key because the filename.
Safe Writers
To retailer information in a safe manner, the objectstore-secure module supplies Writers which encrypt information when saved on disk.
iOS/macOS/tvOS/watchOS: KeychainStoreWritre(“com.service.title”, “com.service.group”) Android: EncryptedSharedPreferencesStoreWriter(“prefs_name”, context)
Wrapped Writers
The ValueTransformingStoreWriter supplies a hook to encode/decode values earlier than they’re written to disk. The remodel strategies are outlined as (kind: KType, worth: T) -> T, when unhandled you will need to return the unique worth.
transformGet = { _, worth -> (worth as? String)?.base64Decoded() ?: worth },
transformSet = { _, worth -> (worth as? String)?.base64Encoded() ?: worth }
)
The MemCachedStoreWriter supplies lazy in-memory caching round any ObjectStoreWriter implementation.
Obtain
mavenCentral()
// Or snapshots
maven(“https://s01.oss.sonatype.org/content material/repositories/snapshots/“)
}
dependencies {
implementation(“org.drewcarlson:objectstore-core:$VERSION“)
// Serializers
implementation(“org.drewcarlson:objectstore-cbor:$VERSION“)
implementation(“org.drewcarlson:objectstore-json:$VERSION“)
implementation(“org.drewcarlson:objectstore-protobuf:$VERSION“)
// Writers
implementation(“org.drewcarlson:objectstore-fs:$VERSION“)
implementation(“org.drewcarlson:objectstore-secure:$VERSION“)
}
Toml (Click on to broaden)
objectstore = “1.0.0-SNAPSHOT“
[libraries]
objectstore-core = { module = “org.drewcarlson:objectstore-core“, model.ref = “objectstore“ }
objectstore-fs = { module = “org.drewcarlson:objectstore-fs“, model.ref = “objectstore“ }
objectstore-cbor = { module = “org.drewcarlson:objectstore-cbor“, model.ref = “objectstore“ }
objectstore-json = { module = “org.drewcarlson:objectstore-json“, model.ref = “objectstore“ }
objectstore-protobuf = { module = “org.drewcarlson:objectstore-protobuf“, model.ref = “objectstore“ }
objectstore-secure = { module = “org.drewcarlson:objectstore-secure“, model.ref = “objectstore“ }
License
This venture is licensed beneath Apache-2.0, present in LICENSE.