146 lines
3.2 KiB
Markdown
146 lines
3.2 KiB
Markdown
|
## Project setup
|
||
|
|
||
|
### Nestjs
|
||
|
|
||
|
Clone a Nest starter project
|
||
|
```bash
|
||
|
git clone https://github.com/nestjs/typescript-starter.git demo-app
|
||
|
cd demo-app
|
||
|
npm install
|
||
|
```
|
||
|
|
||
|
dependentele
|
||
|
```bash
|
||
|
npm install @zenstackhq/runtime @nestjs/jwt bcrypt @nestjs/graphql @nestjs/apollo @apollo/server graphql graphql-scalars type-graphql
|
||
|
npm install prisma zenstack @types/bcrypt typegraphql-prisma-nestjs --save-dev
|
||
|
```
|
||
|
|
||
|
### Prisma
|
||
|
|
||
|
Initializam stack-ul Prisma
|
||
|
```bash
|
||
|
npx prisma init
|
||
|
```
|
||
|
|
||
|
Conexiunea db catre bd sqlite
|
||
|
1. schimbam provider-ul in fisierul `schema.prisma`
|
||
|
```
|
||
|
...
|
||
|
datasource db {
|
||
|
provider = "sqlite"
|
||
|
...
|
||
|
```
|
||
|
2. Ajustam url-ul de conectare catre db in fisierul `.env`
|
||
|
```
|
||
|
DATABASE_URL="file:./demo-app.db"
|
||
|
```
|
||
|
3. Declaram tabele unde se vor stoca utilizatorii, rolurile si nivelele de acces
|
||
|
`schema.prisma `
|
||
|
```
|
||
|
model User {
|
||
|
id Int @id @default(autoincrement())
|
||
|
email String @unique
|
||
|
password String
|
||
|
createdAt DateTime @default(now())
|
||
|
updatedAt DateTime @default(now())
|
||
|
roles UsersRoles[]
|
||
|
}
|
||
|
|
||
|
model Role {
|
||
|
id Int @id @default(autoincrement())
|
||
|
name String @unique
|
||
|
createdAt DateTime @default(now())
|
||
|
updatedAt DateTime @default(now())
|
||
|
permission Permission[]
|
||
|
users UsersRoles[]
|
||
|
}
|
||
|
|
||
|
model Permission {
|
||
|
id Int @id @default(autoincrement())
|
||
|
name String @unique
|
||
|
createdAt DateTime @default(now())
|
||
|
updatedAt DateTime @default(now())
|
||
|
role Role @relation(fields: [roleId], references: [id])
|
||
|
roleId Int
|
||
|
}
|
||
|
|
||
|
model UsersRoles {
|
||
|
user User @relation(fields: [userId], references: [id])
|
||
|
userId Int
|
||
|
role Role @relation(fields: [roleId], references: [id])
|
||
|
roleId Int
|
||
|
assignedAt DateTime @default(now())
|
||
|
assignedBy String
|
||
|
@@id([userId, roleId])
|
||
|
}
|
||
|
```
|
||
|
4. Generam clientul prisma
|
||
|
``` bash
|
||
|
npx prisma generate
|
||
|
```
|
||
|
|
||
|
### Zenstack
|
||
|
|
||
|
Copy `schema.prisma` to `../schema.zmodel`
|
||
|
|
||
|
Declaram zmodel-ul pentru tabela produselor `schema.zmodel` si gestionam acesele la ea
|
||
|
```
|
||
|
model Product {
|
||
|
id String @id @default(cuid())
|
||
|
createdAt DateTime @default(now())
|
||
|
updatedAt DateTime @updatedAt
|
||
|
title String
|
||
|
published Boolean @default(false)
|
||
|
author User @relation(fields: [authorId], references: [id])
|
||
|
authorId Int
|
||
|
|
||
|
// author has full access
|
||
|
@@allow('all', auth() == author)
|
||
|
|
||
|
// logged-in users can view published posts
|
||
|
@@allow('read', auth() != null && published)
|
||
|
}
|
||
|
```
|
||
|
generam staf-ul zenstack
|
||
|
``` bash
|
||
|
npx zenstack generate
|
||
|
```
|
||
|
|
||
|
Creem ceva date mock in `prisma/seed.ts`
|
||
|
``` bash
|
||
|
npx prisma migrate dev
|
||
|
npx prisma db seed
|
||
|
```
|
||
|
|
||
|
### GraphQL Integration
|
||
|
|
||
|
In fisierul `schema.zmodel` adaugam si rulam `npx zenstack generate`
|
||
|
```
|
||
|
generator typegraphql {
|
||
|
provider = "typegraphql-prisma-nestjs"
|
||
|
output = "../prisma/generated/type-graphql"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
Enable graphql integration *app.module* si tot aici indicam clientul prisma enhancing de ZenStack
|
||
|
```
|
||
|
...
|
||
|
imports: [
|
||
|
GraphQLModule.forRoot<ApolloDriverConfig>({
|
||
|
driver: ApolloDriver,
|
||
|
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
|
||
|
playground: true,
|
||
|
buildSchemaOptions: { dateScalarMode: 'timestamp' },
|
||
|
],
|
||
|
controllers:
|
||
|
...
|
||
|
```
|
||
|
|
||
|
Registering all resolvers inside `providers` of the Nest module
|
||
|
```
|
||
|
providers: [
|
||
|
...
|
||
|
|
||
|
```
|
||
|
|