Skip to content

Commit

Permalink
feat(typegoose): Add typegoose package (#846)
Browse files Browse the repository at this point in the history
* wip: Typegoose implementation for nestjs-query

* feat: Implementation of the rest of QueryService and Tests

* fix: adding in assemblers that were missed

* docs: Adding documentation for typegoose

* docs: Updating install guides to include Typegoose

* docs: Updating the getting-started documentation

* feat: adding the ability to add and remove items from an array using a specific UpdateArray Input type.

* feat: adds typegoose e2e testing

* tests: fixes failing typegoose unit tests

* chore: remove commented code

* feat(typegoose): Getting tests passing

* Initial work

* feat(typegoose): WIP

* feat(typegoose): WIP

* feat(typegoose): WIP

* feat(typegoose): small corrections

* chore(): Update typegoose example to use QueryOptions decorator

* chore(): Fix linting issues

Co-authored-by: John McInall <johnmcinall@odro.co.uk>
Co-authored-by: John McInall <johnmcinall@pop-os.localdomain>
Co-authored-by: Scott <scott.molinari@adduco.de>
Co-authored-by: John McInall <johnmcinall@gmail.com>
  • Loading branch information
5 people committed Mar 15, 2021
1 parent 0826966 commit 73cf5cd
Show file tree
Hide file tree
Showing 78 changed files with 8,260 additions and 23 deletions.
13 changes: 13 additions & 0 deletions .run/start - typegoose.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="start - typegoose" type="js.build_tools.npm">
<package-json value="$PROJECT_DIR$/examples/package.json" />
<command value="run" />
<scripts>
<script value="start" />
</scripts>
<arguments value="-- typegoose" />
<node-interpreter value="project" />
<envs />
<method v="2" />
</configuration>
</component>
2 changes: 1 addition & 1 deletion documentation/docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Running the documentation locally is the easiest way to view your changes to ens
To run the docs locally do the following:
```
cd ./documentation
npm run install # first time only
npm install # first time only
npm run start
```

Expand Down
91 changes: 91 additions & 0 deletions documentation/docs/introduction/example.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Add the following to `src/todo-item/todo-item.entity.ts`.
{ label: 'TypeOrm', value: 'typeorm', },
{ label: 'Sequelize', value: 'sequelize', },
{ label: 'Mongoose', value: 'mongoose', },
{ label: 'Typegoose', value: 'typegoose', },
]
}>
<TabItem value="typeorm">
Expand Down Expand Up @@ -149,6 +150,40 @@ export const TodoItemEntitySchema = SchemaFactory.createForClass(TodoItemEntity)

```

</TabItem>
<TabItem value="typegoose">

```ts title="todo-item/todo-item.entity.ts"
import { Base } from '@typegoose/typegoose/lib/defaultClasses';
import { Prop, modelOptions } from '@typegoose/typegoose';

@modelOptions({
schemaOptions: {
timestamps: true,
collection: 'todo-items',
toObject: { virtuals: true }
}
})
export class TodoItemEntity extends Base {

@Prop({ required: true })
title!: string;

@Prop()
description?: string;

@Prop({ required: true })
completed!: boolean;

@Prop({ default: Date.now })
createdAt!: Date;

@Prop({ default: Date.now })
updatedAt!: Date;
}

```

</TabItem>
</Tabs>

Expand Down Expand Up @@ -227,6 +262,7 @@ The `NestjsQueryGraphQLModule` will automatically create a Resolver that will ex
{ label: 'TypeOrm', value: 'typeorm', },
{ label: 'Sequelize', value: 'sequelize', },
{ label: 'Mongoose', value: 'mongoose', },
{ label: 'Typegoose', value: 'typegoose', },
]
}>
<TabItem value="typeorm">
Expand Down Expand Up @@ -304,6 +340,35 @@ import { TodoItemEntity, TodoItemEntitySchema } from './todo-item.entity';
export class TodoItemModule {}
```

</TabItem>
<TabItem value="typegoose">

```ts {9-19}
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';
import { Module } from '@nestjs/common';
import { NestjsQueryTypegooseModule } from '@nestjs-query/query-typegoose';
import { TodoItemDTO } from './dto/todo-item.dto';
import { TodoItemEntity } from './todo-item.entity';

const guards = [AuthGuard];
@Module({
providers: [TodoItemResolver],
imports: [
NestjsQueryGraphQLModule.forFeature({
imports: [
NestjsQueryTypegooseModule.forFeature([TodoItemEntity]),
],
resolvers: [
{
DTOClass: TodoItemDTO,
},
],
}),
],
})
export class TodoItemModule {}
```

</TabItem>
</Tabs>

Expand All @@ -317,6 +382,7 @@ Next update `app.module` to set up your db connection and the `graphql` nest mod
{ label: 'TypeOrm', value: 'typeorm', },
{ label: 'Sequelize', value: 'sequelize', },
{ label: 'Mongoose', value: 'mongoose', },
{ label: 'Typegoose', value: 'typegoose', },
]
}>
<TabItem value="typeorm">
Expand Down Expand Up @@ -414,6 +480,31 @@ export class AppModule {}

```

</TabItem>
<TabItem value="typegoose">

```ts
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { TypegooseModule } from 'nestjs-typegoose';
import { TodoItemModule } from './todo-item/todo-item.module';

@Module({
imports: [
TypegooseModule.forRoot('mongodb://localhost/nest', options),
GraphQLModule.forRoot({
// set to true to automatically generate schema
autoSchemaFile: true,
}),
TodoItemModule,
],
})
export class AppModule {}


