DEV Community

Charlotte Bush
Charlotte Bush

Posted on

Snapshots through the Prisma

Well, the cliche "new year, new me" may not apply to me personally, but it definitely applies to my Flatiron final project. I spent my last month in bootcamp making a patient advocacy tool for mental health prescriptions, and I'm really excited to deploy it and give it to its eager beta testers (and hopefully show it to some prospective employers!) That said, it's in dire need of some new years' TLC if I want to get it to that point. On the agenda:

1. Finding a good ORM library
2. Installing it, with node and express

  1. Converting the ORM models, seed logic, relationships, auth, and backend routes from Python, Flask, and SQLite into something more easily compatible with Postgres
  2. Setting up the Postgres docker image
  3. Updating the readme to reflect all the changes (I love good docs!)

So far, I'm about halfway through my list, converting my backend from my beloved Python into my uneasy new frenemies Typescript and Express. I found a cool new ORM to help manage everything, which is adding another mean girl to the Plastics' cafeteria table: Prisma.

Things I've loved about Prisma: writing models and relationships is so much more concise. A models.py file that took a hundred or so lines in Python (it was the association proxy stuff, I'm sure of it!) became a 50-line schema.prisma file.

Things I've hated about Prisma: the syntax feels like a hybrid of Python's more intuitive grammar and Javascript's finicky logic, and it definitely has a learning curve.

Today's adventure was wrapping up the search routing for my capstone app, which once I got the hang of, Prisma had a variety of tools to help me use to make it a little easier to offer users even more functionality than I'd originally planned. Let's get into it!

The first thing I noticed was that all the concision of writing models in Prisma totally vanished while I was writing routes. In Python, the GET request for my search functionality was about ten lines:

class MedicationsByName(Resource):
    def get(self):
        query= request.args.get("q")
        medications = Medication.query.filter(
            Medication.name_brand.ilike(f"%{query}%")
            | Medication.name_generic.ilike(f"%{query}%")
        ).all()
        return make_response([medication.to_dict(rules=("-faves","-treatments", "-conditions")) for medication in medications], 200)  

api.add_resource(MedicationsByName, '/medications')  
Enter fullscreen mode Exit fullscreen mode

In Prisma, if I pasted the whole of my GET request in here, it'd make this blog unreadably long. It's something like 80 lines, mostly of opening and closing curly brackets, which makes it feel more verbose than it actually is.

Once I got over that initial annoyance, though, I found out something cool. Prisma's model queries and filter conditionslet you essentially wire up a search GET to look for more than one model property at once. While you can also do this in Python, Prisma made it surprisingly elegant today!

  where: {
        OR: [
          {
            nameBrand: {
              contains: query,
              mode: "insensitive",
            },
Enter fullscreen mode Exit fullscreen mode

I used Prisma's "where" and "or" to essentially tell my search GET "hey, go back to the seed data, and look for the following fields in each object." Where in Python I was pretty much just letting users search for medications by name (brand or generic) I can now let users search for all types of attributes of their medications with a few quick keystrokes including side effects and other conditions that the medications can treat.

On top of that, adding:

mode: "insensitive"
Enter fullscreen mode Exit fullscreen mode

to the search objects allows the GET request to query my yaml database accurately regardless of the casing of the search terms. I also added Full-Text Search to my "side effects" search routing so that my GET request can more easily go through the big wall of text that is side effects data in my database.

I know I have a long way to go learning this new framework, but I picked up something really cool today, and was able to use it to make my search functionality more useful for my app's eventual users! Thanks, Prisma!

Top comments (0)