Skip to content

Commit

Permalink
feat: read entity
Browse files Browse the repository at this point in the history
  • Loading branch information
guiseek committed Oct 1, 2023
1 parent 913af79 commit 561c7bc
Show file tree
Hide file tree
Showing 25 changed files with 442 additions and 120 deletions.
241 changes: 211 additions & 30 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@swc-node/register": "~1.4.2",
"@swc/cli": "~0.1.62",
"@swc/core": "~1.3.51",
"@types/bcrypt": "^5.0.0",
"@types/jest": "^29.4.0",
"@types/node": "~18.7.1",
"@typescript-eslint/eslint-plugin": "^5.60.1",
Expand Down Expand Up @@ -60,11 +61,13 @@
"@angular/router": "~16.2.0",
"@nestjs/common": "^10.0.2",
"@nestjs/core": "^10.0.2",
"@nestjs/jwt": "^10.1.1",
"@nestjs/platform-express": "^10.0.2",
"@nestjs/swagger": "^7.1.11",
"@nx/devkit": "16.8.1",
"@swc/helpers": "~0.5.0",
"angular-in-memory-web-api": "^0.16.0",
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"pg": "^8.11.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import {

@Entity({name: '<%=fileName%>'})
export class <%=className%>Impl implements <%=className%> {
<% for(let prop of entity.props) { %>
<% if (prop.name === 'id') { %>
@PrimaryGeneratedColumn('uuid')
id: string

@Column()
name: string

<% } %>
<% if (prop.name === 'createdAt') { %>
@CreateDateColumn()
createdAt: Date

<% } %>
<% if (prop.name === 'updatedAt') { %>
@UpdateDateColumn()
updatedAt: Date
<% } else { %>
@Column()
<% } %>
<%= prop.name %>: <%= prop.type %>
<% } %>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ import {<%= className %>} from '<%= domain %>'
import {ApiProperty} from '@nestjs/swagger'

export class <%= className %>Dto implements <%= className %> {
<% for(let prop of entity.props) { %>
@ApiProperty()
id: string

@ApiProperty()
name: string

@ApiProperty()
createdAt: Date

@ApiProperty()
updatedAt: Date
<%= prop.name %>: <%= prop.type %>
<% } %>
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Create<%=className%>} from '<%=domain%>'
import {ApiProperty} from '@nestjs/swagger'
import {IsNotEmpty, IsString} from 'class-validator'
import {IsNotEmpty} from 'class-validator'

export class Create<%=className%>Dto implements Create<%=className%> {
<% for(let prop of entity.props) if (!['id','createdAt','updatedAt'].includes(prop.name)) { %>
@ApiProperty()
@IsString({message: 'o nome deve ser uma string'})
@IsNotEmpty({message: 'por favor, preencha o nome'})
name: string
@IsNotEmpty({message: 'por favor, preencha o <%= prop.name %>'})
<%= prop.name %>: <%= prop.type %>
<% } %>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import {

@Entity({name: '<%=fileName%>'})
export class <%=className%>Impl implements <%=className%> {
<% for(let prop of entity.props) { %>
<% if (prop.name === 'id') { %>
@PrimaryGeneratedColumn('uuid')
id: string

@Column()
name: string

<% } %>
<% if (prop.name === 'createdAt') { %>
@CreateDateColumn()
createdAt: Date

<% } %>
<% if (prop.name === 'updatedAt') { %>
@UpdateDateColumn()
updatedAt: Date
<% } else { %>
@Column()
<% } %>
<%= prop.name %>: <%= prop.type %>
<% } %>
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface Create<%= className %> {
name: string
<% for(let prop of entity.props) { %>
<% if (!['id','createdAt','updatedAt'].includes(prop.name)) { %>
<%= prop.name %>: <%= prop.type %>
<% } %>
<% } %>
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<h2 mat-dialog-title>Criar <%=fileName%></h2>

<form mat-dialog-content [formGroup]="form" (submit)="form.submit()">
<% for(let prop of entity.props) if (!['id','createdAt','updatedAt'].includes(prop.name)) { %>
<mat-form-field>
<mat-label> Nome </mat-label>
<input matInput formControlName="name" />
<mat-label> <%= prop.name %> </mat-label>
<input matInput formControlName="<%= prop.name %>" />
<mat-error> Obrigatório </mat-error>
</mat-form-field>
<% } %>

<p>{{message$ | async}}</p>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<h2 mat-dialog-title>Editar <%=fileName%></h2>

<form mat-dialog-content [formGroup]="form" (submit)="form.submit()">
<% for(let prop of entity.props) if (!['createdAt','updatedAt'].includes(prop.name)) { %>
<mat-form-field>
<mat-label> Id </mat-label>
<input matInput formControlName="id" readonly tabindex="-1" />
<mat-error> Obrigatório </mat-error>
</mat-form-field>
<mat-form-field>
<mat-label> Nome </mat-label>
<input matInput formControlName="name" autofocus />
<mat-label> <%= prop.name %> </mat-label>
<input matInput formControlName="<%= prop.name %>" />
<mat-error> Obrigatório </mat-error>
</mat-form-field>
<% } %>

<p>{{message$ | async}}</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,14 @@
</mat-checkbox>
</td>
</ng-container>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef>#</th>
<td mat-cell *matCellDef="let row">{{row.id}}</td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>Nome</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container>
<ng-container matColumnDef="createdAt">
<th mat-header-cell *matHeaderCellDef mat-sort-header disableClear>
Criado em
</th>
<td mat-cell *matCellDef="let row">{{row.createdAt | date}}</td>
</ng-container>
<ng-container matColumnDef="updatedAt">
<th mat-header-cell *matHeaderCellDef mat-sort-header disableClear>
Alterado em
</th>
<td mat-cell *matCellDef="let row">{{row.updatedAt | date}}</td>

<% for (let prop of entity.props) { %>
<ng-container matColumnDef="<%= prop.name %>">
<th mat-header-cell *matHeaderCellDef><%= prop.name %></th>
<td mat-cell *matCellDef="let row">{{row.<%= prop.name %>}}</td>
</ng-container>
<% } %>

<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef>Ações</th>
<td mat-cell *matCellDef="let row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,25 @@ export class <%=pluralClass%>Container
{
columns = new FormControl<Columns<<%=className%>>>([
'select',
'name',
'createdAt',
<% for (let prop of entity.props) { %>
'<%= prop.name %>',
<% } %>
'actions',
])

columnList: FilterByFieldOption[] = [
{text: 'nome', value: 'name', type: 'text'},
<% for(let prop of entity.props) { %>
{
text: '<%= prop.name %>',
value: '<%= prop.name %>',
type:
<% if (prop.type === 'string') { %>'text'<% } %>
<% if (prop.type === 'boolean') { %>'bool'<% } %>
<% if (prop.type === 'number') { %>'number'<% } %>
<% if (prop.type === 'Date') { %>'date'<% } %>
<% if (!['string','boolean','number', 'Date'].includes(prop.type)) { %>'text'<% } %>
},
<% } %>
]

readonly destroyRef = inject(DestroyRef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import {TypedFormGroup} from '@dev/shared-ui-forms'
export class Create<%=className%>Form extends TypedFormGroup<Create<%=className%>> {
constructor() {
super({
name: new FormControl('', {
<% for(let prop of entity.props) if (!['id','createdAt','updatedAt'].includes(prop.name)) { %>
<%= prop.name %>: new FormControl(<% if (prop.type === 'string') { %>''<% } %>
<% if (prop.type === 'boolean') { %>false<% } %>
<% if (prop.type === 'number') { %>0<% } %>
<% if (prop.type === 'Date') { %>''<% } %>
<% if (!['string','boolean','number', 'Date'].includes(prop.type)) { %>''<% } %>, {
nonNullable: true,
validators: [Validators.required],
}),
<% } %>
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import {TypedFormGroup} from '@dev/shared-ui-forms'
export class Update<%=className%>Form extends TypedFormGroup<Update<%=className%>> {
constructor(value: <%=className%>) {
super({
id: new FormControl('', {
<% for(let prop of entity.props) if (!['createdAt','updatedAt'].includes(prop.name)) { %>
<%= prop.name %>: new FormControl(<% if (prop.type === 'string') { %>''<% } %>
<% if (prop.type === 'boolean') { %>false<% } %>
<% if (prop.type === 'number') { %>0<% } %>
<% if (prop.type === 'Date') { %>''<% } %>
<% if (!['string','boolean','number', 'Date'].includes(prop.type)) { %>''<% } %>, {
nonNullable: true,
validators: [Validators.required],
}),
name: new FormControl(),
<% } %>
})
this.patchValue(value)
}
Expand Down
34 changes: 27 additions & 7 deletions tools/plugin/layer/src/generators/entity/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
generateFeatureFiles,
generateResourceFiles,
} from './generators'
import {readEntity} from './utilities'

function normalizeSchema(
tree: Tree,
Expand All @@ -29,7 +30,9 @@ function normalizeSchema(
feature: getProjectImportPath(project.feature),
}

return {project, scope}
const entity = readEntity(tree, options.entity)

return {project, entity, scope}
}

export async function entityGenerator(
Expand All @@ -38,13 +41,30 @@ export async function entityGenerator(
) {
const name = names(options.name)

const {project, scope} = normalizeSchema(tree, options)

await generateDomainFiles(tree, project.domain, name)
await generateDataSourceFiles(tree, project.dataSource, name, scope.domain)
await generateDataAccessFiles(tree, project.dataAccess, name, scope.domain)
const {project, entity, scope} = normalizeSchema(tree, options)
await generateDomainFiles(tree, project.domain, name, entity)
await generateDataSourceFiles(
tree,
project.dataSource,
name,
scope.domain,
entity
)
await generateDataAccessFiles(
tree,
project.dataAccess,
name,
scope.domain,
entity
)
await generateResourceFiles(tree, project.resource, name, scope.dataSource)
await generateFeatureFiles(tree, project.feature, name, scope.dataAccess)
await generateFeatureFiles(
tree,
project.feature,
name,
scope.dataAccess,
entity
)
}

export default entityGenerator
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {addExport, pathShift} from '../../../utilities'
import {NormalizedEntity} from '../schema'
import {Name} from '../../../interfaces'
import {join} from 'path'
import {
Expand All @@ -12,14 +13,16 @@ export async function generateDataAccessFiles(
tree: Tree,
project: ProjectConfiguration,
name: Name,
domain: string
domain: string,
entity: NormalizedEntity
) {
const sourceFolder = join(__dirname, '..', 'files', 'data-access')
const scope = pathShift(project.sourceRoot)
generateFiles(tree, sourceFolder, project.sourceRoot, {
...name,
scope,
domain,
entity,
})

/* Application */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {addExport} from '../../../utilities'
import {NormalizedEntity} from '../schema'
import {Name} from '../../../interfaces'
import {join} from 'path'
import {
Expand All @@ -12,11 +13,16 @@ export async function generateDataSourceFiles(
tree: Tree,
project: ProjectConfiguration,
name: Name,
domain: string
domain: string,
entity: NormalizedEntity
) {
const sourceFolder = join(__dirname, '..', 'files', 'data-source')

generateFiles(tree, sourceFolder, project.sourceRoot, {...name, domain})
generateFiles(tree, sourceFolder, project.sourceRoot, {
...name,
entity,
domain,
})

{
const sourceFile = join(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ import {
generateFiles,
ProjectConfiguration,
} from '@nx/devkit'
import {NormalizedEntity} from '../schema'

export async function generateDomainFiles(
tree: Tree,
project: ProjectConfiguration,
name: Name
name: Name,
entity: NormalizedEntity
) {
const sourceFolder = join(__dirname, '..', 'files', 'domain')
generateFiles(tree, sourceFolder, project.sourceRoot, name)

{
const sourceFile = join(project.sourceRoot, 'lib', 'entities', 'index.ts')
addExport(tree, sourceFile, `./${name.fileName}`)
}
generateFiles(tree, sourceFolder, project.sourceRoot, {...name, entity})

{
const sourceFile = join(project.sourceRoot, 'lib', 'dtos', 'index.ts')
Expand Down
Loading

0 comments on commit 561c7bc

Please sign in to comment.