Adding jsonb columns for custom fields

The use case I’m working with at the moment is a real-time collaborative editor, which involves storing a series of Changesets to a document; each Changeset holds a post_id, author_id, document length, and string representation of the changes. It would be feasible to store this information as JSON like so:

{
  post_id: 1,
  author_id: 2,
  length: 10,
  changes: ['1-5', 'world']
}

but putting it into a text representation

"1|2|10|['1-5', 'world']"

, or simply storing that JSON as text in the DB, doesn’t really work because I need to be able to search for rows by post_id:

PluginStoreRow.where(plugin_name: :my_plugin, key: :changeset).where("value ->>'post_id' = 1")
1 Like