nocobase/docs/en-US/api/database/collection.md

511 lines
9.0 KiB
Markdown
Raw Normal View History

# Collection
2023-01-15 14:20:12 +00:00
## Overview
2022-11-13 15:00:59 +00:00
2023-01-15 14:25:21 +00: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 15:00:59 +00:00
```javascript
const { Database } = require('@nocobase/database')
2023-01-15 14:20:12 +00:00
// Create database instance
2022-11-13 15:00:59 +00:00
const db = new Database({...});
2023-01-15 14:20:12 +00:00
// Define data model
2022-11-13 15:00:59 +00:00
db.collection({
name: 'users',
2023-01-15 14:20:12 +00:00
// Define model fields
2022-11-13 15:00:59 +00:00
fields: [
2023-01-15 14:20:12 +00:00
// Scalar field
2022-11-13 15:00:59 +00:00
{
name: 'name',
type: 'string',
},
2023-01-15 14:20:12 +00:00
// Association field
2022-11-13 15:00:59 +00:00
{
name: 'profile',
type: 'hasOne' // 'hasMany', 'belongsTo', 'belongsToMany'
}
],
});
```
2023-01-15 14:20:12 +00:00
Refer to [Fields](/api/database/field.md) for more field types.
2023-01-15 14:20:12 +00:00
## Constructor
2023-01-15 14:20:12 +00:00
**Signature**
* `constructor(options: CollectionOptions, context: CollectionContext)`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:25:21 +00:00
| `options.name` | `string` | - | Identifier of the collection |
2023-01-15 14:20:12 +00:00
| `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 |
2023-01-15 14:25:21 +00:00
| `options.sortable?` | `string \| boolean \| { name?: string; scopeKey?: string }` | - | Configure which fields are sortable; not sortable by default |
2023-01-15 14:20:12 +00:00
| `options.autoGenId?` | `boolean` | `true` | Whether to automatically generate unique primary key; `true` by default |
| `context.database` | `Database` | - | The context database in which it resides |
2023-01-15 14:20:12 +00:00
**Example**
2023-01-15 14:20:12 +00:00
Create a table <i>posts</i>:
```ts
const posts = new Collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
},
{
type: 'double',
name: 'price',
}
]
}, {
2023-01-15 14:20:12 +00:00
// An existing database instance
database: db
});
```
2023-02-05 04:33:01 +00:00
## Instance Members
### `options`
2023-01-15 14:20:12 +00:00
Initial parameters for data table configuration, which are consistent with the `options` parameter of the constructor.
### `context`
2023-01-15 14:20:12 +00:00
The contextual environment to which the current data table belongs, currently mainly the database instance.
### `name`
2023-01-15 14:20:12 +00:00
Name of the data table.
### `db`
2023-01-15 14:20:12 +00:00
The database instance to which it belongs.
### `filterTargetKey`
2023-01-15 14:20:12 +00:00
Name of the field that is used as the primary key.
### `isThrough`
2023-01-15 14:20:12 +00:00
Whether it is an intermediate table.
### `model`
2023-01-15 14:20:12 +00:00
Match the Model type of Sequelize.
### `repository`
2023-01-15 14:20:12 +00:00
Data repository instance.
2023-02-05 04:33:01 +00:00
## Field Configuration Methods
### `getField()`
2023-01-15 14:20:12 +00:00
Get a field object whose corresponding name has been defined in the data table.
2023-01-15 14:20:12 +00:00
**Signature**
* `getField(name: string): Field`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `name` | `string` | - | Name of the field |
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
const field = posts.getField('title');
```
### `setField()`
2023-01-15 14:20:12 +00:00
Set a field to the data table.
2023-01-15 14:20:12 +00:00
**Signature**
* `setField(name: string, options: FieldOptions): Field`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `name` | `string` | - | Name of the field |
| `options` | `FieldOptions` | - | Configuration of the field, refer to [Field](./field) for details |
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({ name: 'posts' });
posts.setField('title', { type: 'string' });
```
### `setFields()`
2023-01-15 14:20:12 +00:00
Set multiple fields to the data table.
2023-01-15 14:20:12 +00:00
**Signature**
* `setFields(fields: FieldOptions[], resetFields = true): Field[]`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `fields` | `FieldOptions[]` | - | Configuration of the fields, refer to [Field](./field) for details |
| `resetFields` | `boolean` | `true` | Whether to reset existing fields |
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({ name: 'posts' });
posts.setFields([
{ type: 'string', name: 'title' },
{ type: 'double', name: 'price' }
]);
```
### `removeField()`
2023-01-15 14:20:12 +00:00
Remove a field object whose corresponding name has been defined in the data table.
2023-01-15 14:20:12 +00:00
**Signature**
* `removeField(name: string): void | Field`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `name` | `string` | - | Name of the field |
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.removeField('title');
```
### `resetFields()`
2023-01-15 14:20:12 +00:00
Reset (Empty) fields of the data table.
2023-01-15 14:20:12 +00:00
**Signature**
* `resetFields(): void`
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.resetFields();
```
### `hasField()`
2023-01-15 14:20:12 +00:00
Check if the data table has defined a field object with the corresponding name.
2023-01-15 14:20:12 +00:00
**Signature**
* `hasField(name: string): boolean`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `name` | `string` | - | Name of the field |
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.hasField('title'); // true
```
### `findField()`
2023-01-15 14:20:12 +00:00
Find field objects in the data table that match the conditions.
2023-01-15 14:20:12 +00:00
**Signature**
* `findField(predicate: (field: Field) => boolean): Field | undefined`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `predicate` | `(field: Field) => boolean` | - | The condition |
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.findField(field => field.name === 'title');
```
### `forEachField()`
2023-01-15 14:20:12 +00:00
Iterate over field objects in the data table.
2023-01-15 14:20:12 +00:00
**Signature**
* `forEachField(callback: (field: Field) => void): void`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `callback` | `(field: Field) => void` | - | Callback function |
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.forEachField(field => console.log(field.name));
```
2023-02-05 04:33:01 +00:00
## Index Configuration Methods
### `addIndex()`
2023-01-15 14:20:12 +00:00
Add data table index.
2023-01-15 14:20:12 +00:00
**Signature**
* `addIndex(index: string | string[] | { fields: string[], unique?: boolean,[key: string]: any })`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `index` | `string \| string[]` | - | Names of fields to be indexed |
| `index` | `{ fields: string[], unique?: boolean, [key: string]: any }` | - | Full configuration |
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.addIndex({
fields: ['title'],
unique: true
});
```
### `removeIndex()`
2023-01-15 14:20:12 +00:00
Remove data table index.
2023-01-15 14:20:12 +00:00
**Signature**
* `removeIndex(fields: string[])`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `fields` | `string[]` | - | Names of fields to remove indexes |
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
],
indexes: [
{
fields: ['title'],
unique: true
}
]
});
posts.removeIndex(['title']);
```
2023-02-05 04:33:01 +00:00
## Table Configuration Methods
### `remove()`
2023-01-15 14:20:12 +00:00
Remove data table.
2023-01-15 14:20:12 +00:00
**Signature**
* `remove(): void`
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
posts.remove();
```
2023-02-05 04:33:01 +00:00
## Database Operation Methods
### `sync()`
2023-01-15 14:20:12 +00: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.
2023-01-15 14:20:12 +00:00
**Signature**
* `sync(): Promise<void>`
2023-01-15 14:20:12 +00:00
**Example**
```ts
const posts = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
await posts.sync();
```
### `existsInDb()`
2023-01-15 14:20:12 +00:00
Check whether the data table exists in the database.
2023-01-15 14:20:12 +00:00
**Signature**
* `existsInDb(options?: Transactionable): Promise<boolean>`
2023-01-15 14:20:12 +00:00
**Parameter**
2023-01-15 14:20:12 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-01-15 14:20:12 +00:00
| `options?.transaction` | `Transaction` | - | Transaction instance |
2023-01-15 14:20:12 +00:00
**Example**
```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 14:20:12 +00:00
**Signature**
* `removeFromDb(): Promise<void>`
2023-01-15 14:20:12 +00:00
**Example**
```ts
const books = db.collection({
name: 'books'
});
2023-01-15 14:20:12 +00:00
// Synchronize the table books to the database
await db.sync();
2023-01-15 14:20:12 +00:00
// Remove the table books from the database
await books.removeFromDb();
```