Next.js
Accessing Data

Accessing data

ROQ generated SDK on client is directly tight with the Prisma schema of the application. The generated SDK make developer easier to query data from ROQ BaaS database. All database operation such as read and write data can be done through useRoqClient() method.

Read data using SDK

In this example, we demonstrate how to retrieve and count reviews from BaaS using the Roq SDK.

const roqClient = useRoqClient()
 
useEffect(() => {
    const fetchReviewCount = async () =>{
	    const reviewCount = await roqClient.review.findManyWithCount({ orderBy: {
		    created_at: 'desc'
		  }
		})	
 
		console.log(`Reviews:`)
		console.log(reviewCount)
	}
	fetchReviewCount()
 
}, [roqClient])
 

The important thing to note that the code syntax for the data operation follows a Prisma-like pattern.

await roqClient.review.findManyWithCount()

The benefit of a Prisma-like pattern is easily understood due to its common format: roqClient.[entity].[operation].

JSON:API is utilized to create REST APIs in ROQ BaaS, resulting in JSON-format response data. For instance, the data response from our earlier code in the browser console:

review count data

Write data using SDK

To write or update data, we can use update method. For instance, to update review data with the review id f61176b6-ba89-4ec6-8840-1efffb7f51e6:

const updateReviewbyID = async () => {
    const updateResult = await roqClient.review.update({
	    data: { comment: "Highly recommended book" },
		    where: {
			    id: "f61176b6-ba89-4ec6-8840-1efffb7f51e6"
		}
	},)
}
   
updateReviewbyID()

The provided review id will be used to update the comment data.

update review data