Skip to content

Commit

Permalink
Add schemas and queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
ychescale9 committed Dec 1, 2023
1 parent 9bc111a commit 659181b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import io.github.reactivecircus.kstreamlined.kmp.persistence.database.FeedItemEntity
import io.github.reactivecircus.kstreamlined.kmp.persistence.database.InstantAdapter
import io.github.reactivecircus.kstreamlined.kmp.persistence.database.KStreamlinedDatabase
import javax.inject.Singleton

Expand All @@ -24,6 +26,9 @@ object PersistenceModule {
schema = KStreamlinedDatabase.Schema,
context = context,
name = "kstreamlined.db",
),
feedItemEntityAdapter = FeedItemEntity.Adapter(
publishTimeAdapter = InstantAdapter
)
)
}
Expand Down
1 change: 1 addition & 0 deletions kmp/persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ kotlin {
commonMain {
dependencies {
implementation(libs.androidx.dataStore)
api(libs.kotlinx.datetime)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.reactivecircus.kstreamlined.kmp.persistence.database

import app.cash.sqldelight.ColumnAdapter
import kotlinx.datetime.Instant
import kotlinx.datetime.toInstant

public val InstantAdapter: ColumnAdapter<Instant, String> = object : ColumnAdapter<Instant, String> {
override fun decode(databaseValue: String): Instant {
return databaseValue.toInstant()
}

override fun encode(value: Instant): String {
return value.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import kotlin.Boolean;

CREATE TABLE feedOriginEntity (
key TEXT NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
description TEXT NOT NULL,
selected INTEGER AS Boolean NOT NULL
);

CREATE INDEX index_feedOriginEntity_selected ON feedOriginEntity(selected);

allFeedOrigins:
SELECT * FROM feedOriginEntity;

upsertFeedOrigin:
INSERT OR REPLACE INTO feedOriginEntity
VALUES (:key, :title, :description, :selected);
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import kotlin.Boolean;
import kotlinx.datetime.Instant;

CREATE TABLE feedItemEntity (
id TEXT NOT NULL PRIMARY KEY,
feedOriginKey TEXT NOT NULL,
title TEXT NOT NULL,
publishTime TEXT NOT NULL,
savedForLater INTEGER AS Boolean NOT NULL
publishTime TEXT AS Instant NOT NULL,
contentUrl TEXT NOT NULL,
imageUrl TEXT,
savedForLater INTEGER AS Boolean NOT NULL,
FOREIGN KEY(feedOriginKey) REFERENCES feedOriginEntity(key) ON DELETE CASCADE
);

findAllFeedItems:
SELECT * FROM feedItemEntity;
CREATE INDEX index_feedItemEntity_feedOriginKey ON feedItemEntity(feedOriginKey);
CREATE INDEX index_feedItemEntity_savedForLater ON feedItemEntity(savedForLater);

feedItemsForSelectedOrigins:
SELECT feedItemEntity.* FROM feedItemEntity
JOIN feedOriginEntity ON feedItemEntity.feedOriginKey = feedOriginEntity.key
WHERE feedOriginEntity.selected = 1
ORDER BY feedItemEntity.publishTime DESC;

savedFeedItems:
SELECT * FROM feedItemEntity WHERE savedForLater = 1 ORDER BY publishTime DESC;

upsertFeedItem:
INSERT OR REPLACE INTO feedItemEntity
VALUES (:id, :feedOriginKey, :title, :publishTime, :contentUrl, :imageUrl, :savedForLater);

0 comments on commit 659181b

Please sign in to comment.