Skip to content

Commit

Permalink
Consider entitySuffix and dtoSuffix refs jhipster#209
Browse files Browse the repository at this point in the history
- Use asDto and asEntity methods provided by JHipster (this ensures that the entitySuffix and dtoSuffix configOptions are used)
- Fix error where Instant fieldType did not work with sqlite
  • Loading branch information
glutengo committed Sep 5, 2021
1 parent c407a66 commit 76d305d
Show file tree
Hide file tree
Showing 24 changed files with 164 additions and 150 deletions.
Empty file modified cli/nhipster.js
100644 → 100755
Empty file.
11 changes: 10 additions & 1 deletion generators/entity-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ const dbTypes = {
'byte[]': 'blob'
};

function sanitizeDbType(fieldType, dbType) {
if (dbType === 'sqlite') {
if (fieldType === 'timestamp') {
return 'datetime';
}
}
return fieldType;
}

module.exports = class extends EntityServerGenerator {
constructor(args, opts) {
super(args, { fromBlueprint: true, ...opts }); // fromBlueprint variable is important
Expand Down Expand Up @@ -65,6 +74,6 @@ module.exports = class extends EntityServerGenerator {
}

addDbType(fieldType) {
return dbTypes[fieldType];
return sanitizeDbType(dbTypes[fieldType], this.devDatabaseType);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppModule } from '../src/app.module';
import { INestApplication } from '@nestjs/common';
import { AuthGuard } from '../src/security/guards/auth.guard';
import { RolesGuard } from '../src/security/guards/roles.guard';
import { <%= entityClass %>DTO } from '../src/service/dto/<%= entityFileName %>.dto';
import { <%= asDto(entityClass) %> } from '../src/service/dto/<%= entityFileName %>.dto';
import { <%= entityClass %>Service } from '../src/service/<%= entityFileName %>.service';

describe('<%= entityClass %> Controller', () => {
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('<%= entityClass %> Controller', () => {

it('/GET all <%= entityApiUrl %> ', async () => {

const getEntities: <%= entityClass %>DTO[] = (await request(app.getHttpServer())
const getEntities: <%= asDto(entityClass) %>[] = (await request(app.getHttpServer())
.get('/api/<%= entityApiUrl %>')
.expect(200)).body;

Expand All @@ -54,7 +54,7 @@ describe('<%= entityClass %> Controller', () => {
it('/GET <%= entityApiUrl %> by id', async () => {


const getEntity: <%= entityClass %>DTO = (await request(app.getHttpServer())
const getEntity: <%= asDto(entityClass) %> = (await request(app.getHttpServer())
.get('/api/<%= entityApiUrl %>/' + entityMock.id)
.expect(200)).body;

Expand All @@ -65,7 +65,7 @@ describe('<%= entityClass %> Controller', () => {

it('/POST create <%= entityApiUrl %>', async () => {

const createdEntity: <%= entityClass %>DTO = (await request(app.getHttpServer())
const createdEntity: <%= asDto(entityClass) %> = (await request(app.getHttpServer())
.post('/api/<%= entityApiUrl %>')
.send(entityMock)
.expect(201)).body;
Expand All @@ -78,7 +78,7 @@ describe('<%= entityClass %> Controller', () => {
it('/PUT update <%= entityApiUrl %>', async () => {


const updatedEntity: <%= entityClass %>DTO = (await request(app.getHttpServer())
const updatedEntity: <%= asDto(entityClass) %> = (await request(app.getHttpServer())
.put('/api/<%= entityApiUrl %>')
.send(entityMock)
.expect(201)).body;
Expand All @@ -92,7 +92,7 @@ describe('<%= entityClass %> Controller', () => {
it('/PUT update <%= entityApiUrl %> from id', async () => {


const updatedEntity: <%= entityClass %>DTO = (await request(app.getHttpServer())
const updatedEntity: <%= asDto(entityClass) %> = (await request(app.getHttpServer())
.put('/api/<%= entityApiUrl %>/' + entityMock.id)
.send(entityMock)
.expect(201)).body;
Expand All @@ -107,7 +107,7 @@ describe('<%= entityClass %> Controller', () => {
it('/DELETE <%= entityApiUrl %>', async () => {


const deletedEntity: <%= entityClass %>DTO = (await request(app.getHttpServer())
const deletedEntity: <%= asDto(entityClass) %> = (await request(app.getHttpServer())
.delete('/api/<%= entityApiUrl %>/' + entityMock.id)
.expect(204)).body;

Expand Down
28 changes: 15 additions & 13 deletions generators/entity-server/templates/server/src/domain/entity.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ for (idx in fields) {
}
for (idx in relationships) {
const relationship = relationships[idx];
if (relationship.otherEntityAngularName === 'User'){
const otherEntityClass = asEntity(relationship.otherEntity.entityClass);
if (relationship.otherEntity.entityClass === 'User'){
isUserRelationship = true;
}
else if (!uniqueEntities[relationship.otherEntityAngularName] && relationship.otherEntityAngularName != asEntity(entityClass)) {
uniqueEntities[relationship.otherEntityAngularName] = relationship.otherEntityFileName;
else if (!uniqueEntities[otherEntityClass] && otherEntityClass != asEntity(entityClass)) {
uniqueEntities[otherEntityClass] = relationship.otherEntityFileName;
}
}
_%>
Expand All @@ -36,7 +37,7 @@ import { <%= enumClass %> } from './enumeration/<%= uniqueEnums[enumClass] %>';
<%_ }); _%>

<%_ if (isUserRelationship === true) { _%>
import { User } from './user.entity';
import { <%= asEntity('User') %> } from './user.entity';
<%_ } _%>

<%_ if (typeof javadoc == 'undefined') { _%>
Expand Down Expand Up @@ -109,34 +110,35 @@ export class <%= asEntity(entityClass) %> extends BaseEntity {
const relationshipRequired = relationships[idx].relationshipRequired;
const otherEntityNameCapitalized = relationships[idx].otherEntityNameCapitalized;
const ownerSide = relationships[idx].ownerSide; // before was ownerSide === true in and userRel many-to-one
const otherEntityClass = asEntity(relationships[idx].otherEntity.entityClass);
let userRelationship = false;
if (relationship.otherEntityAngularName === 'User'){
if (otherEntityClass === 'User'){
userRelationship = true;
}
if (relationshipType === 'one-to-many') {_%>
@OneToMany(type => <%= relationship.otherEntityAngularName %> <%_ if (userRelationship === false) { _%>, other => other.<%= otherEntityRelationshipName %><%_ } _%>)
<%= relationshipFieldNamePlural %>: <%= relationship.otherEntityAngularName %>[];
@OneToMany(type => <%= otherEntityClass %> <%_ if (userRelationship === false) { _%>, other => other.<%= otherEntityRelationshipName %><%_ } _%>)
<%= relationshipFieldNamePlural %>: <%= otherEntityClass %>[];
<%_ } else if (relationshipType === 'many-to-one') { _%>
@ManyToOne(type => <%= relationship.otherEntityAngularName %> <%_ if (!ownerSide && userRelationship === false ) { _%>, other => other.<%= otherEntityRelationshipNamePlural %> <%_ } _%>)
<%= relationshipFieldName %>: <%= relationship.otherEntityAngularName %>;
@ManyToOne(type => <%= otherEntityClass %> <%_ if (!ownerSide && userRelationship === false ) { _%>, other => other.<%= otherEntityRelationshipNamePlural %> <%_ } _%>)
<%= relationshipFieldName %>: <%= otherEntityClass %>;
<%_ } else if (relationshipType === 'many-to-many') { _%>
@ManyToMany(type => <%= relationship.otherEntityAngularName %> )
@ManyToMany(type => <%= otherEntityClass %> )
<%_ if (ownerSide === true) { _%>
@JoinTable({
name: '<%= joinTableName %>',
joinColumn: { name: '<%= getColumnName(name) %>_id', referencedColumnName: "id" },
inverseJoinColumn: { name: '<%= getColumnName(relationship.relationshipName) %>_id', referencedColumnName: "id" }
})
<%_ } _%>
<%= relationshipFieldNamePlural %>: <%= relationship.otherEntityAngularName %>[];
<%= relationshipFieldNamePlural %>: <%= otherEntityClass %>[];
<%_ } else { _%>
@OneToOne(type => <%= relationship.otherEntityAngularName %>)
@OneToOne(type => <%= otherEntityClass %>)
<%_ if (ownerSide === true) { _%>@JoinColumn()<%_ } _%>
<%= relationshipFieldName %>: <%= relationship.otherEntityAngularName %>;
<%= relationshipFieldName %>: <%= otherEntityClass %>;
<%_ }
} _%>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EntityRepository, Repository } from 'typeorm';
import { <%= entityClass %> } from '../domain/<%= entityFileName %>.entity';
import { <%= asEntity(entityClass) %> } from '../domain/<%= entityFileName %>.entity';

@EntityRepository(<%= entityClass %>)
export class <%= entityClass %>Repository extends Repository<<%= entityClass %>> {}
@EntityRepository(<%= asEntity(entityClass) %>)
export class <%= entityClass %>Repository extends Repository<<%= asEntity(entityClass) %>> {}
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,31 @@ for (idx in fields) {
}
for (idx in relationships) {
const relationship = relationships[idx];
if (relationship.otherEntityAngularName === 'User'){
const otherDto = asDto(relationship.otherEntity.entityClass);
if (relationship.otherEntity.entityClass === 'User'){
isUserRelationship = true;
}
else if (!uniqueEntities[relationship.otherEntityAngularName] && relationship.otherEntityAngularName != asEntity(entityClass)) {
uniqueEntities[relationship.otherEntityAngularName] = relationship.otherEntityFileName;
else if (!uniqueEntities[otherDto] && otherDto != asDto(entityClass)) {
uniqueEntities[otherDto] = relationship.otherEntityFileName;
}
}
_%>

<%_ Object.keys(uniqueEntities).forEach(function(entityClass) { _%>
import { <%= entityClass %>DTO } from './<%= uniqueEntities[entityClass] %>.dto';
<%_ Object.keys(uniqueEntities).forEach(function(entityDto) { _%>
import { <%= entityDto %> } from './<%= uniqueEntities[entityDto] %>.dto';
<%_ }); _%>
<%_ Object.keys(uniqueEnums).forEach(function(enumClass) { _%>
import { <%= enumClass %> } from '../../domain/enumeration/<%= uniqueEnums[enumClass] %>';
<%_ }); _%>

<%_ if (isUserRelationship === true) { _%>
import { UserDTO } from './user.dto';
import { <%= asDto('User')%> } from './user.dto';
<%_ } _%>

/**
* A <%= asEntity(entityClass) %> DTO object.
* A <%= asDto(entityClass) %> object.
*/
export class <%= asEntity(entityClass) %>DTO extends BaseDTO {
export class <%= asDto(entityClass) %> extends BaseDTO {

<%_ for (idx in fieldsWithoutId) {
const field = fieldsWithoutId[idx];
Expand Down Expand Up @@ -98,25 +99,26 @@ export class <%= asEntity(entityClass) %>DTO extends BaseDTO {
const relationshipValidate = relationship.relationshipValidate;
const relationshipRequired = relationship.relationshipRequired;
const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized;
const otherEntityDto = asDto(relationship.otherEntity.entityClass);
if (typeof relationships.javadoc != 'undefined') { _%>
if (typeof relationships.javadoc != 'undefined') { _%>
<%- formatAsFieldJavadoc(relationships.javadoc) %>
<%_ } _%>
<%_ if (relationshipType === 'one-to-many') { _%>
@ApiModelProperty({ type: <%= relationship.otherEntityAngularName %>DTO, isArray: true, <%_ if (typeof relationships.javadoc != 'undefined') { _%> description: '<%- formatAsApiDescription(relationships.javadoc) %>' <%_ } else { _%> description: '<%= relationshipFieldNamePlural %> relationship' <%_ } _%> })
<%= relationshipFieldNamePlural %>: <%= relationship.otherEntityAngularName %>DTO[];
@ApiModelProperty({ type: <%= otherEntityDto %>, isArray: true, <%_ if (typeof relationships.javadoc != 'undefined') { _%> description: '<%- formatAsApiDescription(relationships.javadoc) %>' <%_ } else { _%> description: '<%= relationshipFieldNamePlural %> relationship' <%_ } _%> })
<%= relationshipFieldNamePlural %>: <%= otherEntityDto %>[];
<%_ } else if (relationshipType === 'many-to-one') { _%>
@ApiModelProperty({ type: <%= relationship.otherEntityAngularName %>DTO, <%_ if (typeof relationships.javadoc != 'undefined') { _%> description: '<%- formatAsApiDescription(relationships.javadoc) %>' <%_ } else { _%> description: '<%= relationshipFieldName %> relationship' <%_ } _%> })
<%= relationshipFieldName %>: <%= relationship.otherEntityAngularName %>DTO;
@ApiModelProperty({ type: <%= otherEntityDto %>, <%_ if (typeof relationships.javadoc != 'undefined') { _%> description: '<%- formatAsApiDescription(relationships.javadoc) %>' <%_ } else { _%> description: '<%= relationshipFieldName %> relationship' <%_ } _%> })
<%= relationshipFieldName %>: <%= otherEntityDto %>;
<%_ } else if (relationshipType === 'many-to-many') { _%>
@ApiModelProperty({ type: <%= relationship.otherEntityAngularName %>DTO, isArray: true, <%_ if (typeof relationships.javadoc != 'undefined') { _%> description: '<%- formatAsApiDescription(relationships.javadoc) %>' <%_ } else { _%> description: '<%= relationshipFieldNamePlural %> relationship' <%_ } _%> })
<%= relationshipFieldNamePlural %>: <%= relationship.otherEntityAngularName %>DTO[];
@ApiModelProperty({ type: <%= otherEntityDto %>, isArray: true, <%_ if (typeof relationships.javadoc != 'undefined') { _%> description: '<%- formatAsApiDescription(relationships.javadoc) %>' <%_ } else { _%> description: '<%= relationshipFieldNamePlural %> relationship' <%_ } _%> })
<%= relationshipFieldNamePlural %>: <%= otherEntityDto %>[];
<%_ } else { _%>
@ApiModelProperty({ type: <%= relationship.otherEntityAngularName %>DTO, <%_ if (typeof relationships.javadoc != 'undefined') { _%> description: '<%- formatAsApiDescription(relationships.javadoc) %>' <%_ } else { _%> description: '<%= relationshipFieldName %> relationship' <%_ } _%> })
<%= relationshipFieldName %>: <%= relationship.otherEntityAngularName %>DTO;
@ApiModelProperty({ type: <%= otherEntityDto %>, <%_ if (typeof relationships.javadoc != 'undefined') { _%> description: '<%- formatAsApiDescription(relationships.javadoc) %>' <%_ } else { _%> description: '<%= relationshipFieldName %> relationship' <%_ } _%> })
<%= relationshipFieldName %>: <%= otherEntityDto %>;
<%_ }
} _%>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, HttpException, HttpStatus, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { FindManyOptions, FindOneOptions } from 'typeorm';
import { <%= entityClass %>DTO } from '../service/dto/<%= entityFileName %>.dto';
import { <%= asDto(entityClass) %> } from '../service/dto/<%= entityFileName %>.dto';
import { <%= entityClass %>Mapper } from '../service/mapper/<%= entityFileName %>.mapper';
import { <%= entityClass %>Repository } from '../repository/<%= entityFileName %>.repository';

Expand Down Expand Up @@ -30,29 +30,29 @@ export class <%= entityClass %>Service {

constructor(@InjectRepository(<%= entityClass %>Repository) private <%= asEntity(entityInstance) %>Repository: <%= entityClass %>Repository) {}

async findById(id: <%= pkType %>): Promise<<%= entityClass %>DTO | undefined> {
async findById(id: <%= pkType %>): Promise<<%= asDto(entityClass) %> | undefined> {
const options = { relations: relationshipNames };
const result = await this.<%= asEntity(entityInstance) %>Repository.findOne(id, options);
return <%= entityClass %>Mapper.fromEntityToDTO(result);
}

async findByFields(options: FindOneOptions<<%= entityClass %>DTO>): Promise<<%= entityClass %>DTO | undefined> {
async findByFields(options: FindOneOptions<<%= asDto(entityClass) %>>): Promise<<%= asDto(entityClass) %> | undefined> {
const result = await this.<%= asEntity(entityInstance) %>Repository.findOne(options);
return <%= entityClass %>Mapper.fromEntityToDTO(result);
}

async findAndCount(options: FindManyOptions<<%= entityClass %>DTO>): Promise<[<%= entityClass %>DTO[], number]> {
async findAndCount(options: FindManyOptions<<%= asDto(entityClass) %>>): Promise<[<%= asDto(entityClass) %>[], number]> {
options.relations = relationshipNames;
const resultList = await this.<%= asEntity(entityInstance) %>Repository.findAndCount(options);
const <%= asEntity(entityInstance) %>DTO: <%= entityClass %>DTO[] = [];
const <%= asEntity(entityInstance) %>DTO: <%= asDto(entityClass) %>[] = [];
if (resultList && resultList[0]) {
resultList[0].forEach(<%= asEntity(entityInstance) %> => <%= asEntity(entityInstance) %>DTO.push(<%= entityClass %>Mapper.fromEntityToDTO(<%= asEntity(entityInstance) %>)));
resultList[0] = <%= asEntity(entityInstance) %>DTO;
}
return resultList;
}

async save(<%= asEntity(entityInstance) %>DTO: <%= entityClass %>DTO, creator?: string): Promise<<%= entityClass %>DTO | undefined> {
async save(<%= asEntity(entityInstance) %>DTO: <%= asDto(entityClass) %>, creator?: string): Promise<<%= asDto(entityClass) %> | undefined> {
const entity = <%= entityClass %>Mapper.fromDTOtoEntity(<%= asEntity(entityInstance) %>DTO);
if (creator) {
if (!entity.createdBy) {
Expand All @@ -64,7 +64,7 @@ export class <%= entityClass %>Service {
return <%= entityClass %>Mapper.fromEntityToDTO(result);
}

async update(<%= asEntity(entityInstance) %>DTO: <%= entityClass %>DTO, updater?: string): Promise<<%= entityClass %>DTO | undefined> {
async update(<%= asEntity(entityInstance) %>DTO: <%= asDto(entityClass) %>, updater?: string): Promise<<%= asDto(entityClass) %> | undefined> {
const entity = <%= entityClass %>Mapper.fromDTOtoEntity(<%= asEntity(entityInstance) %>DTO);
if (updater) {
entity.lastModifiedBy = updater;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { <%= asEntity(entityClass) %> } from '../../domain/<%= entityFileName %>.entity';
import { <%= asEntity(entityClass) %>DTO } from '../dto/<%= entityFileName %>.dto';
import { <%= asDto(entityClass) %> } from '../dto/<%= entityFileName %>.dto';


/**
* A <%= asEntity(entityClass) %> mapper object.
* A <%= entityClass %> mapper object.
*/
export class <%= asEntity(entityClass) %>Mapper {
export class <%= entityClass %>Mapper {

static fromDTOtoEntity (entityDTO: <%= asEntity(entityClass) %>DTO): <%= asEntity(entityClass) %> {
static fromDTOtoEntity (entityDTO: <%= asDto(entityClass) %>): <%= asEntity(entityClass) %> {
if (!entityDTO) {
return;
}
Expand All @@ -20,11 +20,11 @@ export class <%= asEntity(entityClass) %>Mapper {

}

static fromEntityToDTO (entity: <%= asEntity(entityClass) %>): <%= asEntity(entityClass) %>DTO {
static fromEntityToDTO (entity: <%= asEntity(entityClass) %>): <%= asDto(entityClass) %> {
if (!entity) {
return;
}
let entityDTO = new <%= asEntity(entityClass) %>DTO();
let entityDTO = new <%= asDto(entityClass) %>();

const fields = Object.getOwnPropertyNames(entity);

Expand Down
Loading

0 comments on commit 76d305d

Please sign in to comment.