git.iko.soy/rowtype-yoga/purescript-yoga-scylladb
on main help sign in
Release v0.1.1
commite8b392ae88db022fa03debdf74a4b113eabbbbcf authorMark <mark.eibes@gmail.com> dateWed 12 Aug 2026, 23:07 (2 months ago) parentfd7f188
5 files changed+48−24
spago.yaml+1−1 src/Yoga/ScyllaDB/Schema.purs+4−19 src/Yoga/ScyllaDB/Schema.test.js+2 src/Yoga/ScyllaDB/Schema.test.purs+30−2 src/Yoga/ScyllaDB/ScyllaDB.purs+11−2
spago.yaml+1−1
@@ -3,7 +3,7 @@ workspace: {}
33package:
44 name: yoga-scylladb
55 publish:
6− version: 0.1.0
6+ version: 0.1.1
77 license: MIT
88 location:
99 githubOwner: rowtype-yoga
src/Yoga/ScyllaDB/Schema.purs+4−19
@@ -9,7 +9,6 @@ import Data.Either (Either(..))
99import Data.Foldable (foldl)
1010import Data.Map as Map
1111import Data.Maybe (Maybe(..))
12−import Data.Nullable (toNullable)
1312import Data.String.Regex as Regex
1413import Data.String.Regex.Flags as RegexFlags
1514import Data.Symbol (class IsSymbol, reflectSymbol)
@@ -486,22 +485,8 @@ else instance HasAnyDMLRL (RL.Cons "delete" Unit rest)
486485else instance HasAnyDMLRL rest => HasAnyDMLRL (RL.Cons label typ rest)
487486else instance Fail (Text "WHERE requires a preceding SELECT, UPDATE (set), or DELETE") => HasAnyDMLRL RL.Nil
488487
489−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
490−-- FieldToCQLValue: convert a PureScript value to Foreign for CQL
491−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
492−
493−class FieldToCQLValue :: Type -> Constraint
494−class FieldToCQLValue a where
495− fieldToCQLValue :: a -> Foreign
496−
497−instance FieldToCQLValue a => FieldToCQLValue (Maybe a) where
498− fieldToCQLValue = toNullable >>> unsafeCoerce
499−else instance FieldToCQLValue DateTime where
500− fieldToCQLValue = unsafeCoerce
501−else instance FieldToCQLValue Scylla.UUID where
502− fieldToCQLValue = unsafeCoerce
503−else instance FieldToCQLValue a where
504− fieldToCQLValue = unsafeCoerce
488+-- Values use the public Scylla.ToCQLValue extension point. Maybe must unwrap
489+-- through that instance so wrapped domain values keep their database encoding.
505490
506491-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
507492-- RecordValuesRL: walk a RowList and extract values from a record
@@ -517,12 +502,12 @@ instance RecordValuesRL RL.Nil row where
517502instance
518503 ( IsSymbol name
519504 , Row.Cons name typ rest row
520− , FieldToCQLValue typ
505+ , Scylla.ToCQLValue typ
521506 , RecordValuesRL tail row
522507 ) =>
523508 RecordValuesRL (RL.Cons name typ tail) row where
524509 recordValuesRL _ rec =
525− [ fieldToCQLValue (Record.get (Proxy :: Proxy name) rec) ]
510+ [ Scylla.toCQLValue (Record.get (Proxy :: Proxy name) rec) ]
526511 <> recordValuesRL (Proxy :: Proxy tail) rec
527512
528513-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
src/Yoga/ScyllaDB/Schema.test.jsnew file+2
@@ -0,0 +1,2 @@
1+export const foreignTypeName = (value) => value === null ? "null" : typeof value;
2+export const foreignStringValue = (value) => value;
src/Yoga/ScyllaDB/Schema.test.purs+30−2
@@ -2,17 +2,23 @@ module Yoga.ScyllaDB.Schema.Test where
22
33import Prelude
44
5−import Foreign (unsafeToForeign)
5+import Data.Array as Array
6+import Data.Maybe (Maybe(..), fromJust)
7+import Foreign (Foreign, unsafeToForeign)
8+import Partial.Unsafe (unsafePartial)
69import Unsafe.Coerce (unsafeCoerce)
710import Type.Function (type (#))
811import Type.Proxy (Proxy(..))
912import Data.DateTime (DateTime)
10−import Data.Maybe (Maybe)
1113import Test.Spec (Spec, describe, it)
1214import Test.Spec.Assertions (shouldEqual)
1315import Yoga.ScyllaDB.ScyllaDB as Scylla
1416import Yoga.ScyllaDB.Schema as S
1517
18+foreign import foreignTypeName :: Foreign -> String
19+foreign import foreignStringValue :: Foreign -> String
20+
21+
1622type UsersTable = S.Table "users"
1723 ( id :: Scylla.UUID # S.PartitionKey
1824 , name :: String
@@ -33,6 +39,15 @@ type EventsTable = S.Table "events"
3339 , value :: Number
3440 )
3541
42+newtype EncodedValue = EncodedValue String
43+
44+instance Scylla.ToCQLValue EncodedValue where
45+ toCQLValue _ = unsafeToForeign "encoded"
46+
47+type EncodedTable = S.Table "encoded"
48+ ( value :: Maybe EncodedValue
49+ )
50+
3651spec :: Spec Unit
3752spec = do
3853 describe "ScyllaDB Schema" do
@@ -112,6 +127,19 @@ spec = do
112127 it "maps Maybe Int to int" do
113128 S.cqlTypeName (Proxy :: Proxy (Maybe Int)) `shouldEqual` "int"
114129
130+ describe "ToCQLValue" do
131+
132+ it "unwraps Maybe through the inner CQL value conversion" do
133+ let encoded = Scylla.toCQLValue (Just (EncodedValue "raw") :: Maybe EncodedValue)
134+ foreignTypeName encoded `shouldEqual` "string"
135+ foreignStringValue encoded `shouldEqual` "encoded"
136+ foreignTypeName (Scylla.toCQLValue (Nothing :: Maybe EncodedValue)) `shouldEqual` "null"
137+
138+ it "uses ToCQLValue when extracting typed insert values" do
139+ let S.Q query = S.from (Proxy :: Proxy EncodedTable) # S.insert { value: Just (EncodedValue "raw") }
140+ Array.length query.values `shouldEqual` 1
141+ foreignStringValue (unsafePartial $ fromJust $ Array.head query.values) `shouldEqual` "encoded"
142+
115143 describe "Builder: SELECT" do
116144
117145 it "generates SELECT * with builder" do
src/Yoga/ScyllaDB/ScyllaDB.purs+11−2
@@ -3,9 +3,10 @@ module Yoga.ScyllaDB.ScyllaDB where
33import Prelude
44
55import Data.Array as Array
6−import Data.Maybe (Maybe)
6+import Data.DateTime (DateTime)
7+import Data.Maybe (Maybe(..))
78import Data.Newtype (class Newtype)
8−import Data.Nullable (Nullable)
9+import Data.Nullable (Nullable, toNullable)
910import Data.Nullable as Nullable
1011import Data.Time.Duration (Milliseconds)
1112import Effect (Effect)
@@ -307,6 +308,14 @@ instance ToCQLValue Foreign where
307308instance ToCQLValue UUID where
308309 toCQLValue = unsafeCoerce
309310
311+instance ToCQLValue DateTime where
312+ toCQLValue = unsafeCoerce
313+
314+instance ToCQLValue a => ToCQLValue (Maybe a) where
315+ toCQLValue = case _ of
316+ Nothing -> unsafeCoerce (toNullable Nothing)
317+ Just value -> toCQLValue value
318+
310319instance ToCQLValue a => ToCQLValue (Array a) where
311320 toCQLValue arr = unsafeCoerce (map toCQLValue arr)
312321