Skip to content

Cannot restore Parse.File from aggregation query result #1456

@mtrezza

Description

@mtrezza

New Issue Checklist

Issue Description

A Parse.File is stored in the DB as only the filename. When retrieving raw data from the DB (e.g. via a MongoDB aggregation query), the file data is in some cases not - and cannot be reliably be - parsed by the Parse Server internals. This means that instead of a valid Parse.File, the only file data available is the filename, e.g. abc.txt. To convert this into a valid Parse.File with Parse.File.fromJSON(...), the URL needs to be provided as well. The URL is however is composed by the Parse Server storage adapter.

Effectively that means it's not possible to easily recreate a Parse.File from aggregate query data. At least in Cloud Code, it should be possible to easily recreate the URL without having to manually access the storage adapter's getFileLocation method.

Steps to reproduce

  1. Execute an aggregate query that returns a file which is not part of the class on which the query runs.
  2. File data is returned is only the file name, e.g. { file: 'abc.txt' }.
  3. The file URL is unknown, so Parse.File.fromJSON(...) cannot be used to recreate an already saved file.

#Actual Outcome
A file URL has to be composed by manually accessing the storage adapter.

Expected Outcome

Parse.File should provide a method that auto-composes the URL, for example:

let json = {
  "__type": "File",
  "name": "abc.txt",
}
const file = Parse.File.fromJSON(json, undefined, undefined, { composeUrl: true });

json = file.toJSON();

/*
json == {
  "__type": "File",
  "name": "abc.txt",
  "url": "https://localhost:1337/parse/files/appId/abc.txt"
}
*/

Alternative

Parse Server could parse the query results for specific markers to substitute an occurrence of a raw file object with an actual Parse.File object. But that may cause conceptual issues, see parse-community/parse-server#7868.

Hack

Return the file name with a key of a file field of the same class on which the aggregation query runs. This will make Parse Server convert it to a Parse File because the schema demands so. That hack may break anytime though.

Environment

Server

  • Parse Server version: 5.1.1

Client

  • Parse JS SDK version: 3.4.1

Logs

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    bounty:$50Bounty applies for fixing this issue (Parse Bounty Program)type:featureNew feature or improvement of existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions