Coder Social home page Coder Social logo

objectstore's Introduction

ObjectStore

Maven Central Codecov

A modular object storage framework for Kotlin multiplatform projects.

Usage

ObjectStore provides a simple key/value storage interface which by default uses Type details to derive the Key automatically. To create an ObjectStore you need two things:

  • ObjectStoreWriter: Providers the persistence mechanism to store data for later access.
  • ObjectStoreSerializer: Provides the serialization mechanism to transform objects for storage.
val store = ObjectStore(
    storeWriter = SharedPreferencesStoreWriter("prefs", context),
    storeSerializer = JsonStoreSerializer()
)

// Store an object
store.put(User("username", "email", ...))

// Get an object or null
val user: User? = store.getOrNull<User>()
// Get an object or throw
val user: User = store.get<User>()
// Get an object or default
val user: User = store.get(default = User(...))

// Get a StateFlow
val userFlow: StateFlow<User?> = store.getFlow<User>()
// Calls to `put` new user objects will be emitted
userFlow.collect { println(it) }

// Get all keys
store.keys()

// Remove an object
store.remove<User>()
// Remove all objects
store.clear()

When storing basic types such as String, Boolean, etc. you must provide a key for the record.

store.put(false, key = "my_key")
store.get<Boolean>(default = false, key = "my_key")

NOTE: When targeting Javascript, all classes used with ObjectStore must be annotated with @Serializable. This is used to derive class and parameter name based keys, other platforms do not use the Kotlinx.serialization library in objectstore-core.

Serializers

Turning objects into data suitable for storage requires a ObjectStoreSerializer implementation. The following modules provide serialization capabilities using the matching Kotlinx.serialization module.

  • objectstore-cbor: CborStoreSerializer()
  • objectstore-json: JsonStoreSerializer()
  • objectstore-protobuf: ProtoBufStoreSerializer()

Writers

Storing object data requires a ObjectStoreWriter implementation. The following Writers are provided in the objectstore-core module:

  • Android: SharedPreferencesStoreWriter("prefs_name", context)
  • iOS/macOS/tvOS/watchOS: UserDefaultsStoreWriter()
  • Browser JS: LocalStorageStoreWriter()
  • All: InMemoryStoreWriter()

File Writer

The objectstore-fs provides file based storage using okio. All targets are supported except iosArm32 and jsBrowser.

val store = ObjectStore(
    storeWriter = FileStoreWriter("/storage-directory")
)

The provided path must not exist or be an existing directory where files can be stored. Each value will be stored in a separate file using the hex encoded key as the filename.

Secure Writers

To store data in a secure way, the objectstore-secure module provides Writers which encrypt data when stored on disk.

  • iOS/macOS/tvOS/watchOS: KeychainStoreWritre("com.service.name", "com.service.group")
  • Android: EncryptedSharedPreferencesStoreWriter("prefs_name", context)

Wrapped Writers

The ValueTransformingStoreWriter provides a hook to encode/decode values before they are written to disk. The transform methods are defined as (type: KType, value: T) -> T, when unhandled you must return the original value.

val storeWriter = InMemoryStoreWriter().transformValue(
    transformGet = { _, value -> (value as? String)?.base64Decoded() ?: value },
    transformSet = { _, value -> (value as? String)?.base64Encoded() ?: value }
)

The MemCachedStoreWriter provides lazy in-memory caching around any ObjectStoreWriter implementation.

val storeWriter = FileStoreWriter("/data").memCached()

Download

Maven Central Sonatype Nexus (Snapshots)

