nocobase/docs/en-US/development/server/collections/collection-template.md

83 lines
1.3 KiB
Markdown
Raw Normal View History

2022-11-07 01:45:44 +00:00
# Collection templates
<Alert>
2022-11-07 01:45:44 +00:00
📢 Collection templates are scheduled to be available in Q4 2022.
</Alert>
2022-11-07 01:45:44 +00:00
In real business scenarios, different collections may have their own initialization rules and business logic, and NocoBase addresses such issues by providing collection templates.
2022-11-07 01:45:44 +00:00
## General collections
```ts
db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
],
});
```
2022-11-07 01:45:44 +00:00
## Tree structure collections
```ts
db.collection({
name: 'categories',
tree: 'adjacency-list',
fields: [
{
type: 'string',
name: 'name',
},
{
type: 'string',
name: 'description',
},
{
type: 'belongsTo',
name: 'parent',
target: 'categories',
foreignKey: 'parentId',
},
{
type: 'hasMany',
name: 'children',
target: 'categories',
foreignKey: 'parentId',
},
],
});
```
2022-11-07 01:45:44 +00:00
## Parent-child inheritance collections
```ts
db.collection({
name: 'a',
fields: [
],
});
db.collection({
name: 'b',
inherits: 'a',
fields: [
],
});
```
2022-11-07 01:45:44 +00:00
## More templates
2022-11-07 01:45:44 +00:00
As in the case of calendar collections, each initialized collection needs to be initialized with special cron and exclude fields, and the definition of such fields is done by the template
```ts
db.collection({
name: 'events',
template: 'calendar',
});
```