Types not matching on DB query

#1

Hi all,

Fairly new to Purescript with some light Haskell experience. I’m trying to access a SQLite database via the SQLite3 package using code that is almost verbatim from the example the package provides:

getMerchants = launchAff do
    conn <- newDB "./data"

    exists <- (\rows -> 1 == length rows) <$> queryDB conn "SELECT 1 from merchants"
    log $ "merchant: " <> (show exists)

    closeDB conn

The last line results in the following error:

Could not match type

Aff

with type

Effect

while trying to match type Aff Unit
with type Effect t0
while checking that expression closeDB conn
has type Effect t0
in value declaration getMerchants

where t0 is an unknown type

I understand that the closeDB line returns an Aff, but I was under the impression that I’m feeding that Aff into launchAff, which takes that Aff and returns an Effect.

Any help would be greatly appreciated as I’ve spent the last four hours just trying to figure out how to open and close a SQLite connection.

#2

Whose log are you using? Effect.Console.log and Effect.Class.Console.log are different, I think the latter may work for you? (Possibly, I’m unsure though as I haven’t used the latter)

#3

I was using Effect.Console.log.

Looking at the type signatures of each, it does seem like I should’ve been using Effect.Class.Console.log. I’ll try it out when I’m back at work tomorrow and see if it solves the issue. Thanks for the response!

#4

expression closeDB conn has type Effect t0

Hm, what’s your closeDB type? Maybe you have an older version that it returns Effect and not Aff

#5

So changing the log to Effect.Class.Console has shifted the error to the log line (second from the bottom) and it now reads:

Could not match type

Aff

with type

Function (Array Foreign)

while trying to match type t1 t2
with type Array Foreign -> t0
while checking that expression (discard ((apply log) ((…) (…)))) ($__unused ->
closeDB conn
)
has type Array Foreign -> t0
in value declaration getMerchants

where t0 is an unknown type
t2 is an unknown type
t1 is an unknown type

The type for closeDB is:

closeDB :: DBConnection -> Aff Unit

#6

Is there a reason for using such an old version of purescript-node-sqlite3? You’re presumably using version 0.5.0 whereas the latest is 6.0.0

#7

From package.json:

"dependencies": {
    "sqlite3": "^4.1.1"
  }

I just installed it via bower as per the package’s Readme.

To your point, through, I was looking at the wrong documentation. I am editing my above response with the correct type signature.

#8

That’s your sqlite version, not the purescript-node-sqlite3 version

For what it’s worth however, using bower is outdated and we recommend moving to spago
Although you’ll still need to install sqlite3 via npm

#9

Thanks for the info about spago, I’ll look into that.

Looks like purescript-node-sqlite3 has version 6.0.0

#10

I believe your issue is that you haven’t passed in the params argument to queryDB. Should just be something like:

exists <- (\rows -> 1 == length rows) <$> queryDB conn "SELECT 1 from merchants" []
1 Like
#11

Well that was a dumb mistake on my part. Oof.

That has moved the error to just the queryDB function call. It now reads:

Could not match type

Foreign

with type

t1 t2

while trying to match type Aff Foreign
with type t0 (t1 t2)
while checking that expression ((queryDB conn) “SELECT 1 from merchants”) []
has type t0 (t1 t2)
in value declaration getMerchants

where t0 is an unknown type
t1 is an unknown type
t2 is an unknown type

I appreciate your help on this!

#12

I think the example in the documentation is possibly outdated, as queryDB returns Aff Foreign, so your function on the LHS of <$> should be of type Foreign -> b, yet the documentation uses rows as if it was an array-like

I don’t really mess with Foreign, so your best bet is to explore that/look at the tests which are a more modern example of how to work with this module https://github.com/justinwoo/purescript-node-sqlite3/blob/v6.0.0/test/Main.purs

1 Like
#13

Looks like you’re spot on. Thanks for the tip, I’ll be sure to check tests in the future.