1. 38
  1. 21

    I use this a lot in conjunction with a json blob. This allows for document storage without super strict structure enforcement:

    Something like:

    CREATE TABLE releases (
      "raw" BLOB,
      "uri" TEXT GENERATED ALWAYS AS (json_extract(raw, '$.uri')) VIRTUAL,
      "date" timestamp GENERATED ALWAYS AS (json_extract(raw, '$.date')) VIRTUAL,
      "name" TEXT GENERATED ALWAYS AS (json_extract(raw, '$.name')) VIRTUAL
    1. 5

      I’ve been doing this in Postgres and it’s worked out well. We had a source of JSON events and wanted to store them for analysis, but didn’t know ahead of time what indices we’d need. We just knew we’d probably want the ability to do that analysis in the future.

      So we ingested everything as jsonb and now I can add generated columns and indices retroactively where needed for query performance. So far, no regrets.

      1. 2

        Indeed, the fact that both sqlite and postgres support virtual columns is great that it allows a path for minimal scaling up from the former to the later if desired.

      2. 3

        What is stored if the field is missing?

        1. 4

          NULL is stored:

          sqlite> select json_extract('{}', '$.uri') is null;
      3. 2

        Do not embed your business logic in the database.

        1. 19

          Only a Sith deals in absolutes. (:

          What about tools like PostgREST that allow you to embed all the business logic in the database?

          1. 9

            Why not?

            1. 8

              There are conflicting opinions on this.

              Some people think they should push everything into the DB and the UI basically equates to pretty eye candy and user experience(UX).

              Some people prefer a middle ground, and some people think all the business logic should live outside the DB.

              I personally don’t think there is any one right answer. I think it depends on where the API boundary is, which mostly depends on the application.

              If you have a need/desire to give end users DB access, then you almost certainly want a lot(if not all) business logic in the DB.

              If you treat the DB as nothing more than a convenient place to store some data, then putting business logic there is stupid.

              Most databases can support any/all of these options.

              1. 5

                zie’s answer is good. Another perspective: single responsibility for microservices.

                You don’t reimplement logic in multiple apps writing to the same store. So either you have a microservice dedicated to providing the API needed while remaining the single-source-of-truth, or you have business logic in the RDBMS so that the RDBMS effectively embeds that microservice.

                And then it’s a question of available skillsets of your staff, and how many people are good at debugging stored procedures vs issuing distributed traces, etc.

                It’s all trade-offs.

                1. 5

                  so that the RDBMS effectively embeds that microservice

                  That’s an awesome way to describe the approach that I haven’t heard before. It sounds like it could also be used to make some db engineers twitch when I refer to their stored procedures repo as a microservice. Great! :-)

                  1. 4

                    With a custom wire RPC format no less.

                  2. 1

                    I agree with this as a valid perspective.

                2. 4

                  The example given wasn’t really business logic, right? Just an abstraction over normalization?

                  1. 3

                    It’s not business logic, it’s pretty much like a database view, which certainly belongs into a database. I think abstractions for data belong close to the data. That also means that you can change, replace, the thing that interacts with the data, the actual business logic.

                    One can be cut by this easily, when designing database schemas to close to a framework, ORM, etc. only for things to change and having a horrible migration path, potentially having to replicate very unidiomatic behavior.

                    So I’d argue, data and data representation doesn’t belong in your business logic.

                    Or at least for keeping logic and data separate, which you don’t do if you essentially build parts of the schema or views in your business logic.

                    1. 2

                      …unless you can put all the logic in there. …or, you need to do selects on that data. …or, other business units have views into your schema …or, you have compliance requirements to do so