```
**NOTE** For the sake of brevity, the `options` object in the Mongoose and Typegoose examples aren't defined. If you'd like to see full examples of all of the persistence services, please refer to the `./examples` directory in the [source code](https://github.com/doug-martin/nestjs-query/tree/master/examples).

</TabItem>
</Tabs>

Expand Down
1 change: 1 addition & 0 deletions documentation/docs/introduction/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Nestjs-query is composed of multiple packages
* [`@nestjs-query/query-typeorm`](https://github.com/doug-martin/nestjs-query/tree/master/packages/query-typeorm) - Package that implements a Typeorm service that can be used by itself or with the graphql resolver provided by `@nestjs-query/query-graphql`.
* [`@nestjs-query/query-sequelize`](https://github.com/doug-martin/nestjs-query/tree/master/packages/query-sequelize) - Package that implements a Sequelize service that can be used by itself or with the graphql resolver provided by `@nestjs-query/query-graphql`.
* [`@nestjs-query/query-mongoose`](https://github.com/doug-martin/nestjs-query/tree/master/packages/query-mongoose) - Package that implements a Mongoose service that can be used by itself or with the graphql resolver provided by `@nestjs-query/query-graphql`.
* [`@nestjs-query/query-typegoose`](https://github.com/doug-martin/nestjs-query/tree/master/packages/query-typegoose) - Package that implements a Typegoose service that can be used by itself or with the graphql resolver provided by `@nestjs-query/query-graphql`.

## Migration Guides

Expand Down
8 changes: 8 additions & 0 deletions documentation/docs/introduction/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ npm i @nestjs-query/query-mongoose @nestjs/common @nestjs/mongoose mongoose mong
```

**NOTE** `@nestjs-query/query-mongoose` has opted for peer dependencies to follow nest conventions and to prevent duplicate installations of packages.

## @nestjs-query/query-typegoose

```sh
npm i @nestjs-query/query-typegoose @nestjs/common nestjs/typegoose @typegoose/typegoose mongoose mongodb
```

**NOTE** `@nestjs-query/query-typegoose` has opted for peer dependencies to follow nest conventions and to prevent duplicate installations of packages.
1 change: 1 addition & 0 deletions documentation/docs/persistence/services.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The following ORMs are supported out of the box.
* [TypeOrm](./typeorm/getting-started.md)
* [Sequelize](./sequelize/getting-started.md)
* [Mongoose](./mongoose/getting-started.md)
* [Typegoose](./typegoose/getting-started.md)

`@nestjs-query/core` also provides a number of base `QueryService`s that can be used to create custom query services.
[See the Services docs](../concepts/services.md#service-helpers)
Expand Down
57 changes: 57 additions & 0 deletions documentation/docs/persistence/typegoose/custom-service.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: Custom Service
---

To create a custom query service to add your own methods to you can extend the `TypegooseQueryService`.

```ts title="todo-item.service.ts"
import { QueryService } from '@nestjs-query/core';
import { InjectModel } from '@nestjs/mongoose';
import { TypegooseQueryService } from '@nestjs-query/query-typegoose';
import { TodoItemEntity } from './entity/todo-item.entity';

@QueryService(TodoItemEntity)
export class TodoItemService extends TypegooseQueryService<TodoItemEntity> {
constructor(@InjectModel(TodoItemEntity) model: ReturnModelType<typeof TodoItemEntity>) {
super(model);
}

async markAllAsCompleted(): Promise<number> {
const entities = await this.query({ filter: { completed: { is: true } } });

const { updatedCount } = await this.updateMany(
{ completed: true }, // update
{ id: { in: entities.map((e) => e.id) } }, // filter
);
// do some other business logic
return updatedCount;
}
}
```

To use the custom service in the auto-generated resolver you can specify the `ServiceClass` option.

```ts title="todo-item.module.ts" {12,16}
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';
import { NestjsQueryTypegooseModule } from '@nestjs-query/query-typegoose';
import { Module } from '@nestjs/common';
import { TodoItemDTO } from './dto/todo-item.dto';
import { TodoItemEntity } from './todo-item.entity';
import { TodoItemService } from './todo-item.service';

@Module({
imports: [
NestjsQueryGraphQLModule.forFeature({
imports: [NestjsQueryTypegooseModule.forFeature([TodoItemEntity])],
services: [TodoItemService],
resolvers: [
{
DTOClass: TodoItemDTO,
ServiceClass: TodoItemService,
},
],
}),
],
})
export class TodoItemModule {}
```
18 changes: 18 additions & 0 deletions documentation/docs/persistence/typegoose/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Getting Started
---

The `@nestjs-query/query-typegoose` package provides an implementation of `@nestjs-query/core` [QueryService](../../concepts/services.md).

This package is built using [typegoose](https://typegoose.github.io/typegoose/) and [nestjs/typegoose](https://github.com/kpfromer/nestjs-typegoose#readme). If you are unfamiliar with them I suggest you read their documentation first.

## Installation

[Installation Docs](../../introduction/install.md#nestjs-queryquery-typegoose)

## Docs

- Read the [QueryService docs](../services.mdx)
- [Relations](./relations.mdx) - How to work with relations/references in `typegoose` with `nestjs-query`
- [Custom Service](./custom-service.md) - Example custom service
- [Serialization](./serialization.md) - How to serialize `typegoose` models.
Loading

0 comments on commit 73cf5cd

Please sign in to comment.