Fork me on GitHub
#clojurescript
<
2024-06-30
>
stagmoose00:06:48

i currently have a side project with python backend (fastapi+prisma) and cljs frontend. there's a thing that make me feel nasty: in my prisma have this for one table of my postgresql:

model BookReview {
    id        String   @id @default(uuid())
    quotes    String[]
    summary   String?
    user      User     @relation(fields: [userId], references: [id])
    userId    String
    book      Book     @relation(fields: [bookId], references: [id])
    bookId    String
    bookUrl   String
    isPublic  Boolean  @default(false)
    createdAt DateTime @default(now())
    updatedAt DateTime @updatedAt
}
in my cljs frontend:
($ Checkbox {:color "success"
            :radius "full"
            :defaultSelected (gobj/get entry "isPublic")
            :onValueChange (gfunc/debounce (fn [public?]
                                             (p/let [data (js/fetch "/book/review_make_public"
                                                                    #js {:method "POST"
                                                                         :headers #js {"Content-Type" "application/json"}
                                                                         :body (js/JSON.stringify
                                                                                #js {:id (gobj/get entry "id")
                                                                                     :is_public public?})})
                                                     json-data (.json data)]
                                               (notify "toggle public? successfully")
                                               (js/console.log "new review: " json-data)))
                                           1000)} "public?"))
in my python backend:
@router.post("/review_make_public")
async def review_make_public(request: Request, db = Depends(get_db), auth: JwtAuthorizationCredentials = Security(access_security)):
    req = await request.json()
    print("updated_book_review req: ", req)

    book_review_id = req.get("id")
    is_public = req.get("is_public")

    new_bookreview = db.bookreview.update(
        where={"id": book_review_id},
        data={"isPublic": is_public, "updatedAt": datetime.now()}
    )

    return new_bookreview
You can see there's a weird key inconsistency: is_public and isPublic. (because i want to follow python's snake_case naming style) I wonder what's the story for people go fullstack clj/cljs to deal with this "key naming"? Can they unify those naming things, like: define a db schema in clojure naming style, and then clj and cljs can know that schema -> so there will not have this inconsistency

stagmoose00:06:40

from: https://www.stitchdata.com/docs/destinations/postgresql/reference#transformations--column-naming Column names in PostgreSQL: Must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_) Must begin with a letter or underscore --- so i think there's will always have some inconsistency, because i cannot use public? as db column name (while i can use public? in clj/cljs)

valerauko07:06:55

i have a "middleware" on the client side to convert keys between snake_case to kebab-case

stagmoose07:06:01

do you mean middleware on the cljs side? how to do that? thought middleware is on the clj backend haha.

p-himik20:06:07

@U020WAVDMMK That page describes limits on unquoted identifiers. You can use any symbols in identifiers, as long as the name stays quoted:

CREATE TEMPORARY TABLE "??" (
    "1a"                           TEXT,
    "public?"                      TEXT,
    "!@#$%^&*()-=+[]{}:;',.<>/?`~" TEXT
);

stagmoose12:07:33

sorry didn't notice that! thanks for you pointing it out.

vputz20:06:32

Hey, feeling... Real Dumb 🙂 I've got a tiny re-frame spa running, but this is a generic cljs problem for me (and I'm well new). I'm trying to implement the PKCE login flow for my little SPA, and I'm stuck on the very most basic part: generating the code verifier and code challenge. I've got a code verifier function generating a random string, and I'm stuck on what should be the most basic easy thing: getting a sha256 digest of that string and the base64 encoding of that digest. And man, I get wrapped around the axle! If I use cljs-hash I get a great hex string representing the sha256 hash, which is fine, but if I use googe.crypt.base64/encodeString then it encodes the string of the hex digest, which results in the wrong thing. If I use goog.crypt.Sha256 I get a #js [int int int] which I can't seem to treat as anything. buddy/core doesn't seem to work in cljs. This seems... real fundamental and I feel real stupid. I can't find examples of PKCE flow in clojurescript anywhere. I'm pretty new to front-end but have done a few things in clojure and trying to push cljs for a new project but if I can't even get a basic login flow going, I may be stuck. Any pointers here?

thheller21:06:36

many of the goog functions take and return byte arrays instead of strings, just make sure you handle those properly. there are functions for that generally.

vputz21:06:42

I was noticing that (`#js [.. .. ..]`) but after the end of a day had trouble converting them to/from basic cljs types (sorry, as mentioned very new at the cljs side of things)

thheller07:07:39

#js [] is a javascript array. you do not convert this to CLJS, you basically stay in the javascript side of things

thheller07:07:10

there are utility methods in goog.crypt to convert them, see https://google.github.io/closure-library/api/goog.crypt.html

thheller07:07:03

and just like there is a base42/encodeString there is a base64/encodeByteArray. this is all more about JS interop than it is about CLJS

dvingo22:07:39

the browser has support for this built in(https://developer.mozilla.org/en-US/docs/Web/API/Crypto) eg. https://github.com/curityio/pkce-javascript-example you should be able to convert the js to cljs or even just import js from cljs

vputz15:07:54

Thanks; that's a nice minimal example. Doing it in a re-framey way shouldn't be too bad. I apologize for the continued noob questions, but is there a clojurescript "approved solution" for two basic ideas: 1. Assembling an url (I could try and js my way through eg goog.Uri and javascript which may be just fine, but thought I would ask) 2. Redirecting the user (Right now I'm just setting js/window location to the new URL Thanks again!

dvingo13:07:18

no worries. Anything that involves interacting the the "system" (browser, mobile, node.js etc) will be the same for plain JS. so for 1. you can use https://developer.mozilla.org/en-US/docs/Web/API/URL or a library like https://github.com/lambdaisland/uri for 2. -whatever solution you'd do in js I would recommend spending a little time going through https://funcool.github.io/clojurescript-unraveled/ to help speed up the learning :)