- You want to keep your business invariants OUT of your codebase.
- You don't need a JOIN to get a relational data out of your database.
- You want transactional action on your data
- You want a strong query language to get efficiently whatever data you have.
- You want to take benefit of a RDBMS to manage all security issues.
Let me clarify on these use cases and benefits you can get.
1. Business invariants:
For example, you're developing a microservice to manage your users. It'll handle new users registration. One obvious business invariant here is:
Email must be unique
So, you started to write test, write code to validate it in your code, right ?
I would say, in this case, you can get benefit of a RDBMS (PostgreSQL) in this case, by using a sql statement to enforce this rule OUT of your code base:
CREATE TABLE unique_email_policy_records ( id serial NOT NULL, email character varying not null, CONSTRAINT unique_email_policy_records_pkey PRIMARY KEY (id) ); CREATE UNIQUE INDEX unique_email_policy_records_keys ON unique_email_policy_records USING btree (email);
This sql script will enforce that, when you want to insert new user into your database, it must not contain duplicate email. No need in your code to enforce this.
2. You don't need a JOIN to get a relational data out of your database.
So, after you already insert your user in your database, you'll want to query the users that you have.
There're many ways to achive this. You can use a sql trigger, so that whenever a user is registered, it'll insert into new table, which is for querying.
CREATE TABLE user_records%SUFFIX% ( id serial NOT NULL, email character varying not null, encrypted_password character varying not null, created_at TIMESTAMP not null, updated_at TIMESTAMP, CONSTRAINT user_records_pkey%SUFFIX% PRIMARY KEY (id) );
This script will auto refresh with any new valid user registration. So you query this table to get your users out of database !
3. You want transactional actions on your data
I don't know much about NoSQL, but when i handle my write, i want it to be atomic. That means, whenever any error happens, no new write to my database at all.
All or Nothing
RDBMS will help you achieve this , simple and efficient !
4. You want a strong query language to get efficiently whatever data you have.
So now, you have your users table to query. You want to deliver it to client, to your users, your customers.
You should use GraphQL for that purpose ! More specifically, there're many frameworks out there could help you
GraphQLify your database for querying. One example is Hasura , all you need to do is run hasura and point it to your RDBMS (PostgreSQL in this case)
No more codebase needed to achieve that
The nice thing is that, your client can now use GraphQL query language to query your data out of your RDBMS !
5. You want to take benefit of a RDBMS to manage all security issues.
After you already deliver your data to users, the last thing to do, is security.
With Hasura (or any other framework like that), it'll allow us to enforce permission rule on your database in a very flexible way !
No need to put it in your code base !
I've heard many dramas about RDBMS, everything is a tradeoff, the most important things you could do as a developer, is choosing the right tool for the job. No silver bullet here.
Happy coding !
Top comments (2)
It's advisable to do this in your code as well, so you can tell the user "This email has already been used". Otherwise, a database exception will be thrown with a message like "Integrity constraint violation", which you can't show to your users and you can't act on.
Ah, i see your point. In my case, i would just catch the piece of code and handle it like you said and return nice error message to the call site.