Database
Zeus support multiple NoSQL
and SQL
databases namely MongoDB
, MySQL
, PostgreSQL
. You can choose any of them as per your need.
All database read and write operations are managed using
Prisma ORMMongodb (default
)
By default Zeus uses MongoDB
as it preferred database.
Replica set Configurations
When using MongoDB with Prisma, it's essential to have a configured replica set, as Prisma relies on transactions to prevent partial writes in nested queries. Without a replica set, you'll encounter an error stating: "Error: Transactions not supported in this deployment."
Mongodb allows you to perform transactions. Transactions are like a series of actions that should happen together; they either all succeed or all fail. Think of it as a way to ensure data consistency.
Since Prisma internally uses transactions to make sure that when you're working with nested queries (queries within other queries), everything is written correctly to the database. If you don't have a replica set, you'll get an error when you try to use nested queries.
For eg - this query below will fail if you don't have a replica set connection.
A replica set is a group of MongoDB servers that maintain the same data, providing redundancy and ensuring high availability. In simpler terms, it's a way to make sure your data is safe and accessible even if something goes wrong with one server.
So to fix this, I've written a Dockerfile script for a mongodb docker image which set up a replica set connection for you locally. It can be found inside ./mongodb_replica_servers/Dockerfile
alongside a docker-compose.yml
file which contains the configurations needed to initialize the replica set.
You dont need to worry about the above code, just run the following command to start the replica set.
_10docker-compose up -d
This will start the replica set on port 27001
and you can use it in your .env
file config.
MySQL
Make sure you have mysql installed locally on your machine before proceeding further.
Update your package.json
file with the supported prisma database schema.
_10 "prisma": {_10 "schema": "./src/prisma/mysql.prisma"_10 },
Then, Update your .env
file with the new database url.
PostgreSQL
Make sure you have postgresql installed locally on your machine before proceeding further.
Update your package.json
file with the supported prisma database schema.
_10 "prisma": {_10 "schema": "./src/prisma/postgres.prisma"_10 },
Then, Update your .env
file with the new database url.
Prisma Schema
Your Prisma schema is the source of truth for your database schema. It's the single source of truth for your database schema and is used to generate the Prisma Client, the Prisma Migrate migration engine (only for relational-database).
The prisma schema can be found in /src/prisma/{mongo, postgres, mysql}.prisma
file.
You don't need to reconfigure anything within the prisma schema file, except you need to switch to another database.
Switching to another database
If you want to support another database aside the default Mongodb
, simply update your package.json
file with the supported prisma database schema.
_10 "prisma": {_10 "schema": "./src/prisma/mongo.prisma" // mongo.schema, postgres.schema, mysql.schema_10 },
Then, Update your .env
file with the new database url.
And run the following command to generate the prisma client.
_10# yarn_10yarn dbPush_10_10# npm_10npm run dbPush