Repository
Overview
On a given Collection object, you can get its Repository object to perform read and write operations on the collection.
Query
Basic Query
On the Repository object, call find* related methods to perform query operations. All query methods support passing a filter parameter to filter data.
Operators
The filter parameter in Repository also provides a variety of operators to perform more diverse query operations.
For more details on operators, please refer to Filter Operators.
Field Control
When performing a query operation, you can control the output fields through the fields, except, and appends parameters.
fields: Specify output fieldsexcept: Exclude output fieldsappends: Append associated fields to the output
Querying Association Fields
The filter parameter supports filtering by association fields, for example:
Association fields can also be nested.
Sorting
You can sort the query results using the sort parameter.
You can also sort by the fields of associated objects.
Create
Basic Create
Create new data objects through the Repository.
Creating Associations
When creating, you can also create associated objects simultaneously. Similar to querying, nested use of associated objects is also supported, for example:
If the associated object already exists in the database, you can pass its ID to establish an association with it during creation.
Update
Basic Update
After getting a data object, you can directly modify its properties on the data object (Model) and then call the save method to save the changes.
The data object Model inherits from the Sequelize Model. For operations on the Model, please refer to Sequelize Model.
You can also update data through the Repository:
When updating, you can control which fields are updated using the whitelist and blacklist parameters, for example:
Updating Association Fields
When updating, you can set associated objects, for example:
Delete
You can call the destroy() method in the Repository to perform a delete operation. You need to specify filter criteria when deleting:
Constructor
Usually not called directly by developers. It is mainly instantiated after registering the type through db.registerRepositories() and specifying the corresponding registered repository type in the parameters of db.collection().
Signature
constructor(collection: Collection)
Example
Instance Members
database
The database management instance of the context.
collection
The corresponding collection management instance.
model
The corresponding model class.
Instance Methods
find()
Queries a dataset from the database, allowing specification of filter conditions, sorting, etc.
Signature
async find(options?: FindOptions): Promise<Model[]>
Type
Details
filter: Filter
Query condition used to filter data results. In the passed query parameters, the key is the field name to query, and the value can be the value to query or used with operators for other conditional data filtering.
For more operators, please refer to Query Operators.
filterByTk: TargetKey
Queries data by TargetKey, which is a convenient method for the filter parameter. The specific field for TargetKey can be configured in the Collection, defaulting to primaryKey.
fields: string[]
Query columns, used to control the data field results. After passing this parameter, only the specified fields will be returned.
except: string[]
Excluded columns, used to control the data field results. After passing this parameter, the passed fields will not be output.
appends: string[]
Appended columns, used to load associated data. After passing this parameter, the specified association fields will also be output.
sort: string[] | string
Specifies the sorting method for the query results. The parameter is the field name, which defaults to ascending asc order. For descending desc order, add a - symbol before the field name, e.g., ['-id', 'name'], which means sort by id desc, name asc.
limit: number
Limits the number of results, same as limit in SQL.
offset: number
Query offset, same as offset in SQL.
Example
findOne()
Queries a single piece of data from the database that meets specific criteria. Equivalent to Model.findOne() in Sequelize.
Signature
async findOne(options?: FindOneOptions): Promise<Model | null>
Example
count()
Queries the total number of data entries that meet specific criteria from the database. Equivalent to Model.count() in Sequelize.
Signature
count(options?: CountOptions): Promise<number>
Type
Example
findAndCount()
Queries a dataset and the total number of results that meet specific criteria from the database. Equivalent to Model.findAndCountAll() in Sequelize.
Signature
async findAndCount(options?: FindAndCountOptions): Promise<[Model[], number]>
Type
Details
The query parameters are the same as find(). The return value is an array where the first element is the query result and the second element is the total count.
create()
Inserts a new record into the collection. Equivalent to Model.create() in Sequelize. When the data object to be created carries information about relationship fields, the corresponding relationship data records will be created or updated as well.
Signature
async create<M extends Model>(options: CreateOptions): Promise<M>
Example
createMany()
Inserts multiple new records into the collection. Equivalent to calling the create() method multiple times.
Signature
createMany(options: CreateManyOptions): Promise<Model[]>
Type
Details
records: An array of data objects for the records to be created.transaction: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
Example
update()
Updates data in the collection. Equivalent to Model.update() in Sequelize. When the data object to be updated carries information about relationship fields, the corresponding relationship data records will be created or updated as well.
Signature
async update<M extends Model>(options: UpdateOptions): Promise<M>
Example
destroy()
Deletes data from the collection. Equivalent to Model.destroy() in Sequelize.
Signature
async destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<number>
Type
Details
filter: Specifies the filter conditions for the records to be deleted. For detailed usage of Filter, refer to thefind()method.filterByTk: Specifies the filter conditions for the records to be deleted by TargetKey.truncate: Whether to truncate the collection data, effective when nofilterorfilterByTkparameter is passed.transaction: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.

