Skip to content

Commit

Permalink
feat: add shared data service in api crud generator
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Mar 11, 2024
1 parent b32bcb9 commit 473faa4
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 33 deletions.
5 changes: 5 additions & 0 deletions libs/api/core/data-access/src/lib/dto/paging.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ export function PagingInput() {

return PagingInputClass
}

export interface PagingInputFields {
page?: number
limit?: number
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,52 @@ exports[`api-crud generator should run successfully 1`] = `
"isBinary": false,
"path": "libs/test/company/data-access/src/lib/test-company-data-admin.service.ts",
},
"test-company-data.service.ts": {
"content": [
"import { Injectable } from '@nestjs/common';",
"import { TestCoreService } from '@proj/test-core-data-access';",
"import { AdminCreateCompanyInput } from './dto/admin-create-company.input';",
"import { AdminFindManyCompanyInput } from './dto/admin-find-many-company.input';",
"import { AdminUpdateCompanyInput } from './dto/admin-update-company.input';",
"import { CompanyPaging } from './entity/company.entity';",
"import { getCompanyWhereAdminInput } from './helpers/get-company-where-admin.input';",
"@Injectable()",
"export class TestCompanyDataService {",
"constructor(private readonly core: TestCoreService) {}",
"async createCompany(input: AdminCreateCompanyInput) {",
"return this.core.data.company.create({ data: input });",
"}",
"async deleteCompany(companyId: string) {",
"const deleted = await this.core.data.company.delete({",
"where: { id: companyId },",
"});",
"return !!deleted;",
"}",
"async findManyCompany(",
"input: AdminFindManyCompanyInput",
"): Promise<CompanyPaging> {",
"return this.core.data.company",
".paginate({",
"orderBy: { createdAt: 'desc' },",
"where: getCompanyWhereAdminInput(input),",
"})",
".withPages({ limit: input.limit, page: input.page })",
".then(([data, meta]) => ({ data, meta }));",
"}",
"async findOneCompany(companyId: string) {",
"return this.core.data.company.findUnique({ where: { id: companyId } });",
"}",
"async updateCompany(companyId: string, input: AdminUpdateCompanyInput) {",
"return this.core.data.company.update({",
"where: { id: companyId },",
"data: input,",
"});",
"}",
"}",
],
"isBinary": false,
"path": "libs/test/company/data-access/src/lib/test-company-data.service.ts",
},
"test-company.data-access.module.ts": {
"content": [
"import { Module } from '@nestjs/common';",
Expand All @@ -269,10 +315,14 @@ exports[`api-crud generator should run successfully 1`] = `
"test-company.service.ts": {
"content": [
"import { Injectable } from '@nestjs/common';",
"import { TestCompanyDataService } from './test-company-data.service';",
"import { TestCompanyDataAdminService } from './test-company-data-admin.service';",
"@Injectable()",
"export class TestCompanyService {",
"constructor(readonly admin: TestCompanyDataAdminService) {}",
"constructor(",
"readonly data: TestCompanyDataService,",
"readonly admin: TestCompanyDataAdminService",
") {}",
"}",
],
"isBinary": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,49 @@ export class ApiTestDataUserService {
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 6`] = `
"import { Injectable } from '@nestjs/common';
import { ApiCoreService } from '@proj/api-core-data-access';
import { UserCreateTestInput } from './dto/user-create-test.input';
import { UserFindManyTestInput } from './dto/user-find-many-test.input';
import { UserUpdateTestInput } from './dto/user-update-test.input';
import { TestPaging } from './entity/test.entity';
import { getTestWhereUserInput } from './helpers/get-test-where-user.input';
@Injectable()
export class ApiTestDataService {
constructor(private readonly core: ApiCoreService) {}
async createTest(input: UserCreateTestInput) {
return this.core.data.test.create({ data: input });
}
async deleteTest(testId: string) {
const deleted = await this.core.data.test.delete({ where: { id: testId } });
return !!deleted;
}
async findManyTest(input: UserFindManyTestInput): Promise<TestPaging> {
return this.core.data.test
.paginate({
orderBy: { createdAt: 'desc' },
where: getTestWhereUserInput(input),
})
.withPages({ limit: input.limit, page: input.page })
.then(([data, meta]) => ({ data, meta }));
}
async findOneTest(testId: string) {
return this.core.data.test.findUnique({ where: { id: testId } });
}
async updateTest(testId: string, input: UserUpdateTestInput) {
return this.core.data.test.update({ where: { id: testId }, data: input });
}
}
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 7`] = `
"import { Module } from '@nestjs/common';
import { ApiCoreDataAccessModule } from '@proj/api-core-data-access';
import { ApiTestService } from './api-test.service';
Expand All @@ -115,22 +158,25 @@ export class ApiTestDataAccessModule {}
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 7`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 8`] = `
"import { Injectable } from '@nestjs/common';
import { ApiTestDataService } from './api-test-data.service';
import { ApiTestDataAdminService } from './api-test-data-admin.service';
import { ApiTestDataService } from './api-test-data.service';
import { ApiTestDataUserService } from './api-test-data-user.service';
@Injectable()
export class ApiTestService {
constructor(
readonly data: ApiTestDataService,
readonly admin: ApiTestDataAdminService,
readonly user: ApiTestDataUserService
) {}
}
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 8`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 9`] = `
"import { Field, InputType } from '@nestjs/graphql';
@InputType()
Expand All @@ -141,7 +187,7 @@ export class AdminCreateTestInput {
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 9`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 10`] = `
"import { Field, InputType } from '@nestjs/graphql';
import { PagingInput } from '@proj/api-core-data-access';
Expand All @@ -153,7 +199,7 @@ export class AdminFindManyTestInput extends PagingInput() {
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 10`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 11`] = `
"import { Field, InputType } from '@nestjs/graphql';
@InputType()
Expand All @@ -164,7 +210,7 @@ export class AdminUpdateTestInput {
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 11`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 12`] = `
"import { Field, InputType } from '@nestjs/graphql';
@InputType()
Expand All @@ -175,7 +221,7 @@ export class UserCreateTestInput {
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 12`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 13`] = `
"import { Field, InputType } from '@nestjs/graphql';
import { PagingInput } from '@proj/api-core-data-access';
Expand All @@ -187,7 +233,7 @@ export class UserFindManyTestInput extends PagingInput() {
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 13`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 14`] = `
"import { Field, InputType } from '@nestjs/graphql';
@InputType()
Expand All @@ -198,7 +244,7 @@ export class UserUpdateTestInput {
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 14`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 15`] = `
"import { Field, ObjectType } from '@nestjs/graphql';
import { PagingResponse } from '@proj/api-core-data-access';
Expand All @@ -220,7 +266,7 @@ export class TestPaging extends PagingResponse<Test>(Test) {}
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 15`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 16`] = `
"import { Prisma } from '@prisma/client';
import { AdminFindManyTestInput } from '../dto/admin-find-many-test.input';
Expand All @@ -241,7 +287,7 @@ export function getTestWhereAdminInput(
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 16`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 17`] = `
"import { Prisma } from '@prisma/client';
import { UserFindManyTestInput } from '../dto/user-find-many-test.input';
Expand All @@ -262,12 +308,12 @@ export function getTestWhereUserInput(
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 17`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 18`] = `
"export * from './lib/api-test.feature.module';
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 18`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 19`] = `
"import { Resolver } from '@nestjs/graphql';
import { ApiTestService } from '@proj/api-test-data-access';
import { ApiAuthGraphQLAdminGuard } from '@proj/api-auth-data-access';
Expand Down Expand Up @@ -317,7 +363,7 @@ export class ApiTestAdminResolver {
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 19`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 20`] = `
"import { Resolver } from '@nestjs/graphql';
import { ApiTestService } from '@proj/api-test-data-access';
import { ApiAuthGraphQLUserGuard } from '@proj/api-auth-data-access';
Expand Down Expand Up @@ -367,7 +413,7 @@ export class ApiTestUserResolver {
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 20`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 21`] = `
"import { Module } from '@nestjs/common';
import { ApiTestDataAccessModule } from '@proj/api-test-data-access';
import { ApiTestResolver } from './api-test.resolver';
Expand All @@ -382,7 +428,7 @@ export class ApiTestFeatureModule {}
"
`;
exports[`api-feature generator should generate the feature libraries with crud for admin and user 21`] = `
exports[`api-feature generator should generate the feature libraries with crud for admin and user 22`] = `
"import { Resolver } from '@nestjs/graphql';
import { ApiTestService } from '@proj/api-test-data-access';
import { Test } from '@proj/api-test-data-access';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('api-feature generator', () => {
"libs/api/test/data-access/src/index.ts",
"libs/api/test/data-access/src/lib/api-test-data-admin.service.ts",
"libs/api/test/data-access/src/lib/api-test-data-user.service.ts",
"libs/api/test/data-access/src/lib/api-test-data.service.ts",
"libs/api/test/data-access/src/lib/api-test.data-access.module.ts",
"libs/api/test/data-access/src/lib/api-test.service.ts",
"libs/api/test/data-access/src/lib/dto/admin-create-test.input.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import { Injectable } from '@nestjs/common'
import { <%= app.className %>CoreService } from '@<%= npmScope %>/<%= app.fileName %>-core-data-access'
import { <%= actor.className %>Create<%= model.className %>Input } from './dto/<%= actor.fileName %>-create-<%= model.fileName %>.input'
import { <%= actor.className %>FindMany<%= model.className %>Input } from './dto/<%= actor.fileName %>-find-many-<%= model.fileName %>.input'
import { <%= actor.className %>Update<%= model.className %>Input } from './dto/<%= actor.fileName %>-update-<%= model.fileName %>.input'
import { <%= model.className %>Paging } from './entity/<%= model.fileName %>.entity'
import { get<%= model.className %>Where<%= actor.className %>Input } from './helpers/get-<%= model.fileName %>-where-<%= actor.fileName %>.input'
import { <%= app.className %><%= model.className %>DataService } from './<%= app.fileName %>-<%= model.fileName %>-data.service'


@Injectable()
export class <%= app.className %><%= model.className %>Data<%= actor.className %>Service {
constructor(private readonly core: <%= app.className %>CoreService) {}
constructor(private readonly data: <%= app.className %><%= model.className %>DataService) {}

async create<%= model.className %>(input: <%= actor.className %>Create<%= model.className %>Input) {
return this.core.data.<%= model.propertyName %>.create({ data: input })
return this.data.create(input)
}

async delete<%= model.className %>(<%= model.propertyName %>Id: string) {
const deleted = await this.core.data.<%= model.propertyName %>.delete({ where: { id: <%= model.propertyName %>Id } })
return !!deleted
return this.data.delete(<%= model.propertyName %>Id)
}

async findMany<%= model.className %>(input: <%= actor.className %>FindMany<%= model.className %>Input): Promise<<%= model.className %>Paging> {
return this.core.data.<%= model.propertyName %>
.paginate({
return this.data.findMany({
orderBy: { createdAt: 'desc' },
where: get<%= model.className %>Where<%= actor.className %>Input(input),
limit: input.limit,
page: input.page
})
.withPages({ limit: input.limit, page: input.page })
.then(([data, meta]) => ({ data, meta }))
}

async findOne<%= model.className %>(<%= model.propertyName %>Id: string) {
return this.core.data.<%= model.propertyName %>.findUnique({ where: { id: <%= model.propertyName %>Id } })
return this.data.findOne(<%= model.propertyName %>Id)
}

async update<%= model.className %>(<%= model.propertyName %>Id: string, input: <%= actor.className %>Update<%= model.className %>Input) {
return this.core.data.<%= model.propertyName %>.update({ where: { id: <%= model.propertyName %>Id }, data: input })
return this.data.update(<%= model.propertyName %>Id, input)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable } from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { <%= app.className %>CoreService, type PagingInputFields } from '@<%= npmScope %>/<%= app.fileName %>-core-data-access'
import { <%= model.className %>Paging } from './entity/<%= model.fileName %>.entity'

@Injectable()
export class <%= app.className %><%= model.className %>DataService {
constructor(private readonly core: <%= app.className %>CoreService) {}

async create(input: Prisma.<%= model.className %>CreateInput) {
return this.core.data.<%= model.propertyName %>.create({ data: input })
}

async delete(<%= model.propertyName %>Id: string) {
const deleted = await this.core.data.<%= model.propertyName %>.delete({ where: { id: <%= model.propertyName %>Id } })
return !!deleted
}

async findMany({
limit = 10,
page = 1,
...input
}: Prisma.<%= model.className %>FindManyArgs & PagingInputFields): Promise<<%= model.className %>Paging> {
return this.core.data.<%= model.propertyName %>
.paginate(input)
.withPages({ limit, page })
.then(([data, meta]) => ({ data, meta }))
}

async findOne(<%= model.propertyName %>Id: string) {
return this.core.data.<%= model.propertyName %>.findUnique({ where: { id: <%= model.propertyName %>Id } })
}

async update(<%= model.propertyName %>Id: string, input: Prisma.<%= model.className %>UpdateInput) {
return this.core.data.<%= model.propertyName %>.update({ where: { id: <%= model.propertyName %>Id }, data: input })
}
}
Loading

0 comments on commit 473faa4

Please sign in to comment.