Skip to content

Commit

Permalink
Merge branch 'user-as-a-service'
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoYongchao committed Jun 24, 2024
2 parents 4c00f4d + 0cc8914 commit f6de859
Show file tree
Hide file tree
Showing 17 changed files with 6,487 additions and 4,812 deletions.
1 change: 1 addition & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SITE_ROOT_URL=http://localhost:3000
SITE_API_URL=${SITE_ROOT_URL}/api
FRONTEND_SITE_URL=http://localhost:3030
FRONTEND_DOCS_URL=http://localhost:4000
FRONTEND_APP_URL=http://localhost:8000

LOG_LEVEL=debug
# default to stdout
Expand Down
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SITE_ROOT_URL=http://localhost:3000
SITE_API_URL=${SITE_ROOT_URL}/api
FRONTEND_SITE_URL=http://localhost:3030
FRONTEND_DOCS_URL=http://localhost:4000
FRONTEND_APP_URL=http://localhost:8000

LOG_LEVEL=debug
# default to stdout
Expand Down
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.16.0
v18.17.1
99 changes: 55 additions & 44 deletions DEVLOG.md
Original file line number Diff line number Diff line change
@@ -1,84 +1,95 @@
# Developer Guide
This is guide for developers to setup the development environment. Before you start to get prepared for the develop environment, you need to make sure your basic tools are installed correctly.

## Development Setup
* Nodejs (Notice: To build the project, the right version is needed, otherwise installation can not be corrected, see .node-version)
* pnpm
* docker

## Development Setup
- copy `.env.dev` to `.env`
- install postgres with vector plugin

```shell
docker pull ankane/pgvector
```
- install dependencies
```shell
pnpm i
```

- init db
- install dababase postgres with vector plugin
```shell
docker pull ankane/pgvector
```

```shell
npx prisma generate # generate PrismaClient
npx prisma migrate dev # init db schema
npx prisma db seed # init db data
```
- start postgres db in docker
```shell
docker run --name callgent-postgres -p 5432:5432 -e POSTGRES_PASSWORD=postgres -e PG_VECTOR_EXTENSION=true -d ankane/pgvector
```

- init db test data
- init db
```shell
npx prisma generate # generate PrismaClient
npx prisma migrate dev # init db schema
npx prisma db seed # init db data
```

```shell
SEED_TEST_DATA=1 npx prisma db seed
```
- init db test data
```shell
pnpm run prisma:seed:test # init db test data
```

- start server
```shell
pnpm run start:dev
```

```shell
pnpm run start:dev
```
If all the above steps are done, and nothing failed, you can access the API at `http://localhost:3000/api`

- run tests
```shell
pnpm run test:e2e
```

## Development Logs

### init project

- init project

```shell
pnpm i -g @nestjs/cli
nest new callgent-api
cd callgent-api
```
```shell
pnpm i -g @nestjs/cli
nest new callgent-api
cd callgent-api
```

### add dependencies

### integrate prisma

- automatically setup the library, scripts and Docker files

```shell
nest add nestjs-prisma
```
```shell
nest add nestjs-prisma
```

- integrate prisma plugins

- ReposModule
- init db

```shell
npx prisma init
```
- init db
```shell
npx prisma init
```

- create prisma schema, then init db

```shell
npx prisma migrate dev --name init
```
```shell
npx prisma migrate dev --name init
```

### multi-tenancy

1. write default value for `tenancy.tenantId` in db

```text
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId'))::int"))
```
```text
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId'))::int"))
```

2. enable postgres row level security(RLS), so that we can filter data by `tenantId` automatically:
config in prisma/migrations/01_row_level_security/migration.sql,
@see <https://github.com/prisma/prisma-client-extensions/tree/main/row-level-security>

3. set `tenantId` into `cls` context:

```ts
cls.set('TENANT_ID', ..
```
Expand Down
Loading

0 comments on commit f6de859

Please sign in to comment.