Homepage

Liquid tags for basic CRUD operations

Last edit:

Note

This roadmap topic includes a suggestion for easily creating/updating various records. As there were many issues with this solution we planned, we came up with something much better: the new graphql tag and the possibility to use GraphQL mutations without having to specify a form configuration. This allows you to seed data directly using the GraphQL editor launched via the pos-cli. It has features like suggestions, syntax highlighting, type checking, error handling, autocomplete and so much more — none of which would be provided by Liquid tags.

This section describes simplification for executing basic CRUD operations.

Problem

In order to update or create resources in a callback, you need to duplicate work by not only writing a GraphQL mutation, but also a Form. This makes the code verbose and error-prone, as the same parameters need to be configured in two different files. It should be possible to do simple Create, Read, Update, Delete [CRUD] operations without such overhead.

Solution

Create a new User/Customization

It would be much easier to write something like this in a callback:


{% parse_json car_properties %}
{
  "model": "a4",
  "production_year": "2002"
}
{% endparse_json %}
{% create 'car', user_id: context.current_user.id, properties: car_properties, result_name: 'car' %}
{{ car.properties.production_year }} # => "2002"

Update one record

Assuming you have just created car, like in the previous example, you could then update it like this:


{% parse_json car_properties %}
{
  "production_year": "2003"
}
{% endparse_json %}
{% update 'car', id: car.id, properties: car_properties, result_name: 'car' %}
{{ car.properties.production_year }} # => "2003" (updated)
{{ car.properties.model }} # => "a4" - note we are not changing this

Delete

Deletion seems to be quite simple:


{% delete 'car', id: car.id result_name: 'car' %}

Increment/Decrement value

This issue is currently unsolved. Let's say you want to create a counter cache property. Currently, it is necessary to write a GraphQL query to take the total count of something and then write a GraphQL mutation to update the value. This is prone to various race condition issues, especially if done in an async callback. It would be easier and more reliable to be able to do it like this:


{% update_number 'car', id: car.id, property: 'number_of_views', value: 1 %}

This might not be the best name, suggestions are welcome.

Update multiple records at once

This is another issue which currently is solved in a non-ideal way. We are not yet sure how to ideally solve this, but we can improve this greatly, if we allow passing ids to update tag described above. We could get all ids of objects to be updated via GraphQL and then would update a bunch of records the same way as we do with one record.

Fetch object(s)

It would be a great shortcut for various scenarios if you could just write:


{% fetch 'car', id: context.params.slug2, result_name: 'car' %}

This would be especially useful in, for example, Email Notifications. If you want more customized or nested data, then fallback to current way via GraphQL would be the way to go.

Questions?

We are always happy to help with any questions you may have.

contact us