Enforce your free-plan limit in the database, not in the UI
Your free plan says "up to 10". Where does that 10 actually live? In most apps I have read, it lives in the interface: a button that disables itself at ten, a count in a store, maybe a check in the route handler that writes the row. That is a suggestion, not a limit. A second tab, a page left open since yesterday, a direct call to the API with a token from devtools, and the eleventh row is in the…
The source explains that the free plan of a release tracker allows up to 10 technologies to be followed. The author argues that enforcing this limit in the database, rather than the user interface, is more effective. This is because the database can immediately reject the action when the limit is reached, while UI-based enforcement may fail if multiple users attempt the action simultaneously or if there are issues with the client-side code.
The author details how they implemented the enforcement in Postgres using a trigger function that checks the number of follows for a user and raises an exception if the limit is exceeded. They also discuss the importance of using advisory locks to prevent race conditions when multiple users try to add a follow at the same time. The author explains that they used a custom SQL state code for the limit rejection, which is returned to the client as part of the error object.
This ensures that the error is displayed consistently to the user, without having to parse a message from the UI. The author concludes by noting that this approach makes it easy to track and handle limit rejections in the application.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.