Skip to content

Commit

Permalink
update to typeorm v0.3.x (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Dec 2, 2024
1 parent 1667d86 commit 99e494f
Show file tree
Hide file tree
Showing 23 changed files with 157 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-integration-angular.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
!contains(github.event.pull_request.labels.*.name, 'pr: disable-compare')
with:
generator-path: generator-jhipster-nodejs
cmd: cli.cjs generate-sample ${{ matrix.legacy-sample-file }} --template-name ${{ matrix.legacy-template-name }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
cmd: cli.cjs generate-sample ${{ matrix.sample }} --samples-folder ${{ matrix.samples-group }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
- name: 'INSTALL DEPENDENCIES of generated app'
run: npm install
if: steps.compare.outputs.equals != 'true'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-integration-microservice.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
!contains(github.event.pull_request.labels.*.name, 'pr: disable-compare')
with:
generator-path: generator-jhipster-nodejs
cmd: cli.cjs generate-sample ${{ matrix.sample }} --template-name ${{ matrix.sample }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
cmd: cli.cjs generate-sample ${{ matrix.sample }} --samples-folder ${{ matrix.samples-group }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
- name: 'INSTALL DEPENDENCIES of generated app'
run: npm install
if: steps.compare.outputs.equals != 'true'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-integration-react.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
!contains(github.event.pull_request.labels.*.name, 'pr: disable-compare')
with:
generator-path: generator-jhipster-nodejs
cmd: cli.cjs generate-sample ${{ matrix.legacy-sample-file }} --template-name ${{ matrix.legacy-template-name }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
cmd: cli.cjs generate-sample ${{ matrix.sample }} --samples-folder ${{ matrix.samples-group }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
- name: 'INSTALL DEPENDENCIES of generated app'
run: npm install
if: steps.compare.outputs.equals != 'true'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-integration-vue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
!contains(github.event.pull_request.labels.*.name, 'pr: disable-compare')
with:
generator-path: generator-jhipster-nodejs
cmd: cli.cjs generate-sample ${{ matrix.legacy-sample-file }} --template-name ${{ matrix.legacy-template-name }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
cmd: cli.cjs generate-sample ${{ matrix.sample }} --samples-folder ${{ matrix.samples-group }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
- name: 'INSTALL DEPENDENCIES of generated app'
run: npm install
if: steps.compare.outputs.equals != 'true'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ This project adds nodejs for your backend, **all TypeScript files generation, no
> - NestJS base app with web controllers, [swagger doc](https://github.com/nestjs/swagger) and [JWT or OAuth2 passport auth](https://github.com/nestjs/passport) services
> - The app starts with four seed users (admin, basic user and anonymous roles), as standard JHipster monolithic app, with SQLite for dev and configurable sql db for prod. But from the 1.5.0 release you can choose mongodb that uses a memory version for dev/test
> - The app starts with four seed users (admin, basic user and anonymous roles), as standard JHipster monolithic app, with SQLite for dev and configurable sql db for prod. MongoDB support is experimental in [TypeORM](https://github.com/typeorm/typeorm/blob/fdbb013d2279264e851dacc4c5c2e94567b65ab7/README.md?plain=1#L255) so is in JHipster NodeJS.
> - The app uses [TypeORM](https://github.com/nestjs/typeorm) and asks you a question for another sql db or for mongodb
Expand Down
2 changes: 1 addition & 1 deletion generators/node-server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const serverFiles = {
'src/security/guards/roles.guard.ts',
'src/security/guards/auth.guard.ts',
'src/security/role-type.ts',
'src/security/password-util.ts',
'src/security/decorators/auth-user.decorator.ts',
'src/security/decorators/roles.decorator.ts',
'src/security/index.ts',
Expand Down Expand Up @@ -88,6 +87,7 @@ export const serverFiles = {
condition: generator => generator.authenticationType === 'jwt',
templates: [
'src/web/rest/user.jwt.controller.ts',
'src/security/password-util.ts',
'src/security/passport.jwt.strategy.ts',
'src/security/payload.interface.ts',
'src/service/dto/password-change.dto.ts',
Expand Down
69 changes: 49 additions & 20 deletions generators/node-server/generator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFile } from 'node:fs/promises';
import BaseApplicationGenerator from 'generator-jhipster/generators/base-application';
import { createNeedleCallback } from 'generator-jhipster/generators/base/support';
import { createNeedleCallback, mutateData } from 'generator-jhipster/generators/base/support';
import { getEnumInfo } from 'generator-jhipster/generators/base-application/support';
import { TEMPLATES_WEBAPP_SOURCES_DIR } from 'generator-jhipster';
import { SERVER_NODEJS_SRC_DIR } from '../generator-nodejs-constants.js';
Expand Down Expand Up @@ -44,6 +44,22 @@ const dbTypes = {
'byte[]': 'blob',
};

const databaseDrivers = {
mongodb: 'mongodb',
mysql: 'mysql2',
postgresql: 'pg',
oracle: 'oracledb',
mssql: 'mssql',
};

const databaseDevDrivers = {
mongodb: 'mongodb-memory-server',
mysql: 'sqlite3',
postgresql: 'sqlite3',
oracle: 'sqlite3',
mssql: 'sqlite3',
};

export default class extends BaseApplicationGenerator {
oldNodejsVersion;
nodejsPackageJson;
Expand Down Expand Up @@ -99,6 +115,17 @@ export default class extends BaseApplicationGenerator {

get [BaseApplicationGenerator.PREPARING]() {
return this.asPreparingTaskGroup({
drivers({ applicationDefaults }) {
applicationDefaults({
__override__: false,
nodeProdDatabaseDriver: ({ databaseType, prodDatabaseType = databaseType }) => databaseDrivers[prodDatabaseType],
nodeDevDatabaseDriver: ({ databaseType, prodDatabaseType = databaseType }) => databaseDevDrivers[prodDatabaseType],
nodeProdDatabaseType: ({ databaseType, prodDatabaseType = databaseType }) =>
prodDatabaseType === 'postgresql' ? 'postgres' : prodDatabaseType,
nodeDevDatabaseType: ({ databaseType, devDatabaseType = databaseType }) =>
devDatabaseType === 'postgresql' ? 'postgres' : devDatabaseType,
});
},
async source({ source }) {
source.addEntityToNodeConfig = ({ entityFileName, persistClass }) =>
this.editFile(
Expand Down Expand Up @@ -142,28 +169,40 @@ export default class extends BaseApplicationGenerator {
});
}

get [BaseApplicationGenerator.PREPARING_EACH_ENTITY]() {
return this.asPreparingEachEntityTaskGroup({
async preparingEachEntityTemplateTask({ entity, application }) {
for (const field of entity.fields) {
const { fieldType } = field;
field.nodejsFieldType = field.fieldValues ? fieldType : (fieldTypes[fieldType] ?? 'any');
field.nodejsColumnType = sanitizeDbType(dbTypes[fieldType], application.devDatabaseType);
get [BaseApplicationGenerator.PREPARING_EACH_ENTITY_FIELD]() {
return this.asPreparingEachEntityFieldTaskGroup({
async preparingEachEntityTemplateTask({ application, field }) {
const { fieldType } = field;
if (field.skipServer) return;

if (field.fieldValues) {
mutateData(field, {
__override__: true,
nodejsFieldType: fieldType,
nodejsColumnType: application.prodDatabaseTypePostgresql ? 'varchar' : 'simple-enum',
});
} else {
mutateData(field, {
__override__: true,
nodejsFieldType: fieldTypes[fieldType] ?? 'any',
nodejsColumnType: sanitizeDbType(dbTypes[fieldType], application.devDatabaseType),
});
}
},
});
}

get [BaseApplicationGenerator.WRITING]() {
return this.asWritingTaskGroup({
async cleanup({ control }) {
async cleanup({ application, control }) {
if (control.existingProject) {
await control.cleanupFiles(this.oldNodejsVersion, {
'3.0.1': [
'.server.eslintrc.json',
'.server.eslintignore',
'server/src/repository/authority.repository.ts',
'server/src/repository/user.repository.ts',
[application.authenticationTypeOauth2, 'node/src/security/password-util.ts'],
],
});
}
Expand Down Expand Up @@ -246,17 +285,7 @@ export default class extends BaseApplicationGenerator {
get [BaseApplicationGenerator.POST_WRITING]() {
return this.asPostWritingTaskGroup({
adjustWorkspacePackageJson({ application }) {
const { nodeServerDependencies, nodeDependencies } = application;

const overrides = {
'@nestjs/typeorm': {
'@nestjs/common': nodeServerDependencies['@nestjs/common'],
'@nestjs/core': nodeServerDependencies['@nestjs/core'],
},
};

this.packageJson.merge({ overrides });
this.mergeDestinationJson('server/package.json', { overrides });
const { nodeDependencies } = application;

if (application.clientFrameworkAngular) {
this.packageJson.merge({
Expand Down
10 changes: 5 additions & 5 deletions generators/node-server/resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@nestjs/platform-express": "10.4.12",
"@nestjs/serve-static": "4.0.2",
"@nestjs/swagger": "8.0.7",
"@nestjs/typeorm": "8.0.5",
"@nestjs/typeorm": "10.0.2",
"bcrypt": "5.1.1",
"browser-sync-client": "3.0.3",
"class-transformer": "0.5.1",
Expand All @@ -19,7 +19,7 @@
"express-session": "1.18.1",
"js-yaml": "4.1.0",
"jwt-decode": "4.0.0",
"mongodb": "6.11.0",
"mongodb": "5.9.2",
"mongodb-memory-server": "10.1.2",
"mssql": "11.0.1",
"mysql2": "3.11.5",
Expand All @@ -28,12 +28,12 @@
"passport-jwt": "4.0.1",
"passport-oauth2": "1.8.0",
"pg": "8.13.1",
"reflect-metadata": "0.1.14",
"reflect-metadata": "0.2.2",
"rxjs": "7.8.1",
"sqlite3": "5.1.7",
"swagger-ui-express": "5.0.1",
"typeorm": "0.2.45",
"typeorm-encrypted": "0.7.0"
"typeorm": "0.3.20",
"typeorm-encrypted": "0.8.0"
},
"devDependencies": {
"@eslint/js": "9.16.0",
Expand Down
10 changes: 6 additions & 4 deletions generators/node-server/templates/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
"lint:fix": "echo \"INFO: no client found\"",
"prettier:check": "prettier --check \"{,src/**/}*.{<%= prettierExtensions %>}\"",
"prettier:format": "prettier --write \"{,src/**/}*.{<%= prettierExtensions %>}\"",
"start:app": "cd server && <%= clientPackageManager %> run start",
"test:client": "<%= clientPackageManager %> test --workspace client",
"test:server": "<%= clientPackageManager %> test --workspace server",
"start": "concurrently -k -s first -n application,client -c red,green npm:start:server npm:start:client",
"start:server": "<%= clientPackageManager %> run start -w server",
"start:client": "<%= clientPackageManager %> run start -w client",
"test:client": "<%= clientPackageManager %> test -w client",
"test:server": "<%= clientPackageManager %> test -w server",
<%_ if (!databaseTypeSql) { _%>
"pretest:server:e2e": "npm run docker:db:up",
<%_ } _%>
"test:server:e2e": "<%= clientPackageManager %> run test:e2e --workspace server",
"test:server:e2e": "<%= clientPackageManager %> run test:e2e -w server",
<%_ if (!databaseTypeSql) { _%>
"posttest:server:e2e": "npm run docker:db:down",
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('<%= entityClass %> Controller', () => {
);

afterEach(async () => {
await app.close();
await app?.close();
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('Account', () => {

});

it('/POST change password', async () => {
it<%- databaseTypeMongodb ? '.skip' : ''%>('/POST change password', async () => {
await request(app.getHttpServer())
.post('/api/account/change-password')
.send(testPasswordChange)
Expand Down Expand Up @@ -155,6 +155,6 @@ describe('Account', () => {

afterEach(async () => {
await service.delete(userAuthenticated);
await app.close();
await app?.close();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('App', () => {
.expect(200));

<%_ if (!skipUserManagement && authenticationType !=='oauth2') { _%>
it('/POST authenticate get jwt authenticate OK', () => request(app.getHttpServer())
it<%- databaseTypeMongodb ? '.skip' : ''%>('/POST authenticate get jwt authenticate OK', () => request(app.getHttpServer())
.post('/api/authenticate')
.send(testUserLogin)
.expect(200));
Expand All @@ -66,6 +66,6 @@ describe('App', () => {
<%_ } _%>

afterEach(async () => {
await app.close();
await app?.close();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ describe('User', () => {
});

afterEach(async () => {
await app.close();
await app?.close();
});
});
18 changes: 2 additions & 16 deletions generators/node-server/templates/server/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"typeorm:schema:sync": "npm run typeorm -- schema:sync"
},
"dependencies": {
"<%- nodeProdDatabaseDriver %>": "<%- nodeServerDependencies[nodeProdDatabaseDriver] %>",
"<%- nodeDevDatabaseDriver %>": "<%- nodeServerDependencies[nodeDevDatabaseDriver] %>",
<%_ if (authenticationType === 'oauth2') { _%>
"@nestjs/axios": "<%- nodeServerDependencies['@nestjs/axios'] %>",
"express-session": "<%- nodeServerDependencies['express-session'] %>",
Expand Down Expand Up @@ -59,17 +61,6 @@
"js-yaml": "<%- nodeServerDependencies['js-yaml'] %>",
<%_ if (authenticationType === 'oauth2') { _%>
"jwt-decode": "<%- nodeServerDependencies['jwt-decode'] %>",
<%_ } _%>
<%_ if (prodDatabaseType === 'mongodb') { _%>
"mongodb": "<%- nodeServerDependencies['mongodb'] %>",
<%_ } else if (prodDatabaseType === 'mysql') {_%>
"mysql2": "<%- nodeServerDependencies['mysql2'] %>",
<%_ } else if (prodDatabaseType === 'postgresql') {_%>
"pg": "<%- nodeServerDependencies['pg'] %>",
<%_ } else if (prodDatabaseType === 'oracle') {_%>
"oracledb": "<%- nodeServerDependencies['oracledb'] %>",
<%_ } else if (prodDatabaseType === 'mssql') {_%>
"mssql": "<%- nodeServerDependencies['mssql'] %>",
<%_ } _%>
"passport": "<%- nodeServerDependencies['passport'] %>",
<%_ if (authenticationType === 'jwt') { _%>
Expand All @@ -79,11 +70,6 @@
<%_ } _%>
"reflect-metadata": "<%- nodeServerDependencies['reflect-metadata'] %>",
"rxjs": "<%- nodeServerDependencies['rxjs'] %>",
<%_ if (databaseType === 'mongodb') { _%>
"mongodb-memory-server": "<%- nodeServerDependencies['mongodb-memory-server'] %>",
<%_ } else {_%>
"sqlite3": "<%- nodeServerDependencies['sqlite3'] %>",
<%_ } _%>
"swagger-ui-express": "<%- nodeServerDependencies['swagger-ui-express'] %>",
"typeorm": "<%- nodeServerDependencies['typeorm'] %>",
"typeorm-encrypted": "<%- nodeServerDependencies['typeorm-encrypted'] %>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class <%= persistClass %> extends BaseEntity {
<%_ } _%>
<%_ if (field.fieldIsEnum) { _%>
@Column({type: 'simple-enum', name: '<%-fieldNameAsDatabaseColumn %>', enum: <%= fieldType %>})
@Column({type: '<%- field.nodejsColumnType %>', name: '<%-fieldNameAsDatabaseColumn %>', enum: <%= fieldType %>})
<%_ } else if (['Instant', 'ZonedDateTime', 'LocalDate'].includes(fieldType)) { _%>
@Column({<%_ if(field.nodejsColumnType) { _%> type: '<%-field.nodejsColumnType%>' ,<%_ }_%> name: "<%-fieldNameAsDatabaseColumn %>"<% if (!required) { %>, nullable: true<% } %><% if (unique) { %>, unique: true<% } %>})
<%_ } else if (fieldType === 'BigDecimal') { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ export class CreateTables1570200270081 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {

if (process.env.BACKEND_ENV === 'prod' || process.env.BACKEND_ENV === 'dev') {
if (queryRunner.isTransactionActive) {
await queryRunner.commitTransaction();
}
if (queryRunner.isTransactionActive) {
await queryRunner.commitTransaction();
}
}


await queryRunner.connection.synchronize();
}
await queryRunner.connection.synchronize();
}

// eslint-disable-next-line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ export class SeedUsersRoles1570200490072 implements MigrationInterface {
this.user3.authorities= [adminRole, userRole];
this.user4.authorities= [userRole];
await Promise.all([this.user1, this.user2, this.user3, this.user4].map(u => transformPassword(u)));
const users = await Promise.all([this.user1, this.user2, this.user3, this.user4].map(u => transformPassword({ ...u })));
await userRepository.save([this.user1, this.user2, this.user3, this.user4]);
await userRepository.save(users);
<%_ } _%>
}

Expand Down
Loading

0 comments on commit 99e494f

Please sign in to comment.