Collection
Overview
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.
Constructor
Signature
constructor(options: CollectionOptions, context: CollectionContext)
Parameters
Example
Create a posts collection:
Instance Members
options
Initial configuration parameters for the collection. Same as the options parameter of the constructor.
context
The context to which the current collection belongs, currently mainly the database instance.
name
Collection name.
db
The database instance it belongs to.
filterTargetKey
The field name used as the primary key.
isThrough
Whether it is a through collection.
model
Matches the Sequelize Model type.
repository
Repository instance.
Field Configuration Methods
getField()
Gets the field object with the corresponding name defined in the collection.
Signature
getField(name: string): Field
Parameters
Example
setField()
Sets a field for the collection.
Signature
setField(name: string, options: FieldOptions): Field
Parameters
Example
setFields()
Sets multiple fields for the collection in batch.
Signature
setFields(fields: FieldOptions[], resetFields = true): Field[]
Parameters
Example
removeField()
Removes the field object with the corresponding name defined in the collection.
Signature
removeField(name: string): void | Field
Parameters
Example
resetFields()
Resets (clears) the fields of the collection.
Signature
resetFields(): void
Example
hasField()
Checks if a field object with the corresponding name is defined in the collection.
Signature
hasField(name: string): boolean
Parameters
Example
findField()
Finds a field object in the collection that meets the criteria.
Signature
findField(predicate: (field: Field) => boolean): Field | undefined
Parameters
Example
forEachField()
Iterates over the field objects in the collection.
Signature
forEachField(callback: (field: Field) => void): void
Parameters
Example
Index Configuration Methods
addIndex()
Adds an index to the collection.
Signature
addIndex(index: string | string[] | { fields: string[], unique?: boolean,[key: string]: any })
Parameters
Example
removeIndex()
Removes an index from the collection.
Signature
removeIndex(fields: string[])
Parameters
Example
Collection Configuration Methods
remove()
Deletes the collection.
Signature
remove(): void
Example
Database Operation Methods
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
Example
removeFromDb()
Signature
removeFromDb(): Promise<void>
Example

