Skip to content

Commit

Permalink
chore: update doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016 committed Dec 17, 2022
1 parent 9e06dc1 commit 5b734a0
Show file tree
Hide file tree
Showing 10 changed files with 424 additions and 75 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
],
sidebar: [
// ['/', '首页'],
['/zh/getting-started', '快速开始'],
['/zh/guide', '指南'],
'/zh/app-acl',
['/zh/app-model-extends', 'ORM 扩展选项'],
Expand Down
12 changes: 6 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ home: true
heroText: fib-app
tagline: 为个人网站和中小企业而生
actionText: 快速上手 →
actionLink: /zh/guide
actionLink: /zh/getting-started
features:
- title: RESTful
details: 内部基于 RESTful 标准实现,一键部署 http 服务
- title: ORM
- title: RESTful/GraphQL
details: 将你的数据库一键变为标准的 http rest 服务, 并带有 GraphQL 支持
- title: RDB ORM
details: 基于 @fxjs/orm, 快速连接已有关系型数据库(MySQL/SQLite)
- title: 高性能
details: 享受 fibjs 的高性能
- title: Fast!
details: 享受来自 FIBJS 生态的高性能
footer: MIT Licensed | Copyright © 2015-present fibjs
---
16 changes: 9 additions & 7 deletions docs/zh/app-internal-api.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## `app.api.*`

通过内部方法, 直接进行 rest 风格的操作, 详情可参考 [@types/app.d.ts] 中的 FibAppInternalApis.
通过内部方法, 直接进行 rest 风格的操作, 详情可参考 [typings/app.d.ts] 中的 FibAppInternalApis.

- app.api.post: FibAppIneternalApiFunction__Post
- app.api.get: FibAppIneternalApiFunction__Get
Expand All @@ -20,7 +20,7 @@ FibAppInternalApis 中的所有所有 rest 操作函数, 内部都经过了 `app

* `app.filterRequest: FibAppSetupChainFn`

注意该函数无返回值, 而是以最后一个参数作为回调函数. 更多详情可参考 [@types/app.d.ts](@types/app.d.ts)
注意该函数无返回值, 而是以最后一个参数作为回调函数. 更多详情可参考 [typings/app.d.ts]

使用 `app.filterRequest` 为 app 定制个性化的路由
------------
Expand Down Expand Up @@ -74,11 +74,13 @@ app.get('/__null_cls', (request) => {
实际上, [Model Function](./app-model-function.md) 正是通过 `app.filterRequest` 实现的, 详情可参考 [src/classes/index.ts] 中关于 `app.post(':classname/:func', ...)` 的实现.

[mq.Routing]:http://fibjs.org/docs/manual/object/ifs/routing.md.html
[src/classes]:src/classes
[src/classes/index.ts]:src/classes/index.ts
[src/classes/base.ts]:src/classes/base.ts
[src/classes/extend.ts]:src/classes/extend.ts

[src/http/index.ts]:https://github.com/fibjs/fib-app/blob/master/src/http/index.ts
[src/http/base.ts]:https://github.com/fibjs/fib-app/blob/master/src/http/base.ts
[src/http/extend.ts]:https://github.com/fibjs/fib-app/blob/master/src/http/extend.ts

### hasMany-extra read/epost/eput(beta)

...
...

[typings/app.d.ts]:https://github.com/fibjs/fib-app/blob/master/typings/app.d.ts
7 changes: 5 additions & 2 deletions docs/zh/app-model-extends.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

你可以在 Model Function 中调用 app.api 上的 rest 风格函数, 来定制属于你的函数, 比如

```JavaScript
```js
module.exports = db => {
var Person = db.define('person', {
name: String,
Expand Down Expand Up @@ -202,9 +202,12 @@ viewFunctions: {
#### 共同点
`viewFunction``function` 很相似

1. 都要返回符合 [`FibAppResponse` 格式](./@types/app.d.ts) 的对象
1. 都要返回符合 [`FibAppResponse` 格式] 的对象
1. 都是 ORM Model 的定义选项

#### 区别
1. `function` 处理 fib-app 中的 `POST /:classname/:func` 请求; `viewFunctions` 处理 fib-app 中的 `GET /:classname/:func``Accept` 头包含 `text/html` 的请求
1. `function` 函数的返回值, fib-app 会尝试以 json 的方式写入 `HttpResponse`; `viewFunction` 函数的返回值, fib-app 会尝试以文本的方式写入 `HttpResponse`

[typings/app.d.ts]:https://github.com/fibjs/fib-app/blob/master/typings/app.d.ts
[`FibAppResponse` 格式]:typings/app.d.ts
174 changes: 174 additions & 0 deletions docs/zh/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# 快速开始

[![NPM version](https://img.shields.io/npm/v/fib-app.svg)](https://www.npmjs.org/package/fib-app)
[![Build Status][actions-image]][actions-url]

[actions-image]:https://github.com/fibjs/fib-app/actions/workflows/run-ci.yml/badge.svg
[actions-url]:https://github.com/fibjs/fib-app/actions/workflows/run-ci.yml

基于 `fib-app`, 快速对业务建模, 并通过 http 对模型进行 restful 操作.

```js
// index.js
const http = require('http');
const App = require('fib-app');

const person_def = App.defineAppModel(orm => {
orm.define('person', {
name: String,
age: Number,
gender: ['male', 'female']
})
})

const app = new App('sqlite:test.db');
app.db.use(person_def);

const svr = new http.Server(1234, [
{
'/1.0': app
}
]);

svr.start();
```

通过 `fibjs index.js`, 运行上述代码, 我们就完成了两项工作:

1. 对数据库中的表(`person`)建模
2. 将其映射到了 `http://127.0.0.1:1234/1.0` 这个 endpoint

现在我们可以通过该 endpoint 对数据库进行操作

## create <Badge type="info" text="POST" />

**请求格式**: `POST http://{endpoint}/{model_name}`

通过 POST 操作, 我们可以向数据库中添加一条 person 数据

```bash
curl -X POST 'http://127.0.0.1:1234/1.0/person' \
-H 'Content-Type: application/json' \
-d '{"name": "Jack", "age": 12, "gender": "male"}'
```

请求结果:

```json
{
"id": 1,
"createdAt": "2022-12-17T13:06:41.592Z"
}
```

这表示, 该操作往数据库中添加了一条新数据, 且其 id 为 1

## get

**请求格式**: `GET http://{endpoint}/{model_name}/:id`

已知一个 `person` 的 id, 可以通过 GET 请求查询其信息

```bash
curl -X GET 'http://127.0.0.1:1234/1.0/person/1'
```

请求结果:

```json
{
"name": "Jack",
"age": 12,
"gender": "male",
"createdAt": "2022-12-17T13:06:41.592Z",
"updatedAt": "2022-12-17T13:06:41.592Z",
"id":1
}
```

如果查询一个不存在的 id, 则会返回错误:

```bash
curl -X GET 'http://127.0.0.1:1234/1.0/person/9999'
```

请求结果

```json
{
"code":4040102,
"message":"Object '9999' not found in class 'person'."
}
```

## find <Badge type="info" text="GET" />

**请求格式**: `GET http://{endpoint}/{model_name}`

在不知道 id 情况下, 我们也可以直接尝试 list `person`,

```bash
curl -X GET 'http://127.0.0.1:1234/1.0/person'
```

请求结果:

```json
[
{
"name": "Jack",
"age": 12,
"gender": "male",
"createdAt": "2022-12-17T13:06:41.592Z",
"updatedAt": "2022-12-17T13:06:41.592Z",
"id":1
}
]
```

## update <Badge type="warning" text="PUT" />

**请求格式**: `PUT http://{endpoint}/{model_name}/:id`

已知一个 `person` 的 id, 可以通过 PUT 请求更改其信息

```bash
curl -X PUT 'http://127.0.0.1:1234/1.0/person/1' \
-H 'Content-Type: application/json' \
-d '{"age": 13}'
```

请求结果:

```json
{
"id":1
}
```

这表示, 该操作改变了数据库中 id 为 1 的 person 的数据


## remove <Badge type="warning" text="DELETE" />

**请求格式**: `DELETE http://{endpoint}/{model_name}/:id`

已知一个 `person` 的 id, 可以通过 DELETE 请求删除这条数据

```bash
curl -X DELETE 'http://127.0.0.1:1234/1.0/person/1'
```

请求结果:

```json
{
"id":1
}
```

这表示, 该操作删除了数据库中 id 为 1 的 person 的数据.

## 下一步呢?

让我们跟随[指南](/zh/guide.html), 学习 fib-app 更多的定制方法.
Loading

0 comments on commit 5b734a0

Please sign in to comment.