2022-10-31 11:52:17 +08:00
# Collection
2023-01-15 22:20:12 +08:00
## Overview
2022-11-13 23:00:59 +08:00
2023-01-15 22:20:12 +08:00
`Collection` is used to define the data model in the system, such as model name, fields, indexes, associations and other information. It is usually called through the `collection` method of the `Database` instance as a proxy entry.
2022-11-13 23:00:59 +08:00
```javascript
const { Database } = require('@nocobase/database')
2023-01-15 22:20:12 +08:00
// Create database instance
2022-11-13 23:00:59 +08:00
const db = new Database({...});
2023-01-15 22:20:12 +08:00
// Define data model
2022-11-13 23:00:59 +08:00
db.collection({
name: 'users',
2023-01-15 22:20:12 +08:00
// Define model fields
2022-11-13 23:00:59 +08:00
fields: [
2023-01-15 22:20:12 +08:00
// Scalar field
2022-11-13 23:00:59 +08:00
{
name: 'name',
type: 'string',
},
2023-01-15 22:20:12 +08:00
// Association field
2022-11-13 23:00:59 +08:00
{
name: 'profile',
type: 'hasOne' // 'hasMany', 'belongsTo', 'belongsToMany'
}
],
});
```
2023-01-15 22:20:12 +08:00
Refer to [Fields ](/api/database/field.md ) for more field types.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
## Constructor
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `constructor(options: CollectionOptions, context: CollectionContext)`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `options.name` | `string` | - | collection identifier |
| `options.tableName?` | `string` | - | Database table name, the value of `options.name` is used if not set |
| `options.fields?` | `FieldOptions[]` | - | Definition of fields, refer to [Field ](./field ) for details |
| `options.model?` | `string \| ModelStatic<Model>` | - | Model type of Sequelize; in case `string` is used, this model name needs to be registered in the db before being called |
| `options.repository?` | `string \| RepositoryType` | - | Data repository type; in case `string` is used, this repository type needs to be registered in the db before being called |
| `options.sortable?` | `string \| boolean \| { name?: string; scopeKey?: string }` | - | Configure which fields are sortable ; not sortable by default |
| `options.autoGenId?` | `boolean` | `true` | Whether to automatically generate unique primary key; `true` by default |
| `context.database` | `Database` | - | The context database in which it resides |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
Create a table < i > posts< / i > :
2022-10-31 11:52:17 +08:00
```ts
const posts = new Collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
},
{
type: 'double',
name: 'price',
}
]
}, {
2023-01-15 22:20:12 +08:00
// An existing database instance
2022-10-31 11:52:17 +08:00
database: db
});
```
2023-01-15 22:20:12 +08:00
## Instance Member
2022-10-31 11:52:17 +08:00
### `options`
2023-01-15 22:20:12 +08:00
Initial parameters for data table configuration, which are consistent with the `options` parameter of the constructor.
2022-10-31 11:52:17 +08:00
### `context`
2023-01-15 22:20:12 +08:00
The contextual environment to which the current data table belongs, currently mainly the database instance.
2022-10-31 11:52:17 +08:00
### `name`
2023-01-15 22:20:12 +08:00
Name of the data table.
2022-10-31 11:52:17 +08:00
### `db`
2023-01-15 22:20:12 +08:00
The database instance to which it belongs.
2022-10-31 11:52:17 +08:00
### `filterTargetKey`
2023-01-15 22:20:12 +08:00
Name of the field that is used as the primary key.
2022-10-31 11:52:17 +08:00
### `isThrough`
2023-01-15 22:20:12 +08:00
Whether it is an intermediate table.
2022-10-31 11:52:17 +08:00
### `model`
2023-01-15 22:20:12 +08:00
Match the Model type of Sequelize.
2022-10-31 11:52:17 +08:00
### `repository`
2023-01-15 22:20:12 +08:00
Data repository instance.
2022-10-31 11:52:17 +08:00
## 字段配置方法
### `getField()`
2023-01-15 22:20:12 +08:00
Get a field object whose corresponding name has been defined in the data table.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `getField(name: string): Field`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `name` | `string` | - | Name of the field |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
const field = posts.getField('title');
```
### `setField()`
2023-01-15 22:20:12 +08:00
Set a field to the data table.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `setField(name: string, options: FieldOptions): Field`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `name` | `string` | - | Name of the field |
| `options` | `FieldOptions` | - | Configuration of the field, refer to [Field ](./field ) for details |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({ name: 'posts' });
posts.setField('title', { type: 'string' });
```
### `setFields()`
2023-01-15 22:20:12 +08:00
Set multiple fields to the data table.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `setFields(fields: FieldOptions[], resetFields = true): Field[]`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `fields` | `FieldOptions[]` | - | Configuration of the fields, refer to [Field ](./field ) for details |
| `resetFields` | `boolean` | `true` | Whether to reset existing fields |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({ name: 'posts' });
posts.setFields([
{ type: 'string', name: 'title' },
{ type: 'double', name: 'price' }
]);
```
### `removeField()`
2023-01-15 22:20:12 +08:00
Remove a field object whose corresponding name has been defined in the data table.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `removeField(name: string): void | Field`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `name` | `string` | - | Name of the field |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.removeField('title');
```
### `resetFields()`
2023-01-15 22:20:12 +08:00
Reset (Empty) fields of the data table.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `resetFields(): void`
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.resetFields();
```
### `hasField()`
2023-01-15 22:20:12 +08:00
Check if the data table has defined a field object with the corresponding name.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `hasField(name: string): boolean`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `name` | `string` | - | Name of the field |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.hasField('title'); // true
```
### `findField()`
2023-01-15 22:20:12 +08:00
Find field objects in the data table that match the conditions.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `findField(predicate: (field: Field) => boolean): Field | undefined`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `predicate` | `(field: Field) => boolean` | - | The condition |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.findField(field => field.name === 'title');
```
### `forEachField()`
2023-01-15 22:20:12 +08:00
Iterate over field objects in the data table.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `forEachField(callback: (field: Field) => void): void`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `callback` | `(field: Field) => void` | - | Callback function |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.forEachField(field => console.log(field.name));
```
2023-01-15 22:20:12 +08:00
## Index Configuration Method
2022-10-31 11:52:17 +08:00
### `addIndex()`
2023-01-15 22:20:12 +08:00
Add data table index.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `addIndex(index: string | string[] | { fields: string[], unique?: boolean,[key: string]: any })`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `index` | `string \| string[]` | - | Names of fields to be indexed |
| `index` | `{ fields: string[], unique?: boolean, [key: string]: any }` | - | Full configuration |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.addIndex({
fields: ['title'],
unique: true
});
```
### `removeIndex()`
2023-01-15 22:20:12 +08:00
Remove data table index.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `removeIndex(fields: string[])`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `fields` | `string[]` | - | Names of fields to remove indexes |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
],
indexes: [
{
fields: ['title'],
unique: true
}
]
});
posts.removeIndex(['title']);
```
2023-01-15 22:20:12 +08:00
## Table Configuration Method
2022-10-31 11:52:17 +08:00
### `remove()`
2023-01-15 22:20:12 +08:00
Remove data table.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `remove(): void`
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.remove();
```
2023-01-15 22:20:12 +08:00
## Database Operation Method
2022-10-31 11:52:17 +08:00
### `sync()`
2023-01-15 22:20:12 +08:00
Synchronize the definitions in data table to the database. In addition to the default `Model.sync` logic in Sequelize, the data tables corresponding to the relational fields will also be handled together.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `sync(): Promise<void>`
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
await posts.sync();
```
### `existsInDb()`
2023-01-15 22:20:12 +08:00
Check whether the data table exists in the database.
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `existsInDb(options?: Transactionable): Promise<boolean>`
2023-01-15 22:20:12 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-01-15 22:20:12 +08:00
| `options?.transaction` | `Transaction` | - | Transaction instance |
2022-10-31 11:52:17 +08:00
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
const existed = await posts.existsInDb();
console.log(existed); // false
```
### `removeFromDb()`
2023-01-15 22:20:12 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `removeFromDb(): Promise<void>`
2023-01-15 22:20:12 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const books = db.collection({
name: 'books'
});
2023-01-15 22:20:12 +08:00
// Synchronize the table books to the database
2022-10-31 11:52:17 +08:00
await db.sync();
2023-01-15 22:20:12 +08:00
// Remove the table books from the database
2022-10-31 11:52:17 +08:00
await books.removeFromDb();
```