Collection is used to define data models in the system, such as model names, fields, indexes, associations, and other information.
It is generally called through the collection method of a Database instance as a proxy entry.
For more field types, please refer to Fields.
Signature
constructor(options: CollectionOptions, context: CollectionContext)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
options.name | string | - | collection identifier |
options.tableName? | string | - | Database table name. If not provided, the value of options.name will be used. |
options.fields? | FieldOptions[] | - | Field definitions. See Field for details. |
options.model? | string | ModelStatic<Model> | - | Sequelize Model type. If a string is used, the model name must have been previously registered on the db. |
options.repository? | string | RepositoryType | - | Repository type. If a string is used, the repository type must have been previously registered on the db. |
options.sortable? | string | boolean | { name?: string; scopeKey?: string } | - | Sortable field configuration. Not sortable by default. |
options.autoGenId? | boolean | true | Whether to automatically generate a unique primary key. Defaults to true. |
context.database | Database | - | The database in the current context. |
Example
Create a posts collection:
optionsInitial configuration parameters for the collection. Same as the options parameter of the constructor.
contextThe context to which the current collection belongs, currently mainly the database instance.
nameCollection name.
dbThe database instance it belongs to.
filterTargetKeyThe field name used as the primary key.
isThroughWhether it is a through collection.
modelMatches the Sequelize Model type.
repositoryRepository instance.
getField()Gets the field object with the corresponding name defined in the collection.
Signature
getField(name: string): FieldParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | - | Field name |
Example
setField()Sets a field for the collection.
Signature
setField(name: string, options: FieldOptions): FieldParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | - | Field name |
options | FieldOptions | - | Field configuration. See Field for details. |
Example
setFields()Sets multiple fields for the collection in batch.
Signature
setFields(fields: FieldOptions[], resetFields = true): Field[]Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
fields | FieldOptions[] | - | Field configuration. See Field for details. |
resetFields | boolean | true | Whether to reset existing fields. |
Example
removeField()Removes the field object with the corresponding name defined in the collection.
Signature
removeField(name: string): void | FieldParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | - | Field name |
Example
resetFields()Resets (clears) the fields of the collection.
Signature
resetFields(): voidExample
hasField()Checks if a field object with the corresponding name is defined in the collection.
Signature
hasField(name: string): booleanParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | - | Field name |
Example
findField()Finds a field object in the collection that meets the criteria.
Signature
findField(predicate: (field: Field) => boolean): Field | undefinedParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
predicate | (field: Field) => boolean | - | Search criteria |
Example
forEachField()Iterates over the field objects in the collection.
Signature
forEachField(callback: (field: Field) => void): voidParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callback | (field: Field) => void | - | Callback function |
Example
addIndex()Adds an index to the collection.
Signature
addIndex(index: string | string[] | { fields: string[], unique?: boolean,[key: string]: any })Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
index | string | string[] | - | Field name(s) to be indexed. |
index | { fields: string[], unique?: boolean, [key: string]: any } | - | Full configuration. |
Example
removeIndex()Removes an index from the collection.
Signature
removeIndex(fields: string[])Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
fields | string[] | - | Combination of field names for the index to be removed. |
Example
remove()Deletes the collection.
Signature
remove(): voidExample
sync()Syncs the collection definition to the database. In addition to the default logic of Model.sync in Sequelize, it also processes collections corresponding to association fields.
Signature
sync(): Promise<void>Example
existsInDb()Checks if the collection exists in the database.
Signature
existsInDb(options?: Transactionable): Promise<boolean>Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
options?.transaction | Transaction | - | Transaction instance |
Example
removeFromDb()Signature
removeFromDb(): Promise<void>Example