-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add shared data service in api crud generator
- Loading branch information
Showing
7 changed files
with
188 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 10 additions & 11 deletions
21
...a-access/lib/__appFileName__-__modelFileName__-data-__actorFileName__.service.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...api-crud/files/data-access/lib/__appFileName__-__modelFileName__-data.service.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
Oops, something went wrong.