Fork me on GitHub
#nbb
<
2023-08-30
>
maleghast14:08:08

Hello All... I am trying to use dotenv in my nbb app, and I really don't grok how to access the key I want from my .env file. Has anyone done this and can offer not only an example, but any recommendations of where to look for insights on how to translate npm package examples to CLJS in the nbb context, as this is clearly something I need to brush up on...?

borkdude14:08:53

do you have an example of how it's used in JS?

maleghast14:08:27

I do...

require('dotenv').config()

const mysql = require('mysql2')

const connection = mysql.createConnection(process.env.DATABASE_URL)

maleghast14:08:16

(I have mysql2 installed as well, and am hoping to "drop in" mysql2 as a replacement / alternative for sqlite)

borkdude14:08:01

do you also have an example of a file that should be read by dotenv so I can test it?

maleghast14:08:56

It should just be a plaintext file in root of project called .env with a key per line, like:

DATABASE_URL=':is:a:database:url'

maleghast14:08:22

I think that I have figured it out... js/process.env.DATABASE_URL seems to work

maleghast14:08:33

I think that the mere act of asking the question got me thinking about it differently, remote rubber-ducking, if you will... Thanks for getting back to me, I appreciate it 🙂

borkdude15:08:06

$ nbb
Welcome to nbb v1.2.173!
user=> (require '["dotenv" :as dotenv])
nil
user=> (dotenv/config)
#js {:parsed #js {:DATABASE_URL ":is:a:database:url"}}
user=> js/process.env.DATABASE_URL
":is:a:database:url"
Yup seems to work

maleghast15:08:31

Excellent 🙂

maleghast15:08:37

Thanks again for looking at it.

borkdude15:08:54

added an example here just in case someone asks again:

maleghast15:08:21

Oh, I think if you do this: (require '["dotenv/config"]) you don't need to do: (dotenv/config)

borkdude15:08:21

confirmed:

(ns dotenv.example
  (:require ["dotenv/config"]))

(prn js/process.env.DATABASE_URL)

maleghast17:08:46

OK, so how would I cast / force a retrieved value to be an Integer in nbb?

maleghast17:08:46

My instinct was to try (Integer/parseInt ...) but that is not recognised in my environment (I'm jacked-in on VSCode with Calva)

borkdude17:08:24

Try parse-int

borkdude17:08:39

no, sorry, parse-long

maleghast17:08:59

That at least parses, but I think that I may be approaching the problem incorrectly.

maleghast17:08:52

I am doing a (SQL) query and then attempting to retrieve the last insert ID from the response, and I had assumed that it was coming back as a string

maleghast17:08:25

I think I am actually failing to retrieve the value correctly 😞

maleghast17:08:02

Do you have a recommendation on how to push a value out to the console in a Calva session?

maleghast17:08:43

(I seem to be having some success with println)

borkdude20:08:07

Perhaps (js/console.log *1) ?

maleghast20:08:45

Ooh, thanks!

maleghast18:08:24

Different question... Let's assume that p_resp is the result of a MySQL query, it's a JavaScript Array of Objects I need to extract insertID which is in the object in the 0-index, so clearly (.-insertId p_resp) is not working, but I can't figure out a JS / nbb / Cljs way to handle this array of Javascript Objects...

borkdude20:08:55

Does this answer your question?

user=> (def arr #js [#js {:foo 1}])
#'user/arr
user=> arr
#js [#js {:foo 1}]
user=> (-> arr first (.-foo))
1

maleghast20:08:09

I think that it might, thanks!