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 3d1a9c0
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 112 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);
}
};
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 @@ -25,7 +25,7 @@ for (idx in relationships) {
if (relationship.otherEntityAngularName === 'User'){
isUserRelationship = true;
}
else if (!uniqueEntities[relationship.otherEntityAngularName] && relationship.otherEntityAngularName != asEntity(entityClass)) {
else if (!uniqueEntities[relationship.otherEntityAngularName] && relationship.otherEntityAngularName != asDto(entityClass)) {
uniqueEntities[relationship.otherEntityAngularName] = relationship.otherEntityFileName;
}
}
Expand All @@ -39,13 +39,13 @@ import { <%= enumClass %> } from '../../domain/enumeration/<%= uniqueEnums[enumC
<%_ }); _%>

<%_ 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
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
12 changes: 6 additions & 6 deletions generators/server/templates/server/e2e/account.e2e-spec.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppModule } from '../src/app.module';
import { ExecutionContext, INestApplication } from '@nestjs/common';
import { AuthGuard } from '../src/security/guards/auth.guard';
import { RolesGuard } from '../src/security/guards/roles.guard';
import { UserDTO } from '../src/service/dto/user.dto';
import { <%= asDto('User')%> } from '../src/service/dto/user.dto';
import { UserService } from '../src/service/user.service';
import { PasswordChangeDTO } from '../src/service/dto/password-change.dto';
import { AuthService } from '../src/service/auth.service';
Expand All @@ -14,7 +14,7 @@ describe('Account', () => {
let service: UserService;
let authService: AuthService;

const testUserDTO: UserDTO = {
const testUserDTO: <%= asDto('User')%> = {
login: 'userTestLogin',
email: '[email protected]',
password: 'testPassword',
Expand All @@ -33,7 +33,7 @@ describe('Account', () => {
newPassword: 'newPassword',
};

let userAuthenticated: UserDTO;
let userAuthenticated: <%= asDto('User')%>;

const authGuardMock = { canActivate: (context: ExecutionContext): any => {
const req = context.switchToHttp().getRequest();
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('Account', () => {
});

it('/POST register new user', async () => {
const createdUser: UserDTO = (
const createdUser: <%= asDto('User')%> = (
await request(app.getHttpServer())
.post('/api/register')
.send(testUserDTO)
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('Account', () => {

it('/POST account update settings', async () => {

const savedTestUser: UserDTO = {
const savedTestUser: <%= asDto('User')%> = {
firstName: 'updateFirstName',
lastName: 'updateLastName',
...testUserAuthenticated,
Expand All @@ -119,7 +119,7 @@ describe('Account', () => {
.send(savedTestUser)
.expect(201);

const updatedUserSettings: UserDTO = await service.findByFields({ where: { login: testUserAuthenticated.login } });
const updatedUserSettings: <%= asDto('User')%> = await service.findByFields({ where: { login: testUserAuthenticated.login } });
expect(updatedUserSettings.firstName).toEqual(savedTestUser.firstName);
expect(updatedUserSettings.lastName).toEqual(savedTestUser.lastName);

Expand Down
14 changes: 7 additions & 7 deletions generators/server/templates/server/e2e/user.e2e-spec.ts.ejs
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 { UserDTO } from '../src/service/dto/user.dto';
import { <%= asDto('User')%> } from '../src/service/dto/user.dto';
import { UserService } from '../src/service/user.service';

describe('User', () => {
Expand All @@ -21,7 +21,7 @@ describe('User', () => {
email: '[email protected]'
};

const testUserDTO: UserDTO = {
const testUserDTO: <%= asDto('User')%> = {
login: 'userTestLogin',
firstName: 'UserTest',
lastName: 'UserTest',
Expand All @@ -45,7 +45,7 @@ describe('User', () => {
});

it('/POST create user', async () => {
const createdUser: UserDTO = (
const createdUser: <%= asDto('User')%> = (
await request(app.getHttpServer())
.post('/api/admin/users')
.send(testUserRequestObject)
Expand Down Expand Up @@ -82,16 +82,16 @@ describe('User', () => {
expect(updatedUser.firstName).toEqual(savedUser.firstName);
<%_ } _%>

await service.delete(savedUser as UserDTO);
await service.delete(savedUser as <%= asDto('User')%>);
});

it('/GET user with a login name', async () => {
testUserDTO.login = 'TestUserGet';
const savedUser: UserDTO = await service.save(testUserDTO);
const savedUser: <%= asDto('User')%> = await service.save(testUserDTO);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password, ...savedUserWithoutPassword } = savedUser;

const getUser: UserDTO = (
const getUser: <%= asDto('User')%> = (
await request(app.getHttpServer())
.get('/api/admin/users/' + savedUser.login)
.expect(200)
Expand All @@ -108,7 +108,7 @@ describe('User', () => {

it('/DELETE user', async () => {
testUserDTO.login = 'TestUserDelete';
const savedUser: UserDTO = await service.save(testUserDTO);
const savedUser: <%= asDto('User')%> = await service.save(testUserDTO);

await request(app.getHttpServer())
.delete('/api/admin/users/' + savedUser.login)
Expand Down
4 changes: 2 additions & 2 deletions generators/server/templates/server/src/client/request.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request as ExpressRequest} from 'express';
import { UserDTO } from '../service/dto/user.dto';
import { <%= asDto('User')%> } from '../service/dto/user.dto';

export interface Request extends ExpressRequest {
user?: UserDTO;
user?: <%= asDto('User')%>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseEntity } from './base/base.entity';
import { Exclude } from 'class-transformer';

@Entity('nhi_user')
export class User extends BaseEntity {
export class <%= asEntity('User') %> extends BaseEntity {
@Column({ unique: true })
login: string;
@Column({ nullable: true })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<%_ const UserEntity = asEntity('User') %>
import { MigrationInterface, QueryRunner, getRepository } from 'typeorm';
<%_ if (authenticationType !== 'oauth2') { _%>
import { User } from '../domain/user.entity';
import { <%= UserEntity %> } from '../domain/user.entity';
import { transformPassword } from '../security';
<%_ } _%>
import { Authority } from '../domain/authority.entity';
Expand All @@ -11,7 +12,7 @@ export class SeedUsersRoles1570200490072 implements MigrationInterface {
role2: Authority = { name: 'ROLE_USER' };

<%_ if (authenticationType !== 'oauth2') { _%>
user1: User = {
user1: <%= UserEntity %> = {
login: 'system',
password: 'system',
firstName: 'System',
Expand All @@ -24,7 +25,7 @@ export class SeedUsersRoles1570200490072 implements MigrationInterface {
lastModifiedBy: 'system'
};
user2: User = {
user2: <%= UserEntity %> = {
login: 'anonymoususer',
password: 'anonymoususer',
firstName: 'Anonymous',
Expand All @@ -37,7 +38,7 @@ export class SeedUsersRoles1570200490072 implements MigrationInterface {
lastModifiedBy: 'system'
};
user3: User = {
user3: <%= UserEntity %> = {
login: 'admin',
password: 'admin',
firstName: 'Administrator',
Expand All @@ -50,7 +51,7 @@ export class SeedUsersRoles1570200490072 implements MigrationInterface {
lastModifiedBy: 'system'
};
user4: User = {
user4: <%= UserEntity %> = {
login: 'user',
password: 'user',
firstName: 'User',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EntityRepository, Repository } from 'typeorm';
import { User } from '../domain/user.entity';
import { <%= asEntity('User') %> } from '../domain/user.entity';

@EntityRepository(User)
export class UserRepository extends Repository<User> {}
@EntityRepository(<%= asEntity('User') %>)
export class UserRepository extends Repository<<%= asEntity('User') %>> {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { UserDTO } from '../../service/dto/user.dto';
import { <%= asDto('User')%> } from '../../service/dto/user.dto';

@Injectable()
export class RolesGuard implements CanActivate {
Expand All @@ -14,7 +14,7 @@ export class RolesGuard implements CanActivate {
}

const request = context.switchToHttp().getRequest();
const user = request.user as UserDTO;
const user = request.user as <%= asDto('User')%>;

return user && user.authorities && user.authorities.some(role => roles.indexOf(role) >= 0);
}
Expand Down
Loading

0 comments on commit 3d1a9c0

Please sign in to comment.