First, get some events in a DB
Continuing right where we left in the ingest guide, we will use the ORM models to add a few more events. Remember that events published by Soroban are XDR encoded. We can use the Stellar SDK to convert back and forth between values and XDR representation. In the following, we will use a topic calledtransfer and we will need some values and addresses. We can generate some test data:
- JavaScript
- Python
- Go
- Java
StellarEvent model from the ingest guide and send them to the database. The model needs an id and a type for each event:Here, we are storing XDR encoded values. We could have instead decided to store decoded values in the database. XDR being a compressed format, choosing when to decode the value is a tradeoff between CPU usage and memory consumption.
Consuming events
Using the same model we used to ingest events into the database, we can query the database to iterate over all events present in the table.- JavaScript
- Python
- Go
- Java
Streaming events
Depending on our application, we might want to consume events periodically by calling the database to see if there is anything new. Or fetch data as needed by our application. There is another possibility: event listeners! While we are at it, we can make the results more readable or usable by using the conversion helpers provided by the Stellar SDK.- JavaScript
- Python
- Go
- Java
Prisma has no database event listeners. A client extension can run code after every Next time a record gets inserted through
create sent through that client. Writes from other clients do not trigger it.listeningPrisma, this code will run. Let’s try this:Going further
Using the techniques we just presented would probably be enough for a lot of use cases. Still, for the readers wanting to go further there are a few things to look into.Asynchronous programming
So far, we have used SQLAlchemy in a synchronous way. If we were to have an endpoint in the backend calling the database, this endpoint would block during the database call. SQLAlchemy supports asynchronous programming withasync and await keywords.
As a general thought, it’s simpler to start with a synchronous logic and then move on to adding support for async when everything works as expected. Debugging concurrent application brings an additional layer of complexity.
SQLAlchemy allows you to simply change from a synchronous session to an asynchronous one without having to change your models nor queries making it a very easy task to use one or the other.