Skip to content

Commit

Permalink
feat(server): refactor bundle and other data schemas (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Feb 22, 2023
1 parent bf81496 commit 809d1de
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 148 deletions.
8 changes: 4 additions & 4 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
## 👨‍💻 Who is `laf` for?

1. **Front-end Developer** + `laf` = Full Stack Developer
- `laf` privided [laf-client-sdk](https://github.com/labring/laf/tree/main/packages/client-sdk) for front-end which can be used in any JS runtime.
- `laf` provided [laf-client-sdk](https://github.com/labring/laf/tree/main/packages/client-sdk) for front-end which can be used in any JS runtime.
- `laf` cloud function are developed using JS/TS, no need to learn any other languages.
- `laf` provides static site hosting in one click, no more worries about server config, nginx, domain name, etc.
- `laf` will provides more SDK in the future (Flutter/Android/iOS) to give you a unified experience on any platforms.
Expand All @@ -98,7 +98,7 @@
- You can provide full source code to your clients which enables them to deploy the application in **any** environment.
- You can modify/customize your cloud platform, `laf` is open-sourced and built with customization in mind.

4. **Node.js Developer**`laf` is developed using `Node.js`, you can treat it as another **Node.js** framwork/platform.
4. **Node.js Developer**`laf` is developed using `Node.js`, you can treat it as another **Node.js** framework/platform.

- You can write/debug/deploy your cloud functions in the browser with minimal effort.
- You can inspect/search logs with no configuration needed.
Expand Down Expand Up @@ -170,10 +170,10 @@ Independent domain names and HTTPS licenses can be applied to your applications

## 🏘️ Community

- [Wechat Group](https://oss.lafyun.com/wofnib-image/2022-04-22-14-21-MRJH9o.png)
- [WeChat Group](https://oss.lafyun.com/wofnib-image/2022-04-22-14-21-MRJH9o.png)
- [QQ Group:603059673](https://jq.qq.com/?_wv=1027&k=DdRCCiuz)
- _More community resources will be added soon!_

## 🌟 Star History

[![Star History Chart](https://api.star-history.com/svg?repos=lafjs/laf&type=Date)](https://star-history.com/#lafjs/laf&Date)
[![Star History Chart](https://api.star-history.com/svg?repos=labring/laf&type=Date)](https://star-history.com/#labring/laf&Date)
4 changes: 2 additions & 2 deletions deploy/build/charts/laf-server/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ spec:
value: {{ .Values.default_region.minio_root_access_key | quote}}
- name: DEFAULT_REGION_MINIO_ROOT_SECRET_KEY
value: {{ .Values.default_region.minio_root_secret_key | quote}}
- name: DEFAULT_REGION_FUNCTION_DOMAIN
value: {{ .Values.default_region.function_domain }}
- name: DEFAULT_REGION_RUNTIME_DOMAIN
value: {{ .Values.default_region.runtime_domain }}
- name: DEFAULT_REGION_WEBSITE_DOMAIN
value: {{ .Values.default_region.website_domain }}
- name: DEFAULT_REGION_TLS
Expand Down
2 changes: 1 addition & 1 deletion deploy/build/charts/laf-server/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ default_region:
apisix_api_key: ""
apisix_public_port: 80
tls: false
function_domain: ""
runtime_domain: ""
website_domain: ""
jwt:
secret: laf_server_abc123
Expand Down
2 changes: 1 addition & 1 deletion deploy/build/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ helm install server -n ${NAMESPACE} \
--set default_region.minio_internal_endpoint=${MINIO_INTERNAL_ENDPOINT} \
--set default_region.minio_root_access_key=${MINIO_ROOT_ACCESS_KEY} \
--set default_region.minio_root_secret_key=${MINIO_ROOT_SECRET_KEY} \
--set default_region.function_domain=${DOMAIN} \
--set default_region.runtime_domain=${DOMAIN} \
--set default_region.website_domain=site.${DOMAIN} \
--set default_region.tls=false \
--set default_region.apisix_api_url=${APISIX_API_URL} \
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/db/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const db = cloud.database();

### 在客户端中访问数据库

前端可使用 [laf-client-sdk](https://github.com/lafjs/laf/tree/main/packages/client-sdk) “直连”数据库,无需与服务端对接口。
前端可使用 [laf-client-sdk](https://github.com/labring/laf/tree/main/packages/client-sdk) “直连”数据库,无需与服务端对接口。

在访问数据库之前,需要先设置一个访问策略。在 云数据库-访问策略 中创建一个新的策略,策略内容如下:

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/db/policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: 访问策略

## 数据访问策略

前端可使用 [laf-client-sdk](https://github.com/lafjs/laf/tree/main/packages/client-sdk) “直连”数据库,无需与服务端对接口。
前端可使用 [laf-client-sdk](https://github.com/labring/laf/tree/main/packages/client-sdk) “直连”数据库,无需与服务端对接口。

访问策略用来对客户端对数据库的操作进行安全控制,一个访问策略由多个集合的访问规则组成,每个集合可配置读写操作权限的访问规则。

Expand Down
8 changes: 4 additions & 4 deletions packages/client-sdk/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### 介绍

Javascript client sdk of [`LaF`](https://github.com/lafjs/laf).
Javascript client sdk of [`laf`](https://github.com/labring/laf).

### 安装

Expand All @@ -11,7 +11,7 @@ Javascript client sdk of [`LaF`](https://github.com/lafjs/laf).
### 使用示例

```js
import { Cloud } from 'laf-client-sdk'
import { Cloud } from "laf-client-sdk";

const cloud = new Cloud({
// the laf app server base url
Expand Down Expand Up @@ -451,8 +451,8 @@ collection.field()

参数说明

| 参数 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------------------------------------- |
| 参数 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ---------------------------------- |
| - | object || 要过滤的字段,不返回传 0,返回传 1 |

使用示例
Expand Down
4 changes: 2 additions & 2 deletions packages/database-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"repository": {
"type": "git",
"url": "git+https://github.com/lafjs/laf.git"
"url": "git+https://github.com/labring/laf.git"
},
"author": "maslow(wangfugen@126.com)",
"license": "ISC",
Expand All @@ -38,4 +38,4 @@
"mocha": "^9.0.2",
"typescript": "^3.6.4"
}
}
}
4 changes: 2 additions & 2 deletions packages/database-ql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/lafjs/laf.git"
"url": "git+https://github.com/labring/laf.git"
},
"keywords": [
"laf",
Expand All @@ -39,4 +39,4 @@
"lodash.set": "4.3.2",
"lodash.unset": "4.5.2"
}
}
}
92 changes: 54 additions & 38 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ type RegionDatabaseConf {
}

type RegionGatewayConf {
driver String // apisix
functionDomain String // cloud function domain
websiteDomain String // website domain
port Int @default(80)
apiUrl String
apiKey String
driver String // apisix
runtimeDomain String // runtime domain (cloud function)
websiteDomain String // website domain
port Int @default(80)
apiUrl String
apiKey String
}

type RegionStorageConf {
Expand All @@ -90,7 +90,7 @@ model Region {
gatewayConf RegionGatewayConf
storageConf RegionStorageConf
tls Boolean @default(false)
state String @default("active") // active, inactive
state String @default("Active") // Active, Inactive
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand All @@ -99,27 +99,43 @@ model Region {
bundles Bundle[]
}

model Bundle {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
displayName String
regionName String
type BundleResource {
limitCPU Int // 1000 = 1 core
limitMemory Int // in MB
requestCPU Int // 1000 = 1 core
requestMemory Int // in MB
databaseCapacity Int // in MB
storageCapacity Int // in MB
networkTrafficOutbound Int // in MB
priority Int @default(0)
state String @default("active") // active, inactive
price Int @default(0)
}

region Region @relation(fields: [regionName], references: [name])
model Bundle {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
displayName String
regionId String @db.ObjectId
resource BundleResource
priority Int @default(0)
state String @default("Active") // Active, Inactive
price Int @default(0)
applications Application[]
region Region @relation(fields: [regionId], references: [id])
@@unique([regionId, name])
}

model ApplicationBundle {
id String @id @default(auto()) @map("_id") @db.ObjectId
appid String @unique
name String
displayName String
resource BundleResource
price Int @default(0)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([regionName, name])
application Application @relation(fields: [appid], references: [appid])
}

type RuntimeImageGroup {
Expand All @@ -133,6 +149,7 @@ model Runtime {
name String @unique
type String
image RuntimeImageGroup
state String @default("Active") // Active, Inactive
version String
latest Boolean
Application Application[]
Expand Down Expand Up @@ -161,28 +178,27 @@ enum ApplicationPhase {
}

model Application {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
appid String @unique
regionName String
bundleName String
runtimeName String
tags String[]
state ApplicationState @default(Running)
phase ApplicationPhase @default(Creating)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
lockedAt DateTime
createdBy String @db.ObjectId
region Region @relation(fields: [regionName], references: [name])
runtime Runtime @relation(fields: [runtimeName], references: [name])
bundle Bundle @relation(fields: [regionName, bundleName], references: [regionName, name])
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
appid String @unique
regionId String @db.ObjectId
runtimeId String @db.ObjectId
tags String[]
state ApplicationState @default(Running)
phase ApplicationPhase @default(Creating)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
lockedAt DateTime
createdBy String @db.ObjectId
region Region @relation(fields: [regionId], references: [id])
runtime Runtime @relation(fields: [runtimeId], references: [id])
configuration ApplicationConfiguration?
storageUser StorageUser?
database Database?
domain ApplicationDomain?
domain RuntimeDomain?
bundle ApplicationBundle?
}

type EnvironmentVariable {
Expand Down Expand Up @@ -288,9 +304,9 @@ model DatabasePolicy {
appid String
name String
injector String?
rules DatabasePolicyRule[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
rules DatabasePolicyRule[]
@@unique([appid, name])
}
Expand Down Expand Up @@ -394,7 +410,7 @@ enum DomainPhase {
Deleted
}

model ApplicationDomain {
model RuntimeDomain {
id String @id @default(auto()) @map("_id") @db.ObjectId
appid String @unique
domain String @unique
Expand Down
13 changes: 6 additions & 7 deletions server/src/application/application-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { StorageService } from '../storage/storage.service'
import { DatabaseService } from '../database/database.service'
import { ClusterService } from 'src/region/cluster/cluster.service'
import { RegionService } from 'src/region/region.service'
import { FunctionDomainService } from 'src/gateway/function-domain.service'
import { RuntimeDomainService } from 'src/gateway/runtime-domain.service'
import { TASK_LOCK_INIT_TIME } from 'src/constants'
import { SystemDatabase } from 'src/database/system-database'

Expand All @@ -27,7 +27,7 @@ export class ApplicationTaskService {
private readonly clusterService: ClusterService,
private readonly storageService: StorageService,
private readonly databaseService: DatabaseService,
private readonly gatewayService: FunctionDomainService,
private readonly gatewayService: RuntimeDomainService,
) {}

@Cron(CronExpression.EVERY_SECOND)
Expand Down Expand Up @@ -82,7 +82,7 @@ export class ApplicationTaskService {

// get region by appid
const region = await this.regionService.findByAppId(appid)
assert(region, `Region ${app.regionName} not found`)
assert(region, `Region ${region.name} not found`)

// reconcile namespace
const namespace = await this.clusterService.getAppNamespace(region, appid)
Expand Down Expand Up @@ -172,7 +172,7 @@ export class ApplicationTaskService {
const app = res.value
const appid = app.appid
const region = await this.regionService.findByAppId(appid)
assert(region, `Region ${app.regionName} not found`)
assert(region, `Region ${region.name} not found`)

// delete namespace (include the instance)
const namespace = await this.clusterService.getAppNamespace(region, appid)
Expand Down Expand Up @@ -202,7 +202,7 @@ export class ApplicationTaskService {
return await this.unlock(appid)
}

// TODO: delete app configuration
// TODO: delete app configuration & bundle

// update phase to `Deleted`
const updated = await db.collection<Application>('Application').updateOne(
Expand Down Expand Up @@ -260,7 +260,7 @@ export class ApplicationTaskService {
*/
async unlock(appid: string) {
const db = SystemDatabase.db
const updated = await db.collection<Application>('Application').updateOne(
await db.collection<Application>('Application').updateOne(
{
appid: appid,
},
Expand All @@ -270,7 +270,6 @@ export class ApplicationTaskService {
},
},
)
if (updated.modifiedCount > 0) this.logger.debug(`unlocked app: ${appid}`)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions server/src/application/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ApplicationService } from './application.service'
import { FunctionService } from '../function/function.service'
import { StorageService } from 'src/storage/storage.service'
import { RegionService } from 'src/region/region.service'
import { FunctionDomainService } from 'src/gateway/function-domain.service'
import { RuntimeDomainService } from 'src/gateway/runtime-domain.service'

@ApiTags('Application')
@Controller('applications')
Expand All @@ -37,7 +37,7 @@ export class ApplicationController {
private readonly appService: ApplicationService,
private readonly funcService: FunctionService,
private readonly regionService: RegionService,
private readonly gatewayService: FunctionDomainService,
private readonly gatewayService: RuntimeDomainService,
private readonly storageService: StorageService,
) {}

Expand Down Expand Up @@ -92,7 +92,7 @@ export class ApplicationController {
})

// [SECURITY ALERT] Do NOT response this region object to client since it contains sensitive information
const region = await this.regionService.findOne(data.regionName)
const region = await this.regionService.findOne(data.regionId)

// TODO: remove these storage related code to standalone api
let storage = {}
Expand Down
Loading

0 comments on commit 809d1de

Please sign in to comment.