nocobase/docs/en-US/development/server/collections/index.md

195 lines
3.8 KiB
Markdown
Raw Normal View History

2022-11-07 01:11:53 +00:00
# Core concepts
## Collection
2022-11-07 01:11:53 +00:00
Collection is a collection of all kinds of data, such as orders, products, users, comments, etc. Different collections are distinguished by name, e.g.
```ts
2022-11-07 01:11:53 +00:00
// Orders
{
name: 'orders',
}
2022-11-07 01:11:53 +00:00
// Products
{
name: 'products',
}
2022-11-07 01:11:53 +00:00
// Users
{
name: 'users',
}
2022-11-07 01:11:53 +00:00
// Comments
{
name: 'comments',
}
```
## Collection Field
2022-11-07 01:11:53 +00:00
Each Collection has a number of Fields.
```ts
2022-11-07 01:11:53 +00:00
// Collection configuration
{
name: 'users',
fields: [
{ type: 'string', name: 'name' },
{ type: 'integer', name: 'age' },
2022-11-07 01:11:53 +00:00
// Other fields
],
}
2022-11-07 01:11:53 +00:00
// sample data
[
{
2022-11-07 01:11:53 +00:00
name: 'Jason',
age: 20,
},
2022-11-07 01:11:53 +00:00
{ {
name: 'Li Si',
age: 18,
}
];
```
2022-11-07 01:11:53 +00:00
The composition of a Collection Field in NocoBase consists of
<img src="./collection-field.svg" />
### Field Type
2022-11-07 01:11:53 +00:00
Different fields are distinguished by name, and type indicates the data type of the field, which is divided into Attribute Type and Association Type, e.g.
2022-11-07 01:11:53 +00:00
**Attribute - Attribute Type**
- string
- text
- date
- boolean
- time
- float
- json
- location
- password
- virtual
- ...
2022-11-07 01:11:53 +00:00
**Relationship - Association Type**
- hasOne
- hasMany
- belongsTo
- belongsToMany
- ...
### Field Component
2022-11-07 01:11:53 +00:00
The field has a data type, the IO of the field value is fine, but it is not enough, if you need to display the field on the interface, you need another dimension of configuration -- `uiSchema`, e.g.
```tsx | pure
2022-11-07 01:11:53 +00:00
// Email field, displayed with Input component, using email validation rules
{
type: 'string',
name: 'email',
uiSchema: {
'x-component': 'Input',
'x-component-props': { size: 'large' },
'x-validator': 'email',
2022-11-07 01:11:53 +00:00
'x-pattern': 'editable', // editable state, and readonly state, read-pretty state
},
}
2022-11-07 01:11:53 +00:00
// Example data
{
email: 'admin@nocobase.com',
}
2022-11-07 01:11:53 +00:00
// Component example
<Input name={'email'} size={'large'} value={'admin@nocobase.com'} />
```
2022-11-07 01:11:53 +00:00
The uiSchema is used to configure the components of the field to be displayed on the interface, each field component will correspond to a value and includes several maintained configurations:
2022-11-07 01:11:53 +00:00
- The component of the field
- The parameters of the component
- The field's validation rules
- The mode of the field (editable, readonly, read-pretty)
- The default value of the field
- Other
2022-11-07 01:11:53 +00:00
[see the UI Schema chapter for more information](/development/client/ui-schema-designer/what-is-ui-schema).
2022-11-07 01:11:53 +00:00
The built-in field components of NocoBase are
- Input
- InputNumber
- Select
- Radio
- Checkbox
- ...
### Field Interface
2022-11-07 01:11:53 +00:00
With Field Type and Field Component you can freely combine several fields, we call this combined template Field Interface, e.g.
```ts
2022-11-07 01:11:53 +00:00
// email field, string + input, email validation rules
{
type: 'string',
name: 'email',
uiSchema: {
'x-component': 'Input',
'x-component-props': {},
'x-validator': 'email',
},
}
2022-11-07 01:11:53 +00:00
// phone field, string + input, phone validation rules
{
type: 'string',
name: 'phone',
uiSchema: {
'x-component': 'Input',
'x-component-props': {},
'x-validator': 'phone',
},
}
```
2022-11-07 01:11:53 +00:00
The above email and phone require a full uiSchema to be configured each time which is very tedious. To simplify the configuration, another concept Field interface is introduced, which can template some parameters, e.g.
```ts
2022-11-07 01:11:53 +00:00
// Template for the email field
interface email {
type: 'string';
uiSchema: {
'x-component': 'Input',
'x-component-props': {},
'x-validator': 'email',
};
}
2022-11-07 01:11:53 +00:00
// Template for the phone field
interface phone {
type: 'string';
uiSchema: {
'x-component': 'Input',
'x-component-props': {},
'x-validator': 'phone',
};
}
2022-11-07 01:11:53 +00:00
// Simplified field configuration
// email
{
interface: 'email',
name: 'email',
}
// phone
{
interface: 'phone',
name: 'phone',
}
```
2022-11-07 01:11:53 +00:00
[More Field Interface here](https://github.com/nocobase/nocobase/tree/main/packages/core/client/src/collection-manager/interfaces)