Skip to content
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

Create 021-server-defined-attributes.md #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vermakhushboo
Copy link
Member

What does this PR do?

RFC to add optional server-defined attributes to user

Test Plan

(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work.)

Related PRs and Issues

(If this PR is related to any other PR or resolves any issue or related to any issue link all related PR and issues here.)

Have you read the Contributing Guidelines on issues?

(Write your answer here.)

### API Changes
<!-- Do we need new API endpoints? List and describe them and their API signatures -->
We need 2 new API endpoints in `databases.php` for creating and deleting server-defined attributes.
- POST `/v1/databases/:databaseId/collections/:collectionId/attributes/server`
Copy link

Choose a reason for hiding this comment

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

Lets also have endpoint for listing them, so we don't need to hard-code then on Console. Its better to have config for it in Appwrite or library.


## Summary
<!-- Describe the problem we want to solve and suggested solution in a few paragraphs -->
This RFC proposes the addition of optional server-defined attributes in all the project collections that can only be added by server and cannot be modified by the client. The user can add them to each collection through the API or console. Some useful sttributes can be `createdBy`, `updatedBy`, `ip` etc.
Copy link

Choose a reason for hiding this comment

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

Let's have exact list of all internal attributes we will have

Choose a reason for hiding this comment

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

it would also be good to clarify exactly what the behavior of each would be.

@byawitz
Copy link
Member

byawitz commented Mar 7, 2024

The idea, in general, would help a lot on the part of Appwrite-console administrators (That can be a new market title 😉) and would help in setting pre-defined field structures for validations.

One way of expanding it would be to add a default callback using a function or other collection attributes.
This way the field value will auto-populate when no value was inserted.

Example 1 - Next appointment date.
In this example the default value would come from a function while passing the current document to that function

POST https://cloud.appwrite/v1/databases/:databaseId/collections/:collectionId/attributes/server
Content-Type: application/json
origin: https://localhost
{
  "id"      : "next_appointment",
  "type"    : "datetime",
  "format"  : "",
  "size"    : 0,
  "required": false,
  "default" : "[FUNCTION_ID]",
  "array"   : false,
  "filters" : []
}

Then, the function would run when the default value is empty, something like 👇, for example.

module.exports = async (context) => {
  try {
    // Set another appointment in 90 days.
    const next = new Date(+new Date() + (84600 * 90 * 1000));
    
    return context.res.json({ value: next });
  } catch (e) {
    return context.res.json({ value: '' });
  }
};

Example 2 - Adding search attribute
In this example the default value would come from the rest of the fields while each %*% variable is an attribute name in the current collection

POST https://cloud.appwrite/v1/databases/:databaseId/collections/:collectionId/attributes/server
Content-Type: application/json
origin: https://localhost
{
  "id"      : "search",
  "type"    : "string",
  "format"  : "",
  "size"    : 512,
  "required": false,
  "default" : "%name% %email% %address% %country% %phone%",
  "array"   : false,
  "filters" : []
}

# Optional server-defined attributes

* Creator: Khushboo Verma
* Relevant Issues: N/A

Choose a reason for hiding this comment

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

Suggested change
* Relevant Issues: N/A
* Relevant Issues:
* https://github.com/appwrite/appwrite/issues/3623


We can allow users to create indexes for new attributes from the `createIndex` endpoint.

We need to add a check in `createDocument` and `updateDocument` endpoints to populate the values of these attributes if they exist for client-side requests.

Choose a reason for hiding this comment

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

We do updates at the utopia-php/database level as well so we might need to do it there instead. For example, updatedAt and createdAt are handled in utopia: https://github.com/utopia-php/database/blob/4199fe8f00f4e181c7782c4a6862845d591c1f03/src/Database/Database.php#L3050-L3051


## Summary
<!-- Describe the problem we want to solve and suggested solution in a few paragraphs -->
This RFC proposes the addition of optional server-defined attributes in all the project collections that can only be added by server and cannot be modified by the client. The user can add them to each collection through the API or console. Some useful sttributes can be `createdBy`, `updatedBy`, `ip` etc.

Choose a reason for hiding this comment

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

it would also be good to clarify exactly what the behavior of each would be.

@jasonetorres
Copy link

👀👀🔥 definitely checking this out

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.

5 participants