docs: update docs

This commit is contained in:
chenos 2021-09-05 00:10:41 +08:00
parent 1229a10df6
commit 2a8112b5a1
7 changed files with 226 additions and 3 deletions

View File

@ -1,6 +1,6 @@
---
title: 介绍
order: 1
order: 0
toc: menu
nav:
title: 核心

135
docs/cores/migration.md Normal file
View File

@ -0,0 +1,135 @@
---
title: 数据库迁移
order: 1
toc: menu
nav:
title: 核心
order: 2
---
# 数据库迁移
NocoBase 通过配置来生成数据表和字段,如:
```ts
const db = new Database();
db.table({
name: 'examples',
fields: [
{ type: 'string', name: 'name' },
{ type: 'string', name: 'title' },
],
});
```
如果需要生成数据表和字段,直接执行 `db.sync()` 操作就可以了。
```ts
// 只更新不删除
await db.sync();
// 全部重建
await db.sync({
force: true,
alter: {
drop: true,
},
});
// 更多参数说明
interface SyncOptions {
/**
* The tables should be created or updated.
*/
tables?: string[] | Table[] | Map<string, Table>;
/**
* If force is true, each DAO will do DROP TABLE IF EXISTS ..., before it tries to create its own table
*/
force?: boolean;
/**
* If alter is true, each DAO will do ALTER TABLE ... CHANGE ...
* Alters tables to fit models. Provide an object for additional configuration. Not recommended for production use. If not further configured deletes data in columns that were removed or had their type changed in the model.
*/
alter?: boolean | SyncAlterOptions;
/**
* Match a regex against the database name before syncing, a safety check for cases where force: true is
* used in tests but not live code
*/
match?: RegExp;
/**
* The schema that the tables should be created in. This can be overridden for each table in sequelize.define
*/
schema?: string;
/**
* An optional parameter to specify the schema search_path (Postgres only)
*/
searchPath?: string;
}
```
如果后续有新增或删除字段操作,直接修改配置即可,如:
```ts
db.table({
name: 'examples',
fields: [
{ type: 'string', name: 'name' },
// { type: 'string', name: 'title' }, // 去掉 title 字段
{ type: 'text', name: 'content' }, // 新增 content 字段
],
});
```
接着执行 `db.sync()` 操作就同步给数据库了。
但是,如果需要更改字段名怎么操作呢,比如将 name 改为 name2需要将配置修一下
```ts
db.table({
name: 'examples',
fields: [
{ type: 'string', name: 'name2' },
],
});
```
这时,执行 `db.sync()` 操作并不会把 name 改为 name2而是又生成了一个新的 name2 字段。如果要更改字段名称,需要配合 migration 来处理,如:
```ts
await queryInterface.renameColumn('examples', 'name', 'name2');
```
这个办法能解决问题,但是需要写另外的 migration 文件来处理。更好的办法,可以给字段一个显式的 uid
```ts
db.table({
name: 'examples',
fields: [
{ uid: 'f0nyq8tg3j1y', type: 'string', name: 'name' },
],
});
```
更改字段配置时uid 不变,修改其他值即可
```ts
db.table({
name: 'examples',
fields: [
{ uid: 'f0nyq8tg3j1y', type: 'string', name: 'name2' },
],
});
```
需要同步给数据库时,接着执行 `db.sync()` 方法。
<Alert title="注意" type="warning">
uid 的方案适用于 table、field、index但是还并未实现。暂时可以结合 migration 来管理数据库结构变更。
</Alert>

View File

@ -3,7 +3,7 @@ title: '@nocobase/database'
order: 1
# toc: menu
group:
order: 1
order: 10
title: 核心库
---

88
docs/cores/web-api.md Normal file
View File

@ -0,0 +1,88 @@
---
title: WEB API 设计
order: 2
toc: menu
nav:
title: 核心
order: 2
---
# WEB API 设计
<Alert title="注意" type="warning">
以下各代码片段,仅供阅读参考,可能与实际代码略有偏差。
</Alert>
和常规的 MVC 思路不同NocoBase 的 Server 非常简单,先来一段简单例子代码感受一下:
```ts
import { Application } from '@nocobase/server';
const api = new Application({
database: {},
resourcer: {},
});
// 配置数据表
api.database.table({
name: 'users',
fields: [
{ type: 'string', name: 'username' },
{ type: 'password', name: 'password' },
],
});
api.listen(3000);
```
相对应的 HTTP API
```bash
GET http://localhost:3000/api/users
POST http://localhost:3000/api/users
GET http://localhost:3000/api/users/1
PUT http://localhost:3000/api/users/1
DELETE http://localhost:3000/api/users/1
```
内置了基础的 REST API除此之外还可以自行扩展
```ts
api.resourcer.registerAction('users:login', async (ctx, next) => {
// 代码省略
});
```
HTTP API 新增了扩展
```bash
POST http://localhost:3000/api/users:login
```
配合 SDK 就是这样的了
```ts
// 常用的 REST API
// GET http://localhost:3000/api/users
api.resource('users').list();
// POST http://localhost:3000/api/users
api.resource('users').create();
// GET http://localhost:3000/api/users/1
api.resource('users').get();
// PUT http://localhost:3000/api/users/1
api.resource('users').update();
// DELETE http://localhost:3000/api/users/1
api.resource('users').destroy();
// 扩展的非 REST 风格的 API
// POST http://localhost:3000/api/users:login
api.resource('users').login();
// POST http://localhost:3000/api/users:register
api.resource('users').register();
// POST http://localhost:3000/api/users:logout
api.resource('users').logout();
// POST http://localhost:3000/api/users:export
api.resource('users').export();
```

View File

@ -13,7 +13,7 @@ Nocude 是一个开源免费的无代码、低代码开发平台。 无论是不
## 架构
<img src="../nocude.png" style="max-width: 800px;width:100%"/>
<img src="../nocobase.png" style="max-width: 800px;width:100%"/>
### 微内核

BIN
docs/nocobase.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 302 KiB