TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: How much logic should I keep at the database vs. app layer?

5 点作者 cliffordfajardo超过 3 年前
As a basis: DB validating the format of an email address: No DB validating the price of a product can't be negative: Yes Q1:How would you define the line between them? Q2:What are your favorite readings/resoucres on this topic?

3 条评论

gwbas1c超过 3 年前
&gt; validating the format of an email address<p>That&#x27;s input validation. If your database offers an email type, use it. Otherwise, this kind of validation happens mostly in the UI (or API layer) and only lightly elsewhere.<p>If you&#x27;re using a strongly-typed language, consider an &quot;email&quot; class instead of a string. You can safely pass around an &quot;email&quot; object and know that it&#x27;s a valid email address.<p>&gt; No DB validating the price of a product can&#x27;t be negative<p>Does your database have an unsigned int type? Does it have a currency type? Use it if you can, otherwise, you&#x27;ll need to enforce it in code.<p>Remember that you still need to validate your input! If you&#x27;re relying on your database to validate your input, you&#x27;ll either send arcane and cryptic errors to the user, or misinterpret unrelated database errors as user errors.<p>&gt; How would you define the line between them<p>It&#x27;s all based on what&#x27;s reasonable. If I have to invent weirdo gymnastics in my database to enforce something, it&#x27;s probably best to do in code.<p>What schemas do is provide guarantees about data that can be enforced, especially through upgrades. IE: This value isn&#x27;t null, and never will be null. This object (row) always has values in columns A, B, and C. This relationship between these rows is always valid because there&#x27;s a foreign key relationship. These values then get enforced through schema changes. It&#x27;s harder for edge cases to sneak in. (Compared with schemaless databases, where your code will need to handle data in outdated formats, or will need lots of edge cases for missing values.)<p>This allows your data to be predictable, so you don&#x27;t have to resort to lots of heuristics in your business logic about edge cases.<p>&gt; What are your favorite readings&#x2F;resoucres on this topic?<p>Experience. Hopefully you can discuss this with a more experienced team member.
评论 #30383345 未加载
AnnikaL超过 3 年前
Personally I prefer to keep most nontrivial validation at the app level. Application code is typically easier to change (redeploying is often easier than a database migration), and it also makes error handling easier.
floppydisc超过 3 年前
As a rule I generally keep most in the app layer after some annoying runins with too much logic at the database layer.<p>Simple validation, like describing that a price cannot be below 0, I keep in the database. Email validation I keep on the app level.<p>It allows for easier deployments, easier debugging and it is easier to migrate to a different database should the need arise.<p>However both works.