Add options to Client.Attach and Client.Detach APIs
hackerwins opened this issue · 1 comments
What would you like to be added:
In some cases, users may prefer to store the original document content in a service DB rather than in Yorkie. This results in the document not adhering to the Single Source of Truth (SSOT) principle and causes duplication of the document between Yorkie and the service DB. Therefore, a separate policy is necessary to maintain consistency.
To address this scenario, it is proposed to add options to Client.Attach and Client.Detach and provide the following guideline:
The main idea is to consider Yorkie Document as the primary source of truth during editing and switch to the service DB as the primary source after editing is completed.
A. Start Document Editing:
- When editing begins, first fetch the key and content of the document stored in the service DB.
- Attach Yorkie Document using this key, with the
createIfNotExists: true
option, along with the content stored in the service DB. - If the document has already been created in Yorkie by another client, receive the content being edited, and if not created, create the Yorkie document using the content stored in the DB.
![Screenshot 2024-03-12 at 4 29 54 PM](https://private-user-images.githubusercontent.com/2059311/311980572-d7bbbaba-9931-47a0-8da8-898ba3bb1745.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTU5MjUxMjEsIm5iZiI6MTcxNTkyNDgyMSwicGF0aCI6Ii8yMDU5MzExLzMxMTk4MDU3Mi1kN2JiYmFiYS05OTMxLTQ3YTAtOGRhOC04OThiYTNiYjE3NDUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDUxNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA1MTdUMDU0NzAxWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZDFlNjA3NzE3ZjgyN2NmODk2MmE5OTM1YzgwZGY3MDk3NzExNDQzZjc3N2I3YjQwYTM5YmRmZjE2MDMzZWYyYyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.Eg0sKIDy3b_FEszZXh_WkjeW9b5olTxWI9mcDbwGwY4)
const doc = new yorkie.Document(`key`);
client.attach(doc, {
createIfNotExists: true,
initialContents: contents
});
B. Finish Document Editing:
- Upon completion of editing, store the document content in Yorkie in the service DB.
- When detaching, if no more clients are editing the document, delete the Yorkie document.
![Screenshot 2024-03-12 at 4 31 45 PM](https://private-user-images.githubusercontent.com/2059311/311981074-f0f9e069-d407-4797-acea-c03e6405b700.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTU5MjUxMjEsIm5iZiI6MTcxNTkyNDgyMSwicGF0aCI6Ii8yMDU5MzExLzMxMTk4MTA3NC1mMGY5ZTA2OS1kNDA3LTQ3OTctYWNlYS1jMDNlNjQwNWI3MDAucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDUxNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA1MTdUMDU0NzAxWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MGFiYjQ2MzE5NGIzZDM3MzdlNjZmZTllMTJiNzNiNzc4NmUzYzZmM2UxZjEzMWVlZTY2MDU0ZGM5YThjYjAwMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.w3zAVZ3kxf_fuWlDBcKJACYeZ9eUSC7pR_-jOU6cw_g)
client.detach(doc, { removeIfNotAttached: true });
Why is this needed:
- To provide users with the flexibility to store document content in a service DB while maintaining document consistency.