8000 Return Relationship Metadata for Offline Support by stnguyen90 · Pull Request #5556 · appwrite/appwrite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Return Relationship Metadata for Offline Support #5556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

stnguyen90
Copy link
Contributor
@stnguyen90 stnguyen90 commented May 19, 2023

What does this PR do?

If the client has offline persistency enabled (indicated by the X-SDK-Offline request header), the server will return the collection's relationships attributes so that while the client is offline, it knows where to store related data.

The header will include something like:

[
  {
    "databaseId": "many-to-one",
    "collectionId": "level1",
    "attributes": {
      "level2": {
        "key": "level2",
        "type": "relationship",
        "relatedCollection": "level2",
        "relationType": "manyToOne",
        "twoWay": true,
        "twoWayKey": "level1",
        "onDelete": "setNull",
        "side": "parent"
      }
    }
  },
  {
    "databaseId": "many-to-one",
    "collectionId": "level3",
    "attributes": {
      "level2": {
        "key": "level2",
        "type": "relationship",
        "relatedCollection": "level2",
        "relationType": "manyToOne",
        "twoWay": true,
        "twoWayKey": "level3",
        "onDelete": "setNull",
        "side": "child"
      },
      "level4": {
        "key": "level4",
        "type": "relationship",
        "relatedCollection": "level4",
        "relationType": "manyToOne",
        "twoWay": true,
        "twoWayKey": "level3",
        "onDelete": "setNull",
        "side": "parent"
      }
    }
  },
  {
    "databaseId": "many-to-one",
    "collectionId": "level2",
    "attributes": {
      "level1": {
        "key": "level1",
        "type": "relationship",
        "relatedCollection": "level1",
        "relationType": "manyToOne",
        "twoWay": true,
        "twoWayKey": "level2",
        "onDelete": "setNull",
        "side": "child"
      },
      "level3": {
        "key": "level3",
        "type": "relationship",
        "relatedCollection": "level3",
        "relationType": "manyToOne",
        "twoWay": true,
        "twoWayKey": "level2",
        "onDelete": "setNull",
        "side": "parent"
      }
    }
  }
]

Test Plan

TODO

Related PRs and Issues

TBD

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

@stnguyen90 stnguyen90 changed the base branch from master to 1.4.x May 19, 2023 00:51
@stnguyen90 stnguyen90 requested a review from abnegate May 19, 2023 00:51
@stnguyen90 stnguyen90 marked this pull request as draft May 19, 2023 00:51
If the client has offline persistency enabled (indicated by the
X-SDK-Offline request header), the server will return the collection's
relationships attributes so that while the client is offline, it knows
where to store related data.
@stnguyen90 stnguyen90 force-pushed the feat-relationship-metadata branch from 00e2f48 to dbdd7c7 Compare May 19, 2023 01:02
*
* Returns true if the user has access to the collection
*/
function getCollectionRelationships(Document $collection, bool $mode, Database $dbForProject, int $depth, array &$collections): bool
Copy link
8000
Member

Choose a reason for hiding this comment

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

Can we set a resource and inject it instead of using a global function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eldadfux, considering your comment over #5587 (comment), do you not want us to set any global functions as a resource? Some other cases where we have functions we want to re-use between endpoints are:

  1. create user
  2. createAttribute
  3. updateAttribute
  4. avatarCallback
  5. some graphql methods

Copy link
Member

Choose a reason for hiding this comment

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

There are a few places where we compromised on this as we didn't had any other reasonable choice. Me and @lohanidamodar are working on a longer term solution to define routes as classes. This will allow us to better reuse or extend routes logic.

8000
}
}

if ($depth + 1 > Database::RELATION_MAX_DEPTH) {
Copy link
Member

Choose a reason for hiding this comment

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

Might be clearer to use >= instead of $depth + 1

$attributes = [];
foreach ($relationshipAttributes as $attribute) {
/** @var Document $attribute */
$relatedCollection = Authorization::skip(fn() => $dbForProject->getDocument('database_' . $collection->getAttribute('databaseInternalId'), $attribute->getAttribute('relatedCollection')));
Copy link
Member

Choose a reason for hiding this comment

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

Instead of skipping auth, can we utilize the permission check at the database level and check if $relatedCollection->isEmpty(), then skip the recursion if it is? Then we can remove the manual check at the start of this function too

@stnguyen90 stnguyen90 changed the title Feat relationship metadata Return Relationship Metadata for Offline Support Aug 1, 2023
@eldadfux eldadfux closed this Aug 7, 2023
< 74E0 a class="d-inline-block" data-test-selector="pr-timeline-events-actor-avatar" data-hovercard-type="user" data-hovercard-url="/users/TorstenDittmann/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="/TorstenDittmann">@TorstenDittmann TorstenDittmann deleted the feat-relationship-metadata branch September 10, 2023 16:15
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
0