NEAR-Edu / near-certification-tools

2 stars 2 forks source link

Use prisma as an alternative to raw SQL query #64

Open sccheruku opened 2 years ago

sccheruku commented 2 years ago

In PR #40 , we added a raw sql query to determine whether a certificate is valid or not based on NEAR account activity.

It might be worth considering a purely prisma approach, for a couple of reasons.

  1. As a matter of practice, it would be a good idea to avoid building raw sql queries because they can open us to sql injection. Although, in this case it is not a concern because we are working with a read only database.
  2. When examining the explain feature on postgresql, we can see that sql query has a high startup cost (cost=19396.61..19396.66 rows=2 width=12).

If we choose to refactor the sql query, we should still make sure the existing tests are passing.

One possible approach with prisma requires looping forward in 6 month time ranges to find a period where no transaction exists for a NEAR account.

Please see image below: image

First we check for the last transaction in a 6 month window, starting from the certificate issue date. Once a transaction is found, we check for another transaction in a 6 month window, starting from the transaction found in the previous check. We keep doing this until no transactions are found in a 6 month window, or we find a transaction that is less than 6 months from the Render date.

It is not ideal to query the database using loops. But is still worth consideration, because the startup cost of query using explain feature of postgresql is considerably low: (cost=24.10..25.36 rows=1 width=407)

ryancwalsh commented 2 years ago

@sccheruku Thank for writing this up!