|
| 1 | +--- |
| 2 | +title: Low-Code Backend Solution for Refine.dev Using Prisma and ZenStack |
| 3 | +description: This post introduces how to build a fully-secured backend API for refine.dev projects with minimum code using Prisma and ZenStack. |
| 4 | +tags: [refine, react, authorization] |
| 5 | +authors: yiming |
| 6 | +date: 2024-06-27 |
| 7 | +image: ./cover.png |
| 8 | +--- |
| 9 | + |
| 10 | +# Low-Code Backend Solution for Refine.dev Using Prisma and ZenStack |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +[Refine.dev](https://refine.dev/) is a very powerful and popular React-based framework for building web apps with less code. It focuses on providing high-level components and hooks to cover common use cases like authentication, authorization, and CRUD. One of the main reasons for its popularity is that it allows easy integration with many different kinds of backend systems via a flexible adapter design. |
| 15 | + |
| 16 | +This post will focus on the most important type of integration: database CRUD. I'll show how easy it is, with the help of Prisma and ZenStack, to turn your database schema into a fully secured API that powers your refine app. You'll see how we start by defining the data schema and access policies, derive an automatic CRUD API from it, and finally integrate with the Refine app via a "Data Provider." |
| 17 | + |
| 18 | +<!-- truncate --> |
| 19 | + |
| 20 | +## A quick overview of the tools |
| 21 | + |
| 22 | +### Prisma |
| 23 | + |
| 24 | +[Prisma](https://www.prisma.io) is a modern TypeScript-first ORM that allows you to manage database schemas easily, make queries and mutations with great flexibility, and ensure excellent type safety. |
| 25 | + |
| 26 | +### ZenStack |
| 27 | + |
| 28 | +[ZenStack](https://zenstack.dev) is a toolkit built above Prisma that adds access control, automatic CRUD web API, etc. It unleashes the ORM's full power for full-stack development. |
| 29 | + |
| 30 | +### Auth.js |
| 31 | + |
| 32 | +[Auth.js](https://authjs.dev/) (successor of NextAuth) is a flexible authentication library that supports many authentication providers and strategies. Although you can use many external services for auth, simply storing everything inside your database is often the easiest way to get started. |
| 33 | + |
| 34 | +## A blogging app |
| 35 | + |
| 36 | +I'll use a simple blogging app as an example to facilitate the discussion. We'll first focus on implementing the authentication and CRUD with essential access control and then expand to more advanced topics. |
| 37 | + |
| 38 | +You can find the link to the completed project's GitHub repo at the end of the post. |
| 39 | + |
| 40 | +### Scaffolding the app |
| 41 | + |
| 42 | +The `create-refine-app` CLI provides several handy templates to scaffold a new app. We'll use the "Next.js" one so that we can easily contain both the frontend and backend in the same project. Most of the ideas in this post can be applied to a standalone backend project as well. |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +We also need to install Prisma and NextAuth: |
| 47 | + |
| 48 | +```bash |
| 49 | +npm install --save-dev prisma |
| 50 | +npm install @prisma/client next-auth@beta |
| 51 | +``` |
| 52 | + |
| 53 | +Finally, we'll create the database schema for our app: |
| 54 | + |
| 55 | +```zmodel title="prisma/schema.prisma" |
| 56 | +datasource db { |
| 57 | + provider = "sqlite" |
| 58 | + url = "file:./dev.db" |
| 59 | +} |
| 60 | +
|
| 61 | +generator client { |
| 62 | + provider = "prisma-client-js" |
| 63 | +} |
| 64 | +
|
| 65 | +model User { |
| 66 | + id String @id() @default(cuid()) |
| 67 | + name String? |
| 68 | + email String? @unique() |
| 69 | + emailVerified DateTime? |
| 70 | + image String? |
| 71 | + createdAt DateTime @default(now()) |
| 72 | + updatedAt DateTime @updatedAt() |
| 73 | + accounts Account[] |
| 74 | + sessions Session[] |
| 75 | + password String |
| 76 | + posts Post[] |
| 77 | +} |
| 78 | +
|
| 79 | +model Post { |
| 80 | + id String @id() @default(cuid()) |
| 81 | + createdAt DateTime @default(now()) |
| 82 | + updatedAt DateTime @updatedAt() |
| 83 | + title String |
| 84 | + content String |
| 85 | + status String @default("draft") |
| 86 | + author User @relation(fields: [authorId], references: [id]) |
| 87 | + authorId String |
| 88 | +} |
| 89 | +
|
| 90 | +model Account { |
| 91 | + ... |
| 92 | +} |
| 93 | +
|
| 94 | +model Session { |
| 95 | + ... |
| 96 | +} |
| 97 | +
|
| 98 | +model VerificationToken { |
| 99 | + ... |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +:::info |
| 104 | +The `Account`, `Session`, and `VerificationToken` models are [required by Auth.js](https://authjs.dev/getting-started/adapters/prisma#schema). |
| 105 | +::: |
| 106 | + |
| 107 | +### Building authentication |
| 108 | + |
| 109 | +The focus of this post will be data access and access control. However, they are only possible with an authentication system in place. We'll use simple credential-based authentication in this app. The implementation involves creating an Auth.js configuration, installing an API route to handle auth requests, and implementing a Refine "Authentication Provider". |
| 110 | + |
| 111 | +I won't elaborate on the details of this part, but you can find the completed code [here](https://github.com/ymc9/refine-nextjs-zenstack/tree/main/src/providers/auth-provider). It should get the registration, login, and session management parts working. |
| 112 | + |
| 113 | +### Set up access control |
| 114 | + |
| 115 | +There are many ways to implement access control. People typically put the check in the API layer with imperative code. ZenStack offers a unique and powerful way to do it declaratively inside the database schema. Let's see how it works. |
| 116 | + |
| 117 | +First, let's initialize the project for ZenStack: |
| 118 | + |
| 119 | +```bash |
| 120 | +npx zenstack@latest init |
| 121 | +``` |
| 122 | + |
| 123 | +It'll install a few dependencies and copies over the `prisma/schema.prisma` file to `/schema.zmodel`. ZModel is a superset of Prisma Schema Language that adds more features like access control. |
| 124 | + |
| 125 | +Next, we'll add policy rules to the schema: |
| 126 | + |
| 127 | +```zmodel title="schema.zmodel" |
| 128 | +model User { |
| 129 | + ... |
| 130 | +
|
| 131 | + // everybody can signup |
| 132 | + @@allow('create', true) |
| 133 | +
|
| 134 | + // full access by self |
| 135 | + @@allow('all', auth() == this) |
| 136 | +} |
| 137 | +
|
| 138 | +
|
| 139 | +model Post { |
| 140 | + ... |
| 141 | +
|
| 142 | + // allow read for all signin users |
| 143 | + @@allow('read', auth() != null && status == 'published') |
| 144 | +
|
| 145 | + // full access by author |
| 146 | + @@allow('all', author == auth()) |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +As you can see, the overall schema still looks very similar to the original Prisma schema. The `@@allow` directive defines access control rules. The `auth()` function returns the current authenticated user. We'll see how it's connected with the authentication system next. |
| 151 | + |
| 152 | +The most straightforward way to use ZenStack is to create an "enhancement" wrapper around the Prisma client. First, run the CLI to generate JS modules that support the enforcement of policies: |
| 153 | + |
| 154 | +```bash |
| 155 | +npx zenstack generate |
| 156 | +``` |
| 157 | + |
| 158 | +Then, you can call the `enhance` API to create an enhanced PrismaClient. |
| 159 | + |
| 160 | +```ts |
| 161 | +const session = await auth(); |
| 162 | +const user = session?.user?.id ? { id: session.user.id } : undefined; |
| 163 | +const db = enhance(prisma, { user }); |
| 164 | +``` |
| 165 | + |
| 166 | +Besides the `prisma` instance, the `enhance` function also takes a second argument that contains the current user. The user object provides value to the `auth()` function call in the schema at runtime. |
| 167 | + |
| 168 | +The enhanced PrismaClient has the same API as the original one, but it will enforce the policy rules automatically for you. |
| 169 | + |
| 170 | +### Automatic CRUD API |
| 171 | + |
| 172 | +Having the ORM instance enhanced with access control capabilities is great. We can now implement CRUD APIs without writing imperative authorization code as long as we use the enhanced client. However, wouldn't it be even cooler if the CRUD APIs were automatically derived from the schema? |
| 173 | + |
| 174 | +ZenStack makes it possible by providing a set of server adapters for popular Node.js frameworks. Using it with Next.js is easy. You'll only need to create an API route handler: |
| 175 | + |
| 176 | +```ts title="src/app/model/[...path]/route.ts" |
| 177 | +import { auth } from '@/auth'; |
| 178 | +import { prisma } from '@/db'; |
| 179 | +import { enhance } from '@zenstackhq/runtime'; |
| 180 | +import { NextRequestHandler } from '@zenstackhq/server/next'; |
| 181 | + |
| 182 | +// create an enhanced Prisma client with user context |
| 183 | +async function getPrisma() { |
| 184 | + const session = await auth(); |
| 185 | + const user = session?.user?.id ? { id: session.user.id } : undefined; |
| 186 | + return enhance(prisma, { user }); |
| 187 | +} |
| 188 | + |
| 189 | +const handler = NextRequestHandler({ getPrisma, useAppDir: true }); |
| 190 | + |
| 191 | +export { |
| 192 | + handler as DELETE, |
| 193 | + handler as GET, |
| 194 | + handler as PATCH, |
| 195 | + handler as POST, |
| 196 | + handler as PUT, |
| 197 | +}; |
| 198 | +``` |
| 199 | + |
| 200 | +You then have a set of CRUD APIs served at "/api/model/[Model Name]/...". The APIs closely resemble PrismaClient's API: |
| 201 | + - `/api/model/post/findMany` |
| 202 | + - `/api/model/post/create` |
| 203 | + - ... |
| 204 | + |
| 205 | +You can find the detailed API specification [here](https://zenstack.dev/docs/reference/server-adapters/api-handlers/rpc). |
| 206 | + |
| 207 | +### Implementing a data provider |
| 208 | + |
| 209 | +We've got the backend APIs ready. Now, the only missing piece is a Refine "Data Provider", which talks to the API to fetch and update data. The following code snippet shows how the `getList` method is implemented. Refine's data provider's data structure is conceptually very close to Prisma, and we only need to do some lightweighted translation: |
| 210 | + |
| 211 | +```ts title="src/providers/data-provider/index.ts" |
| 212 | +export const dataProvider: DataProvider = { |
| 213 | + |
| 214 | + getList: async function <TData extends BaseRecord = BaseRecord>( |
| 215 | + params: GetListParams |
| 216 | + ): Promise<GetListResponse<TData>> { |
| 217 | + const queryArgs: any = {}; |
| 218 | + |
| 219 | + // filtering |
| 220 | + if (params.filters && params.filters.length > 0) { |
| 221 | + const filters = params.filters.map((filter) => |
| 222 | + transformFilter(filter) |
| 223 | + ); |
| 224 | + if (filters.length > 1) { |
| 225 | + queryArgs.where = { AND: filters }; |
| 226 | + } else { |
| 227 | + queryArgs.where = filters[0]; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + // sorting |
| 232 | + if (params.sorters && params.sorters.length > 0) { |
| 233 | + queryArgs.orderBy = params.sorters.map((sorter) => ({ |
| 234 | + [sorter.field]: sorter.order, |
| 235 | + })); |
| 236 | + } |
| 237 | + |
| 238 | + // pagination |
| 239 | + if ( |
| 240 | + params.pagination?.mode === 'server' && |
| 241 | + params.pagination.current !== undefined && |
| 242 | + params.pagination.pageSize !== undefined |
| 243 | + ) { |
| 244 | + queryArgs.take = params.pagination.pageSize; |
| 245 | + queryArgs.skip = |
| 246 | + (params.pagination.current - 1) * params.pagination.pageSize; |
| 247 | + } |
| 248 | + |
| 249 | + // call the API to fetch data and count |
| 250 | + const [data, count] = await Promise.all([ |
| 251 | + fetchData(params.resource, '/findMany', queryArgs), |
| 252 | + fetchData(params.resource, '/count', queryArgs), |
| 253 | + ]); |
| 254 | + |
| 255 | + return { data, total: count }; |
| 256 | + }, |
| 257 | + |
| 258 | + ... |
| 259 | +}; |
| 260 | +``` |
| 261 | + |
| 262 | +With the data provider in place, we now have a fully working CRUD UI. |
| 263 | + |
| 264 | + |
| 265 | + |
| 266 | +You can sign up for two accounts and verify that the access control rules are working as expected - draft posts are only visible to the author. |
| 267 | + |
| 268 | +### Bonus: guarding UI with permission checker |
| 269 | + |
| 270 | +Let's add one more challenge to the problem: the users of our app will have two roles: |
| 271 | + |
| 272 | +- Reader: can only read published posts |
| 273 | +- Writer: can create new posts |
| 274 | + |
| 275 | +Our schema needs to be updated accordingly: |
| 276 | + |
| 277 | +```zmodel title="schema.zmodel" |
| 278 | +model User { |
| 279 | + ... |
| 280 | +
|
| 281 | + role String @default('Reader') |
| 282 | +} |
| 283 | +
|
| 284 | +model Post { |
| 285 | + ... |
| 286 | +
|
| 287 | + // allow read for all signin users |
| 288 | + @@allow('read', auth() != null && status == 'published') |
| 289 | +
|
| 290 | + // allow "Writer" users to create |
| 291 | + @@allow('create', auth().role == 'Writer') |
| 292 | +
|
| 293 | + // full access by author |
| 294 | + @@allow('read,update,delete', author == auth()) |
| 295 | +} |
| 296 | +``` |
| 297 | + |
| 298 | +Now, if you try to create a new post with a "Reader" account, you'll see the following error: |
| 299 | + |
| 300 | + |
| 301 | + |
| 302 | +The operation is denied correctly according to the rules. However, it's not an entirely user-friendly experience. It'd be nice to prevent the "Create" button from appearing in the first place. This can be achieved by combining two additional features from Refine and ZenStack: |
| 303 | + |
| 304 | +- Refine allows you to implement an "Access Control Provider" to verdict whether the current user has permission to perform an action. |
| 305 | +- ZenStack's enhanced PrismaClient has an extra `check` API for inferring permission based on the policy rules. The `check` API is also available in the automatic CRUD API. |
| 306 | + |
| 307 | +:::info |
| 308 | +ZenStack's `check` API doesn't query the database. It's based on logical inference from the policy rules. See more details [here](https://zenstack.dev/docs/guides/check-permission). |
| 309 | +::: |
| 310 | + |
| 311 | +Let's see how these two pieces are put together. First, implement an `AccessControlProvider`: |
| 312 | + |
| 313 | +```ts title="src/providers/access-control-provider/index.ts" |
| 314 | +export const accessControlProvider: AccessControlProvider = { |
| 315 | + can: async ({ resource, action }: CanParams): Promise<CanReturnType> => { |
| 316 | + if (action === 'create') { |
| 317 | + // make a request to "/api/model/:resource/check?q={operation:'create'}" |
| 318 | + let url = `/api/model/${resource}/check`; |
| 319 | + url += |
| 320 | + '?q=' + |
| 321 | + encodeURIComponent( |
| 322 | + JSON.stringify({ |
| 323 | + operation: 'create', |
| 324 | + }) |
| 325 | + ); |
| 326 | + const resp = await fetch(url); |
| 327 | + if (!resp.ok) { |
| 328 | + return { can: false }; |
| 329 | + } else { |
| 330 | + const { data } = await resp.json(); |
| 331 | + return { can: data }; |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + return { can: true }; |
| 336 | + }, |
| 337 | + |
| 338 | + options: { |
| 339 | + buttons: { |
| 340 | + enableAccessControl: true, |
| 341 | + hideIfUnauthorized: false, |
| 342 | + }, |
| 343 | + queryOptions: {}, |
| 344 | + }, |
| 345 | +}; |
| 346 | +``` |
| 347 | + |
| 348 | +Then, register the provider to the top-level `Refine` component: |
| 349 | + |
| 350 | +```tsx title="src/app/layout.tsx" |
| 351 | +<Refine |
| 352 | + accessControlProvider={ accessControlProvider } |
| 353 | + ... |
| 354 | +/> |
| 355 | +``` |
| 356 | + |
| 357 | +You'll immediately notice the difference that, with a "Reader" user, the "Create" button is grayed out and disabled. |
| 358 | + |
| 359 | + |
| 360 | + |
| 361 | +However, you can still directly navigate to the "/blog-post/create" URL to access the create form. We can prevent that by using Refine's `CanAccess` component to guard it: |
| 362 | + |
| 363 | +```tsx title="src/app/blog-post/create/page.tsx" |
| 364 | +<CanAccess |
| 365 | + resource="post" |
| 366 | + action="create" |
| 367 | + fallback={<div>Not Allowed</div>} |
| 368 | +> |
| 369 | + <Create ... /> |
| 370 | +</CanAccess> |
| 371 | +``` |
| 372 | + |
| 373 | +Mission accomplished! We've also done it elegantly without hard coding any permission logic in the UI. Everything about access control is still centralized in the ZModel schema. |
| 374 | + |
| 375 | +## Conclusion |
| 376 | + |
| 377 | +Refine.dev is a great tool for building complex UI without writing complex code. Combined with the superpowers of Prisma and ZenStack, we've now got a full-stack, low-code solution with excellent flexibility. |
| 378 | + |
| 379 | +--- |
| 380 | + |
| 381 | +The completed sample project is here: [https://github.com/ymc9/refine-nextjs-zenstack](https://github.com/ymc9/refine-nextjs-zenstack). |
0 commit comments