Web Developer
I would like to know how you do this in Appwrite Databases. Any ideas?
Votes
0
Replies
7
Participants
2
Messages
8
Web Developer
I would like to know how you do this in Appwrite Databases. Any ideas?
For that, firstly you'll have to set row security via the Console, then you can add the specific permissions via your code .
Can you tell which language or framework you're using?
Web Developer
i use nextjs
Web Developer
thanks
Web Developer
could you provide me an example?
Sure, but since I don't know whether you're using TablesDB or earlier Collection based DB, I'll give you the example for both:
TablesDB you can do something like this:import { Client, TablesDB, ID, Permission, Role } from "appwrite";
const client = new Client() .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!) .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID!);
const tablesDB = new TablesDB(client);
export async function createANewRecord(userId: string) { return await tablesDB.createRow( "DATABASE_ID", "TABLE_ID", ID.unique(), { createdAt: Date.now() }, [ Permission.read(Role.user(userId)), Permission.update(Role.user(userId)), Permission.delete(Role.user(userId)) ] );}Collections DB you can do something like this:import { Client, Databases, ID, Permission, Role } from "appwrite";
const client = new Client() .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!) .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID!);
const databases = new Databases(client);
export async function createANewRecord(userId: string) { return await databases.createDocument( "DATABASE_ID", "COLLECTION_ID", ID.unique(), { createdAt: Date.now() }, [ Permission.read(Role.user(userId)), Permission.update(Role.user(userId)), Permission.delete(Role.user(userId)) ] );}This was just an example, instead of that createdAt: Date.now() you can put any other column name + value pair as per your DB schema. Also, you can set different permissions too as per your choice & requirement.
Web Developer
ok, then i am well-prepared. thank you (i am using the new approach: table-based databases)