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 asjson_extract()
.
Client SDK
Schema Add your JSON column as atext
column in your client-side schema definition. For advanced update tracking, see Advanced Schema Options.
trackPreviousValues
enabled, compare the previous and new values to process only the changes you care about:
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: OperatorsClient SDK
Schema Add your array column as atext
column in your client-side schema definition. For advanced update tracking, see Advanced Schema Options.
trackPreviousValues
enabled, compare the previous and new values to process only the changes you care about:
Attention Supabase users: Supabase can handle writes with arrays, but you must convert from string to array using
jsonDecode
in the connector’s uploadData
function. The default implementation of uploadData
does not handle complex types like arrays automatically.Custom Types
PowerSync serializes custom types as text. For details, see types in sync rules.Postgres
Postgres allows developers to create custom data types for columns. For example:Sync Rules
Custom type columns are converted to text by the PowerSync Service. A column of typelocation_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 atext
column in your client-side schema definition. For advanced update tracking, see Advanced Schema Options.
trackPreviousValues
enabled, compare the previous and new values to process only the changes you care about:
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 following Postgres schema:Advanced Schema Options to Process Writes
With arrays and JSON fields, it’s common for only part of the value to change during an update. To make handling these writes easier, you can enable advanced schema options that let you track exactly what changed in each row—not just the new state.trackPreviousValues
(ortrackPrevious
in our JS SDKs): Access previous values for diffing JSON or array fields. Accessible later viaCrudEntry.previousValues
.trackMetadata
: Adds a_metadata
column for storing custom metadata. Value of the column is accessible later viaCrudEntry.metadata
.ignoreEmptyUpdates
: Skips updates when no data has actually changed.
These advanced schema options are available in the following SDK versions:
- Flutter v1.13.0
- React Native v1.20.1
- JavaScript/Web v1.20.1
- Kotlin Multiplatform v1.1.0
- Swift v1.1.0
- Node.js v0.4.0.