Custom Types, Arrays and JSON
PowerSync is fully compatible with more advanced Postgres types.
Below you can find information on how to use them.
Custom Types
PowerSync treats custom type columns as text.
Postgres
Postgres allows developers to create custom data types for columns. Example of creating a custom type:
Sync Rules
Custom type columns are converted to Text by the PowerSync Service. A column of type location_address
, as defined above, would be synced to clients as the following string:
("1000 S Colorado Blvd.",Denver,CO,80211)
It is not currently possible to extract fields from custom types in sync rules, so the entire column must be synced as text.
Client SDK
Schema
Add your custom type column as a text()
column in your client-side schema definition:
Writing Changes
Write the entire updated column value using a string:
Arrays
PowerSync treats array columns as JSON text. This means that the SQLite JSON operators can be used on any Array columns.
Additionally, some helper methods such as array membership are available in Sync Rules.
Note: Native Postgres arrays, JSON arrays and JSONB arrays are effectively all equivalent in PowerSync.
Postgres
Array columns are defined in Postgres using the following syntax:
Sync Rules
Array columns are converted to text by the PowerSync Service. A text array as defined above would be synced to clients as the following string:
["00000000-0000-0000-0000-000000000000", "12345678-1234-1234-1234-123456789012"]
Array Membership
It’s possible to sync rows dynamically based on the contents of array columns using the IN
operator. For example:
See these additional details when using the IN
operator:
Client SDK
Schema
Add your array column as a text()
column in your client-side schema definition:
Writing Changes
Write the entire updated column value using a string:
Attention Supabase users: Supabase is able to handle writes with arrays, but you’ll have to do the conversion from string to array using jsonDecode
in the connector’s uploadData
function. The default implementation of theuploadData
function doesn’t handle the more complex ones like arrays automatically
JSON and JSONB
The PowerSync Service treats JSON and JSONB columns as text and provides many helpers for working with JSON in Sync Rules.
Note: Native Postgres arrays, JSON arrays and JSONB arrays are effectively all equivalent in PowerSync.
Postgres
JSON columns are represented as:
Sync Rules
PowerSync treats JSON columns as text and provides transformation functions in Sync Rules such as json_extract()
.
Client SDK
Schema
Add your JSON column as a text()
column in your client-side schema definition:
Writing Changes
The default implementation of uploadData
in our Supabase Flutter To-Do List Demo App doesn’t handle more complex types such as JSON objects automatically. Below is some example Dart code for writing JSON updates back to Supabase:
Bonus: Mashup
What if we had a column defined as an array of custom types, where a field in the custom type was JSON? Consider the below Postgres schema:
Was this page helpful?