Skip to content

Commit

Permalink
feat: update generators to use abstract paging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Sep 4, 2023
1 parent 4bd526e commit 78e3c97
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`api-feature generator should generate the feature libraries 1`] = `
"export * from './lib/api-test-data-access.module';
export * from './lib/api-test.service';
export * from './lib/entity/test.entity';
export * from './lib/entity/test-paging.entity';
export * from './lib/dto/admin-create-test.input';
export * from './lib/dto/admin-find-many-test.input';
export * from './lib/dto/admin-update-test.input';
Expand All @@ -16,6 +17,7 @@ import { ApiCoreService } from '@proj/api/core/data-access';
import { AdminCreateTestInput } from './dto/admin-create-test.input';
import { AdminFindManyTestInput } from './dto/admin-find-many-test.input';
import { AdminUpdateTestInput } from './dto/admin-update-test.input';
import { TestPaging } from './entity/test-paging.entity';
import { parseAdminFindManyTest } from './helpers/parse-admin-find-many-test';
@Injectable()
Expand All @@ -31,24 +33,12 @@ export class ApiTestAdminService {
return !!deleted;
}
async findManyTest(input: AdminFindManyTestInput) {
const { where, orderBy, take, skip } = parseAdminFindManyTest(input);
const items = await this.core.data.test.findMany({
where,
orderBy,
take,
skip,
});
return items ?? [];
}
async findManyTestCount(input: AdminFindManyTestInput) {
const { where, orderBy, take, skip } = parseAdminFindManyTest(input);
const [count, total] = await Promise.all([
this.core.data.test.count({ where, orderBy, take, skip }),
this.core.data.test.count({ where, orderBy }),
]);
return { count, skip, take, total };
async findManyTest(input: AdminFindManyTestInput): Promise<TestPaging> {
const { where, orderBy, limit, page } = parseAdminFindManyTest(input);
const [data, meta] = await this.core.data.test
.paginate({ where, orderBy })
.withPages({ limit, page });
return { data, meta };
}
async findOneTest(testId: string) {
Expand Down Expand Up @@ -106,15 +96,12 @@ export class AdminCreateTestInput {
exports[`api-feature generator should generate the feature libraries 6`] = `
"import { Field, InputType, Int } from '@nestjs/graphql';
import { PagingInput } from '@proj/api/core/data-access';
@InputType()
export class AdminFindManyTestInput {
export class AdminFindManyTestInput extends PagingInput() {
@Field({ nullable: true })
search?: string;
@Field(() => Int, { nullable: true, defaultValue: 0 })
skip?: number;
@Field(() => Int, { nullable: true, defaultValue: 10 })
take?: number;
}
"
`;
Expand All @@ -135,9 +122,9 @@ exports[`api-feature generator should generate the feature libraries 8`] = `
import { AdminFindManyTestInput } from '../dto/admin-find-many-test.input';
export function parseAdminFindManyTest(input: AdminFindManyTestInput): {
limit: number;
orderBy: Prisma.TestOrderByWithRelationInput;
skip?: number;
take?: number;
page: number;
where: Prisma.TestWhereInput;
} {
const where: Prisma.TestWhereInput = {};
Expand All @@ -150,10 +137,10 @@ export function parseAdminFindManyTest(input: AdminFindManyTestInput): {
}
return {
where,
skip: input.skip ?? 0,
take: input.take ?? 10,
limit: input.limit ?? 10,
orderBy: { name: 'asc' },
page: input.page ?? 1,
where,
};
}
"
Expand Down Expand Up @@ -185,13 +172,13 @@ exports[`api-feature generator should generate the feature libraries 11`] = `
"import { Resolver } from '@nestjs/graphql';
import { ApiTestService } from '@proj/api/test/data-access';
import { ApiAuthGraphQLAdminGuard } from '@proj/api/auth/data-access';
import { Paging } from '@proj/api/core/data-access';
import { Mutation, Query, Args } from '@nestjs/graphql';
import { UseGuards } from '@nestjs/common';
import {
AdminCreateTestInput,
AdminFindManyTestInput,
Test,
TestPaging,
AdminUpdateTestInput,
} from '@proj/api/test/data-access';
Expand All @@ -210,16 +197,11 @@ export class ApiTestAdminResolver {
return this.service.admin.deleteTest(testId);
}
@Query(() => [Test], { nullable: true })
@Query(() => TestPaging, { nullable: true })
adminFindManyTest(@Args('input') input: AdminFindManyTestInput) {
return this.service.admin.findManyTest(input);
}
@Query(() => Paging, { nullable: true })
adminFindManyTestCount(@Args('input') input: AdminFindManyTestInput) {
return this.service.admin.findManyTestCount(input);
}
@Query(() => Test, { nullable: true })
adminFindOneTest(@Args('testId') testId: string) {
return this.service.admin.findOneTest(testId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApiCoreService } from '@<%= npmScope %>/api/core/data-access'
import { <%= admin.className %>Create<%= model.className %>Input } from './dto/<%= admin.fileName %>-create-<%= model.fileName %>.input'
import { <%= admin.className %>FindMany<%= model.className %>Input } from './dto/<%= admin.fileName %>-find-many-<%= model.fileName %>.input'
import { <%= admin.className %>Update<%= model.className %>Input } from './dto/<%= admin.fileName %>-update-<%= model.fileName %>.input'
import { <%= model.className %>Paging } from './entity/<%= model.fileName %>-paging.entity'
import { parse<%= admin.className %>FindMany<%= model.className %> } from './helpers/parse-<%= admin.fileName %>-find-many-<%= model.fileName %>'

@Injectable()
Expand All @@ -18,19 +19,10 @@ export class Api<%= model.className %><%= admin.className %>Service {
return !!deleted
}

async findMany<%= model.className %>(input: <%= admin.className %>FindMany<%= model.className %>Input) {
const { where, orderBy, take, skip } = parse<%= admin.className %>FindMany<%= model.className %>(input)
const items = await this.core.data.<%= model.propertyName %>.findMany({ where, orderBy, take, skip })
return items ?? []
}

async findMany<%= model.className %>Count(input: <%= admin.className %>FindMany<%= model.className %>Input) {
const { where, orderBy, take, skip } = parse<%= admin.className %>FindMany<%= model.className %>(input)
const [count, total] = await Promise.all([
this.core.data.<%= model.propertyName %>.count({ where, orderBy, take, skip }),
this.core.data.<%= model.propertyName %>.count({ where, orderBy }),
])
return { count, skip, take, total }
async findMany<%= model.className %>(input: <%= admin.className %>FindMany<%= model.className %>Input): Promise<<%= model.className %>Paging> {
const { where, orderBy, limit, page } = parse<%= admin.className %>FindMany<%= model.className %>(input)
const [data, meta] = await this.core.data.<%= model.propertyName %>.paginate({ where, orderBy }).withPages({ limit, page })
return { data, meta }
}

async findOne<%= model.className %>(<%= model.propertyName %>Id: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Field, InputType, Int } from '@nestjs/graphql'
import { PagingInput } from '@<%= npmScope %>/api/core/data-access'

@InputType()
export class <%= admin.className %>FindMany<%= model.className %>Input {
export class <%= admin.className %>FindMany<%= model.className %>Input extends PagingInput() {
@Field({ nullable: true })
search?: string
@Field(() => Int, { nullable: true, defaultValue: 0 })
skip?: number
@Field(() => Int, { nullable: true, defaultValue: 10 })
take?: number
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ObjectType } from '@nestjs/graphql'
import { PagingResponse } from '@<%= npmScope %>/api/core/data-access'
import { <%= model.className %> } from './<%= model.fileName %>.entity'

@ObjectType()
export class <%= model.className %>Paging extends PagingResponse<<%= model.className %>>(<%= model.className %>) {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Prisma } from '@prisma/client'
import { <%= admin.className %>FindMany<%= model.className %>Input } from '../dto/<%= admin.fileName %>-find-many-<%= model.fileName %>.input'

export function parse<%= admin.className %>FindMany<%= model.className %>(input: <%= admin.className %>FindMany<%= model.className %>Input): {
limit: number
orderBy: Prisma.<%= model.className %>OrderByWithRelationInput
skip?: number
take?: number
page: number
where: Prisma.<%= model.className %>WhereInput
} {
const where: Prisma.<%= model.className %>WhereInput = {}
Expand All @@ -17,9 +17,9 @@ export function parse<%= admin.className %>FindMany<%= model.className %>(input:
}

return {
where,
skip: input.skip ?? 0,
take: input.take ?? 10,
limit: input.limit ?? 10,
orderBy: { <%= label.propertyName %>: 'asc' },
page: input.page ?? 1,
where,
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Resolver } from '@nestjs/graphql'
import { <%= app.className %><%= model.className %>Service } from '@<%= npmScope %>/<%= app.fileName %>/<%= model.fileName %>/data-access'
import { <%= app.className %>AuthGraphQLAdminGuard } from '@<%= npmScope %>/<%= app.fileName %>/auth/data-access'
import { Paging } from '@<%= npmScope %>/<%= app.fileName %>/core/data-access'
import { Mutation, Query, Args } from '@nestjs/graphql'
import { UseGuards } from '@nestjs/common'
import {
<%= admin.className %>Create<%= model.className %>Input,
<%= admin.className %>FindMany<%= model.className %>Input,
<%= model.className %>,
<%= model.className %>Paging,
<%= admin.className %>Update<%= model.className %>Input,
} from '@<%= npmScope %>/<%= app.fileName %>/<%= model.fileName %>/data-access'

Expand All @@ -26,16 +26,11 @@ export class <%= app.className %><%= model.className %><%= admin.className %>Res
return this.service.<%= admin.propertyName %>.delete<%= model.className %>(<%= model.propertyName %>Id)
}

@Query(() => [<%= model.className %>], { nullable: true })
@Query(() => <%= model.className %>Paging, { nullable: true })
<%= admin.propertyName %>FindMany<%= model.className %>(@Args('input') input: <%= admin.className %>FindMany<%= model.className %>Input) {
return this.service.<%= admin.propertyName %>.findMany<%= model.className %>(input)
}

@Query(() => Paging, { nullable: true })
<%= admin.propertyName %>FindMany<%= model.className %>Count(@Args('input') input: <%= admin.className %>FindMany<%= model.className %>Input) {
return this.service.<%= admin.propertyName %>.findMany<%= model.className %>Count(input)
}

@Query(() => <%= model.className %>, { nullable: true })
<%= admin.propertyName %>FindOne<%= model.className %>(@Args('<%= model.propertyName %>Id') <%= model.propertyName %>Id: string) {
return this.service.<%= admin.propertyName %>.findOne<%= model.className %>(<%= model.propertyName %>Id)
Expand Down
1 change: 1 addition & 0 deletions libs/tools/src/lib/api/generate-api-lib-data-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function generateApiLibDataAccess(tree: Tree, options: NormalizedAp
const dataAccessExports: string[] = [
`./lib/${options.app}-${options.name}.service`,
`./lib/entity/${substitutions.model.fileName}.entity`,
`./lib/entity/${substitutions.model.fileName}-paging.entity`,
]

if (!options.skipAdminCrud) {
Expand Down
12 changes: 7 additions & 5 deletions libs/tools/src/lib/api/generate-sdk-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ function sdkTemplate(name: string, label: string) {
}
query adminFindMany${className}($input: AdminFindMany${className}Input!) {
count: adminFindMany${className}Count(input: $input) {
...PagingDetails
}
items: adminFindMany${className}(input: $input) {
...${className}Details
paging: adminFindMany${className}(input: $input) {
data {
...${className}Details
}
meta {
...PagingMetaDetails
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { useState } from 'react'

export function useAdminFindMany<%= modelClassName %>(props?: Partial<AdminFindMany<%= modelClassName %>Input>) {
const sdk = useWebSdk()
const [take, setTake] = useState(props?.take ?? 10)
const [skip, setSkip] = useState(props?.skip ?? 0)
const [limit, setLimit] = useState(10)
const [page, setPage] = useState(1)
const [search, setSearch] = useState<string>(props?.search ?? '')

const input: AdminFindMany<%= modelClassName %>Input = { skip, take, search }
const input: AdminFindMany<%= modelClassName %>Input = { page, limit, search }
const query = useQuery(['admin', 'find-many-<%= modelPropertyName %>', input], () => sdk.adminFindMany<%= modelClassName %>({ input }).then((res) => res.data))
const total = query.data?.count?.total ?? 0
const total = query.data?.paging?.meta?.totalCount ?? 0

return {
create<%= modelClassName %>: (input: AdminCreate<%= modelClassName %>Input) =>
Expand All @@ -41,10 +41,10 @@ export function useAdminFindMany<%= modelClassName %>(props?: Partial<AdminFindM
query,
setSearch,
pagination: useUiPagination({
skip,
setSkip,
take,
setTake,
page,
setPage,
limit,
setLimit,
total,
}),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ export function WebAdmin<%= modelClassName %>ListFeature() {

{query.isLoading ? (
<UiLoader />
) : query?.data?.items?.length ? (
) : query?.data?.paging?.data?.length ? (
<AdminUi<%= modelClassName %>Table
delete<%= modelClassName %>={(<%= modelPropertyName %>) => {
if (!window.confirm('Are you sure?')) return
return delete<%= modelClassName %>(<%= modelPropertyName %>.id)
}}
<%= modelPropertyName %>s={query?.data?.items as <%= modelClassName %>[]}
<%= modelPropertyName %>s={query?.data?.paging?.data ?? []}
/>
) : (
<UiAlert message="<%= modelClassName %> not found" />
<UiAlert message="No results" />
)}

<UiPagination pagination={pagination} />
Expand Down

0 comments on commit 78e3c97

Please sign in to comment.