Skip to content

Commit

Permalink
Merge pull request #448 from aXenDeveloper/database/allow_types_from_…
Browse files Browse the repository at this point in the history
…plugins

feat(backend)!: Expose database service to allow type safe from plugins
  • Loading branch information
aXenDeveloper authored Aug 14, 2024
2 parents 575a353 + d76a34a commit 0ec649a
Show file tree
Hide file tree
Showing 108 changed files with 520 additions and 587 deletions.
136 changes: 68 additions & 68 deletions __old_docs/pages/docs/plugins/database/pagination.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Callout } from "nextra/components";
import { Callout } from 'nextra/components';

# Pagination

Expand All @@ -14,7 +14,7 @@ Here is an example of how to use the pagination system based on `blog_categories

We will create a `DTO` to create arguments and return values for the query.

import { Steps } from "nextra/components";
import { Steps } from 'nextra/components';

<Steps>

Expand All @@ -25,9 +25,9 @@ File: `show/dto/show.args.ts`
Arguments used as container for fields to create query in GraphQL schema.

```ts
import { ArgsType } from "@nestjs/graphql";
import { ArgsType } from '@nestjs/graphql';

import { PaginationArgs } from "@/types/database/pagination.type";
import { PaginationArgs } from '@/types/database/pagination.type';

@ArgsType()
export class ShowBlogCategoriesArgs extends PaginationArgs {}
Expand All @@ -38,9 +38,9 @@ export class ShowBlogCategoriesArgs extends PaginationArgs {}
If you want to use `InputType` you can use `PaginationInput` from `pagination.type`.

```ts
import { ArgsType } from "@nestjs/graphql";
import { ArgsType } from '@nestjs/graphql';

import { PaginationInput } from "@/types/database/pagination.type";
import { PaginationInput } from '@/types/database/pagination.type';

@InputType()
export class ShowBlogCategoriesArgs extends PaginationInput {}
Expand All @@ -53,10 +53,10 @@ File: `show/dto/show.obj.ts`
Object used as container for fields to create return values query in GraphQL schema.

```ts
import { Field, Int, ObjectType } from "@nestjs/graphql";
import { Field, Int, ObjectType } from '@nestjs/graphql';

import { PageInfo } from "@/types/database/pagination.type";
import { TextLanguage } from "@/types/database/text-language.type";
import { PageInfo } from '@/types/database/pagination.type';
import { TextLanguage } from '@/types/database/text-language.type';

