Skip to content

Commit

Permalink
feat(server): add func db design, turn pg to mongo (#454)
Browse files Browse the repository at this point in the history
Signed-off-by: maslow <wangfugen@126.com>
  • Loading branch information
maslow authored Nov 30, 2022
1 parent 4dae4da commit 12b1587
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 72 deletions.
2 changes: 1 addition & 1 deletion deploy/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ multipass exec laf-dev -- sudo -u root kubectl apply -f /laf/deploy/scripts/init

```bash
# Forward service in cluster to localhost, run this command in another terminal separately
kubectl port-forward deployment/postgresql 5432 -n laf
kubectl port-forward deployment/mongodb 27017:27017 -n laf
kubectl port-forward deployments/casdoor 30070:8000 -n laf

# Run these in first time or when someone change the schema.
Expand Down
4 changes: 3 additions & 1 deletion server/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

# database
DATABASE_URL=postgresql://adm1n:passw0rd@localhost:5432/sys_db?schema=public
# DATABASE_URL=postgresql://adm1n:passw0rd@localhost:5432/sys_db?schema=public
DATABASE_URL=mongodb://admin:passw0rd@mongo.laf.svc.cluster.local:27017/sys_db?authSource=admin&replicaSet=rs0 #&writeConcern=majority


# jwt settings
JWT_SECRET=abc123
Expand Down
1 change: 1 addition & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@nestjs/terminus": "^9.1.2",
"@nestjs/throttler": "^3.1.0",
"@prisma/client": "^4.6.1",
"bson": "^4.7.0",
"casdoor-nodejs-sdk": "^1.3.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
Expand Down
29 changes: 0 additions & 29 deletions server/prisma/migrations/20221121054527_init/migration.sql

This file was deleted.

20 changes: 0 additions & 20 deletions server/prisma/migrations/20221121100108_add_fields/migration.sql

This file was deleted.

3 changes: 0 additions & 3 deletions server/prisma/migrations/migration_lock.toml

This file was deleted.

62 changes: 51 additions & 11 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,63 @@ generator client {
}

datasource db {
provider = "postgresql"
provider = "mongodb"
url = env("DATABASE_URL")
}

model User {
id String @unique
username String @unique
email String @unique
phone String @unique
id String @id @default(auto()) @map("_id") @db.ObjectId
username String @unique
email String @unique
phone String @unique
created_at DateTime @default(now())
updated_at DateTime @updatedAt
profile UserProfile?
}

model UserProfile {
uid String @unique
openid String?
from String?
avatar String?
name String?
user User @relation(fields: [uid], references: [id])
id String @id @default(auto()) @map("_id") @db.ObjectId
uid String @unique @db.ObjectId
openid String?
from String?
avatar String?
name String?
created_at DateTime @default(now())
updated_at DateTime @updatedAt
user User @relation(fields: [uid], references: [id])
}

enum HttpMethod {
GET
POST
PUT
DELETE
PATCH
HEAD
}

type CloudFunctionSource {
code String
uri String?
version Int @default(0)
hash String?
lang String?
}

model CloudFunction {
id String @id @default(auto()) @map("_id") @db.ObjectId
appid String
name String
source CloudFunctionSource
desc String
tags String[]
websocket Boolean
methods HttpMethod[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
created_by String @db.ObjectId
@@unique([appid, name])
}
1 change: 0 additions & 1 deletion server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class AuthService {
let user = profile?.user
if (!user) {
user = await this.userService.create({
id: this.userService.generateUserId(),
username: casdoorUser.name,
email: casdoorUser.email,
phone: casdoorUser.phone,
Expand Down
3 changes: 2 additions & 1 deletion server/src/collections/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ export class DatabaseService {
assert(db.status.connectionUri, 'Database connection uri is required')

const uri = db.status?.connectionUri
const client = new MongoClient(uri)
const client = new MongoClient(uri, { maxPoolSize: 1, minPoolSize: 0 })
try {
this.logger.verbose(`Connecting to database ${db.metadata.name}`)
await client.connect()
this.logger.log(`Connected to database ${db.metadata.namespace}`)
return client
Expand Down
14 changes: 14 additions & 0 deletions server/src/users/dto/user.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ApiProperty } from '@nestjs/swagger'
import { User, UserProfile } from '@prisma/client'

export class UserProfileDto implements UserProfile {
id: string

@ApiProperty()
uid: string

Expand All @@ -15,6 +17,12 @@ export class UserProfileDto implements UserProfile {
name: string

from: string

@ApiProperty()
created_at: Date

@ApiProperty()
updated_at: Date
}

export class UserDto implements User {
Expand All @@ -32,4 +40,10 @@ export class UserDto implements User {

@ApiProperty()
profile: UserProfileDto

@ApiProperty()
created_at: Date

@ApiProperty()
updated_at: Date
}
10 changes: 5 additions & 5 deletions server/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { Injectable } from '@nestjs/common'
import { Prisma, User } from '@prisma/client'
import { PrismaService } from '../prisma.service'
import * as nanoid from 'nanoid'

import { ObjectID } from 'bson'
@Injectable()
export class UsersService {
constructor(private prisma: PrismaService) {}

/**
* @deprecated
* @returns
*/
generateUserId() {
const nano = nanoid.customAlphabet(
'1234567890abcdefghijklmnopqrstuvwxyz',
Expand All @@ -16,10 +20,6 @@ export class UsersService {
}

async create(data: Prisma.UserCreateInput): Promise<User> {
if (!data.id) {
data.id = this.generateUserId()
}

return this.prisma.user.create({
data,
})
Expand Down

0 comments on commit 12b1587

Please sign in to comment.