repositories {
    mavenCentral()
    // Or snapshots
    maven("https://s01.oss.sonatype.org/content/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 to expand)
[versions]
objectstore = "1.0.0-SNAPSHOT"

[libraries]
objectstore-core = { module = "org.drewcarlson:objectstore-core", version.ref = "objectstore" }
objectstore-fs = { module = "org.drewcarlson:objectstore-fs", version.ref = "objectstore" }
objectstore-cbor = { module = "org.drewcarlson:objectstore-cbor", version.ref = "objectstore" }
objectstore-json = { module = "org.drewcarlson:objectstore-json", version.ref = "objectstore" }
objectstore-protobuf = { module = "org.drewcarlson:objectstore-protobuf", version.ref = "objectstore" }
objectstore-secure = { module = "org.drewcarlson:objectstore-secure", version.ref = "objectstore" }

License

This project is licensed under Apache-2.0, found in LICENSE.

objectstore's People

Contributors

renovate[bot] avatar drewcarlson avatar

Stargazers

ChineseBoy avatar  avatar Mike Cwiklinski avatar Micro·J avatar Vladislav Bauer avatar windedge avatar Gabriel Patzleiner avatar David Scheutz avatar

Watchers

James Cloos avatar  avatar

objectstore's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update JamesIves/github-pages-deploy-action action to v4.6.4
  • Update dependency gradle to v8.10.1
  • Update okio to v3.9.1 (com.squareup.okio:okio-fakefilesystem, com.squareup.okio:okio-nodefilesystem, com.squareup.okio:okio)
  • Update serialization to v1.7.2 (org.jetbrains.kotlinx:kotlinx-serialization-cbor, org.jetbrains.kotlinx:kotlinx-serialization-protobuf, org.jetbrains.kotlinx:kotlinx-serialization-json, org.jetbrains.kotlinx:kotlinx-serialization-core)
  • Update coroutines to v1.9.0 (org.jetbrains.kotlinx:kotlinx-coroutines-test, org.jetbrains.kotlinx:kotlinx-coroutines-core)
  • 🔐 Create all rate-limited PRs at once 🔐

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/code-quality.yml
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • tj-actions/changed-files v44
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-java v4
  • actions/cache v4
  • gradle/gradle-build-action v3
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-java v4
  • actions/cache v4
  • gradle/gradle-build-action v3
.github/workflows/publish.yml
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-java v4
  • actions/cache v4
  • gradle/gradle-build-action v3
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-java v4
  • actions/cache v4
  • gradle/gradle-build-action v3
  • JamesIves/github-pages-deploy-action v4.6.3
.github/workflows/tests.yml
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-java v4
  • actions/cache v4
  • gradle/gradle-build-action v3
  • actions/upload-artifact v3
  • actions/upload-artifact v3
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-java v4
  • gradle/gradle-build-action v3
  • actions/upload-artifact v3
  • actions/upload-artifact v3
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-java v4
  • actions/cache v4
  • gradle/gradle-build-action v3
  • actions/upload-artifact v3
  • actions/upload-artifact v3
  • actions/download-artifact v3
  • codecov/codecov-action v4
  • actions/download-artifact v3
  • mikepenz/action-junit-report v4
gradle
buildSrc/src/main/java/ModuleConfig.kt
gradle.properties
settings.gradle.kts
build.gradle.kts
buildSrc/settings.gradle.kts
buildSrc/build.gradle.kts
gradle/libs.versions.toml
  • org.jetbrains.kotlin:kotlin-gradle-plugin 2.0.10
  • com.android.tools.build:gradle 7.4.2
  • org.jetbrains.kotlinx:kotlinx-coroutines-core 1.8.1
  • org.jetbrains.kotlinx:kotlinx-coroutines-test 1.8.1
  • org.jetbrains.kotlinx:kotlinx-serialization-core 1.7.1
  • org.jetbrains.kotlinx:kotlinx-serialization-json 1.7.1
  • org.jetbrains.kotlinx:kotlinx-serialization-protobuf 1.7.1
  • org.jetbrains.kotlinx:kotlinx-serialization-cbor 1.7.1
  • org.jetbrains.kotlinx:atomicfu 0.24.0
  • org.jetbrains.kotlinx:atomicfu-gradle-plugin 0.24.0
  • androidx.security:security-crypto-ktx 1.1.0-alpha06
  • org.robolectric:robolectric 4.13
  • com.squareup.okio:okio 3.9.0
  • com.squareup.okio:okio-nodefilesystem 3.9.0
  • com.squareup.okio:okio-fakefilesystem 3.9.0
  • org.jetbrains.kotlinx:kotlinx-datetime 0.5.0
  • org.jetbrains.kotlin.multiplatform 2.0.10
  • org.jetbrains.kotlin.jvm 2.0.10
  • org.jetbrains.kotlin.plugin.serialization 2.0.10
  • org.jetbrains.kotlinx.kover 0.6.1
  • org.jetbrains.kotlin.android 2.0.10
  • com.diffplug.spotless 6.19.0
  • org.jetbrains.dokka 1.9.20
  • org.jetbrains.kotlinx.binary-compatibility-validator 0.16.3
  • com.louiscad.complete-kotlin 1.1.0
  • com.vanniktech.maven.publish 0.27.0
objectstore-cbor/build.gradle.kts
objectstore-core/build.gradle.kts
objectstore-fs/build.gradle.kts
objectstore-json/build.gradle.kts
objectstore-protobuf/build.gradle.kts
objectstore-secure/build.gradle.kts
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 8.10

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.