Back

[SOLVED] Database relationship with the User

  • 1
  • Databases
  • Accounts
  • Web
  • Users
Ayush_Parui
16 Aug, 2023, 14:08

I'm creating a web application, which will allow users to upload their projects, how to create the relationship between the user and the particular document the user is creating because after creating multiple users and documents I cannot find how will the user identify which document he/she has created

Please refer to the code below:

`import conf from "@/conf/config"; import { Client, Databases, Account, ID } from "appwrite";

type createProject = { name: string, description: string, logo: string, url: string }

const dbClient = new Client() dbClient.setEndpoint(conf.appwriteUrl).setProject(conf.appwriteProjectId);

export const db = new Databases(dbClient)

export class dbService{ async createProject({name, description, logo, url}: createProject) { try { const project = await db.createDocument(${conf.appwriteDbId},${conf.appwriteCollectionId}, ID.unique(), { name, description, logo, url }) } catch (error:any) { throw error } }

TypeScript
async getCurrentUser() {
    try {
        return account.get()
    } catch (error) {
        console.log("getcurrentUser error: " + error)
    }
} 

}

const dbServices = new dbService() export default dbServices`

TL;DR
Solution: The user can identify their created documents by including the user's unique ID in the document when it is created. This can be done by modifying the code to store the user's ID within the document. Additionally, when retrieving documents for a specific user, the documents can be filtered by the user's ID to ensure they only see their own documents.
joeyouss
16 Aug, 2023, 14:26

Hi, you can: Include the User ID in the Document. Whenever a user creates a document (or a project in your case), you should store the user's unique ID within that document. This way, you'll always know which user created which document.

Filter by User ID when Querying. When retrieving documents for a specific user, you can filter the documents by the user's ID to ensure they only see their own documents.

Ayush_Parui
16 Aug, 2023, 14:35

oh great, thanx.

Guille
16 Aug, 2023, 17:51

[SOLVED] Database relationship with the User

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more