Zeus
Database

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 ORM

Mongodb (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.

test.js

_10
const user = await prisma.user.create({
_10
data: {
_10
name: "Alice",
_10
posts: {
_10
create: [{ title: "Post 1" }, { title: "Post 2" }, { title: "Post 3" }],
_10
},
_10
},
_10
});

💡

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.

docker/mongodb_replica_servers/Dockerfile
docker/docker-compose.yml

_11
FROM mongo:4
_11
_11
# we take over the default & start mongo in replica set mode in a background task
_11
ENTRYPOINT mongod --port $MONGO_REPLICA_PORT --replSet rs0 --bind_ip 0.0.0.0 & MONGOD_PID=$!; \
_11
# we prepare the replica set with a single node and prepare the root user config
_11
INIT_REPL_CMD="rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: '$MONGO_REPLICA_HOST:$MONGO_REPLICA_PORT' }] })"; \
_11
INIT_USER_CMD="db.getUser('$MONGO_INITDB_ROOT_USERNAME') || db.createUser({ user: '$MONGO_INITDB_ROOT_USERNAME', pwd: '$MONGO_INITDB_ROOT_PASSWORD', roles: [ 'root' ] })"; \
_11
# we wait for the replica set to be ready and then submit the commands just above
_11
until (mongo admin --port $MONGO_REPLICA_PORT --eval "$INIT_REPL_CMD && $INIT_USER_CMD"); do sleep 1; done; \
_11
# we are done but we keep the container by waiting on signals from the mongo task
_11
echo "REPLICA SET ONLINE"; wait $MONGOD_PID;

You dont need to worry about the above code, just run the following command to start the replica set.


_10
docker-compose up -d

This will start the replica set on port 27001 and you can use it in your .env file config.

.env

_10
DATABASE_URL="mongodb://root:12345@localhost:27001/veloz?authSource=admin"

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.

.env

_10
DATABASE_URL="mysql://root@localhost:3306/veloz-kit"

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.

.env

_10
DATABASE_URL="postgres://postgres@localhost:5432/veloz-kit"

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.

.env

_10
# mongodb
_10
DATABASE_URL="mongodb://root:12345@localhost:27001/veloz-kit?authSource=admin"
_10
_10
# mysql
_10
DATABASE_URL="mysql://root@localhost:3306/veloz-kit"
_10
_10
# postgresql
_10
DATABASE_URL="postgres://benrobo@localhost:5432/veloz-kit"

And run the following command to generate the prisma client.


_10
# yarn
_10
yarn dbPush
_10
_10
# npm
_10
npm run dbPush

© Veloz 2024