11/10/2023 0 Comments Elixir ecto jsonb![]() If the database is being shared and moved to other sources there is a shape mismatch between our database and our in memory structures. I personally think its worthwhile to take pressure of your database wherever possible, but your situation might be different. We are also moving some computation of parsing these schemas into our Elixir code. If you are in a position to just use a document database for this problem you will shed a lot of complexity. In my example I am clearly trying to bypass SQL schemas and push the responsibility to the runtime. They mean that the schema is not enforced compile time but instead at runtime. ![]() When people say that document databases are schemaless they don’t mean there is no schema. In my implementation I am clearly trying to bend SQL to work similar to how document databases naturally model things. Add type as option to the PolymorphicType.New activity items can be added to the feed without needing changes to the database. ![]() We do need to make sure we have a good pagination strategy in place for this though.We only have to query one table rather than doing joins.Lets loop back on the original goals we had set for ourselves to see how we measure up. ![]() Instead we will define a polymorphic type.ĭefmodule TransactionFeedItem do use Ecto.Schema alias Ecto.Changeset schema "transaction_feed_items" do # shared fields field :amount, :decimal # polymorphic field field :data, PolymorphicType, interest : InterestEarnedTransaction, card : CardTransaction end def changeset ( % _MODULE_ Įlixir you never cease to amaze me! Did we make it? # They probably have some sort of schema, just not one we can enforce at the database level. For one, our unique transaction types are not just loose key value pairs. We could just define it as a :map and call it a day but there are some issues with that. Next, we need a way to have a field that can be any one of our transaction types. It feels safe to assume every transaction would have an amount so lets just go with that. We don’t want to be too aggressive on deciding what fields are shared between all records since we want to support unknown transaction types down the road. For all other fields we will use our polymorphic type. For all the fields we can confidently say will be on every record we will make columns. This one table will house all of our transaction records. To start our adventure we will need to create a schema for our database table. There are always tradeoffs and alternatives - I’ll discuss those briefly at the end.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |