@ptc-org/nestjs-query-core also provides a number of base QueryServices that can be used to create custom query services. See the Services docs
All examples assume the following entity.
- TypeOrm
- Sequelize
- Mongoose
- Typegoose
- Example
- Example
- Example
- Example
todo-item.entity.ts
Creating a Service
Module
Thenestjs-query typeorm, sequelize, mongoose, and typegoose packages provide a module that will add providers to inject auto-created QueryServices using the @InjectQueryService decorator.
In order to use the decorator you will need to use the module that comes with the nestjs-query orm module providing it your entities that you want the services created for.
- TypeOrm
- Sequelize
- Mongoose
- Typegoose
- Example
- Example
- Example
- Example
todo-item.module.ts
Decorator
Once you have imported the correct module, use@InjectQueryService decorator to inject a QueryService into your class or resolver.
todo-item.resolver.ts
The above resolver is an example of manually defining the resolver, if you use the
NestjsQueryGraphQLModule you do not need to
define a resolver.In the above example the DTO and entity are the same shape, if you have a case where they are different or have computed fields
check out Assemblers to understand how to convert to and from the DTO/Entity.
Querying
Thenestjs-query QueryService uses a common Query interface that allows you use a common type regardless of the persistence library in use.
To query for records from your service you can use the query method which will return a Promise of an array of entities. To read more about querying take a look at the Queries Doc.
Example
Get all recordsFiltering
Thefilter option is translated to a WHERE clause.
Example
To find all completedTodoItems by use can use the is operator.
Sorting
Thesorting option is translated to a ORDER BY.
Example
Sorting records bycompleted and title.
Paging
Thepaging option is translated to LIMIT and OFFSET.
Example
Skip the first 20 records and return the next 10.Find By Id
To find a single record you can use thefindById method.
Example
Get By Id
ThegetById method is the same as the findById with one key difference, it will throw an exception if the record is not found.
Example
Aggregating
To perform anaggregate query you can use the aggregate method which accepts a Filter and AggregateQuery.
Supported aggregates are count, sum, avg, min and max.
In this example we’ll aggregate on all records.
Creating
Create One
To create a single record use thecreateOne method.
Example
Create Many
To create multiple records use thecreateMany method.
Example
Updating
Update One
To update a single record use theupdateOne method.
Example
Updates the record with an id equal to 1 to completed.Update Many
To update multiple records use theupdateMany method.
NOTE This method returns a UpdateManyResponse which contains the updated record count.
Example
Updates allTodoItemEntities to completed if their title ends in Bar
Deleting
Delete One
To delete a single record use thedeleteOne method.
Example
Delete the record with an id equal to 1.Delete Many
To delete multiple records use thedeleteMany method.
NOTE This method returns a DeleteManyResponse which contains the deleted record count.
Example
Delete allTodoItemEntities older than Jan 1, 2019.
Foreign Keys
It is a common use case to include a foreign key from your entity in your DTO. To do this you should add the foreign key to your entity as well as your DTO.This section only applies when using typeorm and sequelize with relations
Example
Assume TodoItems can have SubTasks we would set up our SubTaskEntity using the following- TypeOrm
- Sequelize
- Example
- Example
sub-task.entity.ts
todoItemId to the SubTaskDTO.
sub-task.dto.ts
Relations
This section only applies when you combine your DTO and entity and are using Typeorm or Sequelize
@Field or @FilterableField.
Instead decorate the class with @CursorConnection, @OffsetConnection, @UnPagedRelation or @Relation.
Example
Assume you have the following subtask definition.- TypeOrm
- Sequelize
- Example
- Example
sub-task.ts
todoItem is not decorated with a field decorator, instead it is exposed through the @Relation decorator.
Edit this page