Skip to content

Conversation

rkistner
Copy link
Contributor

The substring function roughly matches the SQLite substring function (notably different from the JavaScript equivalent). It is also similar to the Postgres substring function, but different when a negative start or length is specified.

The json_keys function has no direct equivalent in Postgres or SQLite. Postgres has a similar json_object_keys, but that returns each key as a separate row, while we return the keys as a JSON array.

Use cases:

substring:

-- Parameter query - filter by date value
-- substring(created_at, 1, 10) returns the date portion of the timestamp
select id as post_id from posts where substring(created_at, 1, 10) in request.jwt() -> 'sync_dates'

json_keys:

-- Data query - permissions_json: {[user_id]: 'read' | 'write' | 'admin'}
-- This is still limited - we can only operate on the keys, and not the values
select * from items where bucket.user_id in json_keys(permissions_json)

-- even better would be something like this, but that is significantly more complex to implement:
select * from items where (permissions_json ->> bucket.user_id) = 'read'
select * from items where 'read' in (permissions_json ->> bucket.user_id)

Copy link

changeset-bot bot commented Oct 17, 2024

🦋 Changeset detected

Latest commit: 0f90b02

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@powersync/service-sync-rules Minor
@powersync/service-core Patch
@powersync/service-image Patch
test-client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@rkistner rkistner marked this pull request as ready for review October 17, 2024 08:31
Copy link
Collaborator

@stevensJourney stevensJourney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome. It will help a lot with defining sync rules.

@guillempuche
Copy link

Cooool!!!

@rkistner rkistner merged commit 0ab67b0 into main Oct 17, 2024
8 checks passed
@rkistner rkistner deleted the expand-functions branch October 17, 2024 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants