diff --git a/README.md b/README.md index 309c0b927..8d1aa2338 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,9 @@ Parse Dashboard is a standalone dashboard for managing your [Parse Server](https - [Limitations](#limitations) - [CSV Export](#csv-export) - [Views](#views) + - [Data Sources](#data-sources) + - [Aggregation Pipeline](#aggregation-pipeline) + - [Cloud Function](#cloud-function) - [View Table](#view-table) - [Pointer](#pointer) - [Link](#link) @@ -1260,11 +1263,38 @@ This feature will take either selected rows or all rows of an individual class a ▶️ *Core > Views* -Views are saved queries that display aggregated data from your classes. Create a view by providing a name, selecting a class and defining an aggregation pipeline. Optionally enable the object counter to show how many items match the view. Saved views appear in the sidebar, where you can select, edit, or delete them. +Views are saved queries that display data in a table format. Saved views appear in the sidebar, where you can select, edit, or delete them. Optionally you can enable the object counter to show in the sidebar how many items match the view. > [!Caution] > Values are generally rendered without sanitization in the resulting data table. If rendered values come from user input or untrusted data, make sure to remove potentially dangerous HTML or JavaScript, to prevent an attacker from injecting malicious code, to exploit vulnerabilities like Cross-Site Scripting (XSS). +### Data Sources + +Views can pull their data from the following data sources. + +#### Aggregation Pipeline + +Display aggregated data from your classes using a MongoDB aggregation pipeline. Create a view by selecting a class and defining an aggregation pipeline. + +#### Cloud Function + +Display data returned by a Parse Cloud Function. Create a view specifying a Cloud Function that returns an array of objects. Cloud Functions enable custom business logic, computed fields, and complex data transformations. + +Cloud Function views can prompt users for text input and/or file upload when opened. Enable "Require text input" or "Require file upload" checkboxes when creating the view. The user provided data will then be available in the Cloud Function as parameters. + +Cloud Function example: + +```js +Parse.Cloud.define("myViewFunction", request => { + const text = request.params.text; + const fileData = request.params.fileData; + return processDataWithTextAndFile(text, fileData); +}); +``` + +> [!Note] +> Text and file data are ephemeral and only available to the Cloud Function during that request. Files are base64 encoded, increasing the data during transfer by ~33%. + ### View Table When designing the aggregation pipeline, consider that some values are rendered specially in the output table. diff --git a/src/dashboard/Data/Views/CloudFunctionInputDialog.react.js b/src/dashboard/Data/Views/CloudFunctionInputDialog.react.js new file mode 100644 index 000000000..e550d58e7 --- /dev/null +++ b/src/dashboard/Data/Views/CloudFunctionInputDialog.react.js @@ -0,0 +1,99 @@ +import Field from 'components/Field/Field.react'; +import FileInput from 'components/FileInput/FileInput.react'; +import Label from 'components/Label/Label.react'; +import Modal from 'components/Modal/Modal.react'; +import TextInput from 'components/TextInput/TextInput.react'; +import React from 'react'; + +export default class CloudFunctionInputDialog extends React.Component { + constructor(props) { + super(props); + this.state = { + textInput: '', + uploadedFile: null, + }; + } + + handleFileChange = (file) => { + this.setState({ uploadedFile: file }); + }; + + handleConfirm = () => { + const { requireTextInput, requireFileUpload } = this.props; + const params = {}; + + if (requireTextInput) { + params.text = this.state.textInput; + } + + if (requireFileUpload && this.state.uploadedFile) { + // For file uploads, we'll pass the raw file data + // The cloud function will receive this as base64 encoded data + const file = this.state.uploadedFile; + const reader = new FileReader(); + reader.onload = () => { + if (reader.result && typeof reader.result === 'string') { + params.fileData = { + name: file.name, + type: file.type, + size: file.size, + data: reader.result.split(',')[1], // Remove the data URL prefix + }; + } + this.props.onConfirm(params); + }; + reader.readAsDataURL(file); + } else { + this.props.onConfirm(params); + } + }; + + render() { + const { requireTextInput, requireFileUpload, onCancel } = this.props; + + // Check if we have all required inputs + const hasRequiredText = !requireTextInput || this.state.textInput.trim().length > 0; + const hasRequiredFile = !requireFileUpload || this.state.uploadedFile !== null; + const isValid = hasRequiredText && hasRequiredFile; + + return ( + + {requireTextInput && ( + } + input={ + this.setState({ textInput })} + placeholder="Enter text here..." + /> + } + /> + )} + {requireFileUpload && ( + } + input={ + + } + /> + )} + + ); + } +} diff --git a/src/dashboard/Data/Views/CreateViewDialog.react.js b/src/dashboard/Data/Views/CreateViewDialog.react.js index 745762c31..530cad2fc 100644 --- a/src/dashboard/Data/Views/CreateViewDialog.react.js +++ b/src/dashboard/Data/Views/CreateViewDialog.react.js @@ -1,11 +1,22 @@ +import Checkbox from 'components/Checkbox/Checkbox.react'; import Dropdown from 'components/Dropdown/Dropdown.react'; +import Option from 'components/Dropdown/Option.react'; import Field from 'components/Field/Field.react'; import Label from 'components/Label/Label.react'; import Modal from 'components/Modal/Modal.react'; -import Option from 'components/Dropdown/Option.react'; -import React from 'react'; import TextInput from 'components/TextInput/TextInput.react'; -import Checkbox from 'components/Checkbox/Checkbox.react'; +import React from 'react'; + +/** + * The data source types available for views. + * + * @param {string} query An aggregation pipeline query data source. + * @param {string} cloudFunction A Cloud Function data source. + */ +const DataSourceTypes = { + query: 'query', + cloudFunction: 'cloudFunction' +}; function isValidJSON(value) { try { @@ -22,17 +33,30 @@ export default class CreateViewDialog extends React.Component { this.state = { name: '', className: '', + dataSourceType: DataSourceTypes.query, query: '[]', + cloudFunction: '', showCounter: false, + requireTextInput: false, + requireFileUpload: false, }; } valid() { - return ( - this.state.name.length > 0 && - this.state.className.length > 0 && - isValidJSON(this.state.query) - ); + if (this.state.dataSourceType === DataSourceTypes.query) { + return ( + this.state.name.length > 0 && + this.state.className.length > 0 && + this.state.query.trim() !== '' && + this.state.query !== '[]' && + isValidJSON(this.state.query) + ); + } else { + return ( + this.state.name.length > 0 && + this.state.cloudFunction.trim() !== '' + ); + } } render() { @@ -43,7 +67,7 @@ export default class CreateViewDialog extends React.Component { icon="plus" iconSize={40} title="Create a new view?" - subtitle="Define a custom query to display data." + subtitle="Define a data source to display data." confirmText="Create" cancelText="Cancel" disabled={!this.valid()} @@ -51,9 +75,12 @@ export default class CreateViewDialog extends React.Component { onConfirm={() => onConfirm({ name: this.state.name, - className: this.state.className, - query: JSON.parse(this.state.query), + className: this.state.dataSourceType === DataSourceTypes.query ? this.state.className : null, + query: this.state.dataSourceType === DataSourceTypes.query ? JSON.parse(this.state.query) : null, + cloudFunction: this.state.dataSourceType === DataSourceTypes.cloudFunction ? this.state.cloudFunction : null, showCounter: this.state.showCounter, + requireTextInput: this.state.dataSourceType === DataSourceTypes.cloudFunction ? this.state.requireTextInput : false, + requireFileUpload: this.state.dataSourceType === DataSourceTypes.cloudFunction ? this.state.requireFileUpload : false, }) } > @@ -67,32 +94,56 @@ export default class CreateViewDialog extends React.Component { } /> } + label={