@ObjectType()
export class ShowBlogCategories {
Expand Down Expand Up @@ -94,21 +94,21 @@ File: `show/show.service.ts`
Inside service file we will create a `show` method that will return a `ShowBlogCategoriesObj` object.

```ts
import { Injectable } from "@nestjs/common";
import { Injectable } from '@nestjs/common';

import { ShowBlogCategoriesArgs } from "./dto/show.args";
import { ShowBlogCategoriesObj } from "./dto/show.obj";
import { ShowBlogCategoriesArgs } from './dto/show.args';
import { ShowBlogCategoriesObj } from './dto/show.obj';

import { DatabaseService } from "@/plugins/database/database.service";
import { DatabaseService } from '@/plugins/database/database.service';

@Injectable()
export class ShowBlogCategoriesService {
constructor(private databaseService: DatabaseService) {}
constructor(private databaseServices: DatabaseService) {}

async show({
cursor,
first,
last
last,
}: ShowBlogCategoriesArgs): Promise<ShowBlogCategoriesObj> {}
}
```
Expand All @@ -118,24 +118,24 @@ export class ShowBlogCategoriesService {
We will use `inputPaginationCursor()` to create initial values for pagination.

```ts {7-9, 20-35}
import { Injectable } from "@nestjs/common";
import { Injectable } from '@nestjs/common';

import { ShowBlogCategoriesArgs } from "./dto/show.args";
import { ShowBlogCategoriesObj } from "./dto/show.obj";
import { ShowBlogCategoriesArgs } from './dto/show.args';
import { ShowBlogCategoriesObj } from './dto/show.obj';

import { DatabaseService } from "@/plugins/database/database.service";
import { inputPaginationCursor } from "@/functions/database/pagination";
import { blog_categories } from "../../admin/database/schema/categories";
import { SortDirectionEnum } from "@/types/database/sortDirection.type";
import { DatabaseService } from '@/plugins/database/database.service';
import { inputPaginationCursor } from '@/functions/database/pagination';
import { blog_categories } from '../../admin/database/schema/categories';
import { SortDirectionEnum } from '@/types/database/sortDirection.type';

@Injectable()
export class ShowBlogCategoriesService {
constructor(private databaseService: DatabaseService) {}
constructor(private databaseServices: DatabaseService) {}

async show({
cursor,
first,
last
last,
}: ShowBlogCategoriesArgs): Promise<ShowBlogCategoriesObj> {
const pagination = await inputPaginationCursor({
cursor,
Expand All @@ -145,13 +145,13 @@ export class ShowBlogCategoriesService {
last,
primaryCursor: {
direction: SortDirectionEnum.asc,
column: "id",
schema: blog_categories.id
column: 'id',
schema: blog_categories.id,
},
defaultSortBy: {
direction: SortDirectionEnum.asc,
column: "position"
}
column: 'position',
},
});
}
}
Expand All @@ -164,12 +164,12 @@ We will use `findMany()` method to get the data from the database with `with` ar
```ts {27-29}
@Injectable()
export class ShowBlogCategoriesService {
constructor(private databaseService: DatabaseService) {}
constructor(private databaseServices: DatabaseService) {}

async show({
cursor,
first,
last
last,
}: ShowBlogCategoriesArgs): Promise<ShowBlogCategoriesObj> {
const pagination = await inputPaginationCursor({
cursor,
Expand All @@ -179,17 +179,17 @@ export class ShowBlogCategoriesService {
last,
primaryCursor: {
direction: SortDirectionEnum.asc,
column: "id",
schema: blog_categories.id
column: 'id',
schema: blog_categories.id,
},
defaultSortBy: {
direction: SortDirectionEnum.asc,
column: "position"
}
column: 'position',
},
});

const edges = await this.databaseService.db.query.blog_categories.findMany({
...pagination
...pagination,
});
}
}
Expand All @@ -200,25 +200,25 @@ export class ShowBlogCategoriesService {
If you want to use `where` argument you can pass it to the `findMany()` method using `and()` method from `drizzle-orm`.

```ts {2, 38}
import { Injectable } from "@nestjs/common";
import { and, lte } from "drizzle-orm";
import { Injectable } from '@nestjs/common';
import { and, lte } from 'drizzle-orm';

import { ShowBlogCategoriesArgs } from "./dto/show.args";
import { ShowBlogCategoriesObj } from "./dto/show.obj";
import { ShowBlogCategoriesArgs } from './dto/show.args';
import { ShowBlogCategoriesObj } from './dto/show.obj';

import { DatabaseService } from "@/plugins/database/database.service";
import { inputPaginationCursor } from "@/functions/database/pagination";
import { blog_categories } from "../../admin/database/schema/categories";
import { SortDirectionEnum } from "@/types/database/sortDirection.type";
import { DatabaseService } from '@/plugins/database/database.service';
import { inputPaginationCursor } from '@/functions/database/pagination';
import { blog_categories } from '../../admin/database/schema/categories';
import { SortDirectionEnum } from '@/types/database/sortDirection.type';

@Injectable()
export class ShowBlogCategoriesService {
constructor(private databaseService: DatabaseService) {}
constructor(private databaseServices: DatabaseService) {}

async show({
cursor,
first,
last
last,
}: ShowBlogCategoriesArgs): Promise<ShowBlogCategoriesObj> {
const pagination = await inputPaginationCursor({
cursor,
Expand All @@ -228,20 +228,20 @@ export class ShowBlogCategoriesService {
last,
primaryCursor: {
direction: SortDirectionEnum.asc,
column: "id",
schema: blog_categories.id
column: 'id',
schema: blog_categories.id,
},
defaultSortBy: {
direction: SortDirectionEnum.asc,
column: "position"
}
column: 'position',
},
});

const where = lte(blog_categories.created, new Date());

const edges = await this.databaseService.db.query.blog_categories.findMany({
...pagination,
where: and(pagination.where, where)
where: and(pagination.where, where),
});
}
}
Expand All @@ -252,28 +252,28 @@ export class ShowBlogCategoriesService {
We will use `outputPagination()` to create return values for the query. Remember to create a query `totalCount` to get the total count of the query.

```ts {2, 10, 45-47, 49}
import { Injectable } from "@nestjs/common";
import { count } from "drizzle-orm";
import { Injectable } from '@nestjs/common';
import { count } from 'drizzle-orm';

import { ShowBlogCategoriesArgs } from "./dto/show.args";
import { ShowBlogCategoriesObj } from "./dto/show.obj";
import { ShowBlogCategoriesArgs } from './dto/show.args';
import { ShowBlogCategoriesObj } from './dto/show.obj';

import { DatabaseService } from "@/plugins/database/database.service";
import { DatabaseService } from '@/plugins/database/database.service';
import {
inputPaginationCursor,
outputPagination
} from "@/functions/database/pagination";
import { blog_categories } from "../../admin/database/schema/categories";
import { SortDirectionEnum } from "@/types/database/sortDirection.type";
outputPagination,
} from '@/functions/database/pagination';
import { blog_categories } from '../../admin/database/schema/categories';
import { SortDirectionEnum } from '@/types/database/sortDirection.type';

@Injectable()
export class ShowBlogCategoriesService {
constructor(private databaseService: DatabaseService) {}
constructor(private databaseServices: DatabaseService) {}

async show({
cursor,
first,
last
last,
}: ShowBlogCategoriesArgs): Promise<ShowBlogCategoriesObj> {
const pagination = await inputPaginationCursor({
cursor,
Expand All @@ -283,17 +283,17 @@ export class ShowBlogCategoriesService {
last,
primaryCursor: {
direction: SortDirectionEnum.asc,
column: "id",
schema: blog_categories.id
column: 'id',
schema: blog_categories.id,
},
defaultSortBy: {
direction: SortDirectionEnum.asc,
column: "position"
}
column: 'position',
},
});

const edges = await this.databaseService.db.query.blog_categories.findMany({
...pagination
...pagination,
});

const totalCount = await this.databaseService.db
Expand Down Expand Up @@ -323,12 +323,12 @@ return outputPagination({
edges: edges.map(edge => ({
...edge,
name: edge.title,
description: edge.description
description: edge.description,
})),
totalCount,
first,
cursor,
last
last,
});
```

Expand Down
2 changes: 1 addition & 1 deletion __old_docs/pages/docs/plugins/database/search.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { User } from '@/utils/decorators/user.decorator';

@Injectable()
export class ShowCoreFilesService {
constructor(private databaseService: DatabaseService) {}
constructor(private databaseServices: DatabaseService) {}

async show(
{ id: user_id }: User,
Expand Down
20 changes: 10 additions & 10 deletions __old_docs/pages/docs/plugins/database/sorting.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Steps } from "nextra/components";
import { Callout } from "nextra/components";
import { Steps } from 'nextra/components';
import { Callout } from 'nextra/components';

# Sorting

Expand All @@ -25,8 +25,8 @@ Inside `show/dto/show.args.ts` file create a enum to restrict the sorting column

```ts
enum ShowBlogCategoriesSortingColumnEnum {
created = "created",
updated = "updated"
created = 'created',
updated = 'updated',
}
```

Expand All @@ -42,10 +42,10 @@ Remember to export the enum. It will be used in the next steps.
NestJS require to register the enum in the GraphQL schema. You can read more in [NestJS documentation](https://docs.nestjs.com/graphql/unions-and-enums).

```ts
import { registerEnumType } from "@nestjs/graphql";
import { registerEnumType } from '@nestjs/graphql';

registerEnumType(ShowBlogCategoriesSortingColumnEnum, {
name: "ShowBlogCategoriesSortingColumnEnum"
name: 'ShowBlogCategoriesSortingColumnEnum',
});
```

Expand All @@ -54,9 +54,9 @@ registerEnumType(ShowBlogCategoriesSortingColumnEnum, {
Now you have to create a input type to use the enum in the args type.

```ts
import { InputType } from "@nestjs/graphql";
import { InputType } from '@nestjs/graphql';

import { SortDirectionEnum } from "@/types/database/sortDirection.type";
import { SortDirectionEnum } from '@/types/database/sortDirection.type';

@InputType()
class SortByArgs {
Expand All @@ -73,7 +73,7 @@ class SortByArgs {
Now you have to add the input type to the args type as arry and make it optional _(If you want)_.

```ts
import { ArgsType, Field } from "@nestjs/graphql";
import { ArgsType, Field } from '@nestjs/graphql';

@ArgsType()
export class ShowCoreMembersArgs {
Expand All @@ -91,7 +91,7 @@ Inside service in `show/show.service.ts` file you have to add the `sortBy` argum
```ts {9, 22}
@Injectable()
export class ShowBlogCategoriesService {
constructor(private databaseService: DatabaseService) {}
constructor(private databaseServices: DatabaseService) {}

async show({
cursor,
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineConfig } from 'drizzle-kit';

import { DATABASE_ENVS } from '@/database';
import { DATABASE_ENVS } from '@/database/config';

export default defineConfig({
dialect: 'postgresql',
Expand Down
Loading

0 comments on commit 0ec649a

Please sign in to comment.