Skip to content

Commit

Permalink
Merge pull request #797 from conceptadev/feature/typeorm-v03-nest-v9
Browse files Browse the repository at this point in the history
feat(typeorm): create initial support for typeorm ^0.3.0 and nest ^9.0.0
  • Loading branch information
michaelyali authored Nov 29, 2022
2 parents 5e4e363 + 3977dc9 commit 44f2478
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 253 deletions.
13 changes: 13 additions & 0 deletions integration/crud-typeorm/orm.mysql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DataSource } from 'typeorm';

exports.default = new DataSource({
type: 'mysql',
host: '127.0.0.1',
port: 3316,
username: 'nestjsx_crud',
password: 'nestjsx_crud',
database: 'nestjsx_crud',
entities: ['./**/*.entity.ts'],
migrationsTableName: 'orm_migrations',
migrations: ['./seeds.ts'],
});
13 changes: 13 additions & 0 deletions integration/crud-typeorm/orm.postgres.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DataSource } from 'typeorm';

exports.default = new DataSource({
type: 'postgres',
host: '127.0.0.1',
port: 5455,
username: 'root',
password: 'root',
database: 'nestjsx_crud',
entities: ['./**/*.entity.ts'],
migrationsTableName: 'orm_migrations',
migrations: ['./seeds.ts'],
});
6 changes: 2 additions & 4 deletions integration/crud-typeorm/projects/user-project.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ export class UserProject {
@Column({ nullable: true })
public review!: string;

@ManyToOne((type) => Project, (el) => el.userProjects, {
primary: true,
@ManyToOne(() => Project, (el) => el.userProjects, {
persistence: false,
onDelete: 'CASCADE',
})
public project: Project;

@ManyToOne((type) => User, (el) => el.userProjects, {
primary: true,
@ManyToOne(() => User, (el) => el.userProjects, {
persistence: false,
})
public user: User;
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
"test:coverage": "yarn test:all --coverage",
"test:coveralls": "yarn test:coverage --coverageReporters=text-lcov | coveralls",
"test:all": "yarn test:mysql && yarn test:postgres",
"test:postgres": "yarn db:prepare:typeorm && yarn test",
"test:postgres": "yarn db:prepare:typeorm:postgres && yarn test",
"test:mysql": "yarn db:prepare:typeorm:mysql && TYPEORM_CONNECTION=mysql yarn test",
"start:typeorm": "npx nodemon -w ./integration/crud-typeorm -e ts node_modules/ts-node/dist/bin.js integration/crud-typeorm/main.ts",
"db:cli:typeorm": "cd ./integration/crud-typeorm && npx ts-node -r tsconfig-paths/register ../../node_modules/typeorm/cli.js",
"db:sync:typeorm": "yarn db:cli:typeorm schema:sync -f=orm",
"db:drop:typeorm": "yarn db:cli:typeorm schema:drop -f=orm",
"db:seeds:typeorm": "yarn db:cli:typeorm migration:run -f=orm",
"db:prepare:typeorm": "yarn db:drop:typeorm && yarn db:sync:typeorm && yarn db:seeds:typeorm",
"db:prepare:typeorm:mysql": "yarn db:drop:typeorm -c=mysql && yarn db:sync:typeorm -c=mysql && yarn db:seeds:typeorm -c=mysql",
"db:sync:typeorm": "yarn db:cli:typeorm schema:sync",
"db:drop:typeorm": "yarn db:cli:typeorm schema:drop",
"db:seeds:typeorm": "yarn db:cli:typeorm migration:run",
"db:prepare:typeorm:postgres": "yarn db:drop:typeorm -d=orm.postgres.ts && yarn db:sync:typeorm -d=orm.postgres.ts && yarn db:seeds:typeorm -d=orm.postgres.ts",
"db:prepare:typeorm:mysql": "yarn db:drop:typeorm -d=orm.mysql.ts && yarn db:sync:typeorm -d=orm.mysql.ts && yarn db:seeds:typeorm -d=orm.mysql.ts",
"format": "npx pretty-quick --pattern \"packages/**/!(*.d).ts\"",
"lint": "npx eslint \"packages/**/!(*.d).ts\" --fix",
"commit": "npx git-cz",
Expand All @@ -47,12 +47,12 @@
}
},
"dependencies": {
"@nestjs/common": "8.4.3",
"@nestjs/core": "8.4.3",
"@nestjs/platform-express": "8.4.3",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/swagger": "5.2.1",
"@nestjs/testing": "8.4.3",
"@nestjs/typeorm": "8.0.3",
"@nestjs/testing": "^9.0.0",
"@nestjs/typeorm": "^9.0.0",
"@zmotivat0r/mrepo": "0.8.1",
"class-transformer": "0.3.2",
"class-validator": "0.13.2",
Expand All @@ -64,7 +64,7 @@
"reflect-metadata": "0.1.13",
"rxjs": "7.5.5",
"swagger-ui-express": "4.3.0",
"typeorm": "0.2.45"
"typeorm": "^0.3.0"
},
"devDependencies": {
"@nuxtjs/opencollective": "0.2.2",
Expand Down
30 changes: 16 additions & 14 deletions packages/crud-typeorm/src/typeorm-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
ObjectLiteral,
Repository,
SelectQueryBuilder,
ConnectionOptions,
DataSourceOptions,
EntityMetadata,
} from 'typeorm';

Expand All @@ -41,7 +41,7 @@ interface IAllowedRelation {
}

export class TypeOrmCrudService<T> extends CrudService<T> {
protected dbName: ConnectionOptions['type'];
protected dbName: DataSourceOptions['type'];

protected entityColumns: string[];

Expand Down Expand Up @@ -564,13 +564,13 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
? cond.select.filter((column) => allowedRelation.allowedColumns.some((allowed) => allowed === column))
: allowedRelation.allowedColumns;

const select = [
...allowedRelation.primaryColumns,
...(isArrayFull(options.persist) ? options.persist : []),
...columns,
].map((col) => `${alias}.${col}`);
const select = new Set(
[...allowedRelation.primaryColumns, ...(isArrayFull(options.persist) ? options.persist : []), ...columns].map(
(col) => `${alias}.${col}`,
),
);

builder.addSelect(select);
builder.addSelect(Array.from(select));
}
}

Expand Down Expand Up @@ -783,13 +783,15 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
? query.fields.filter((field) => allowed.some((col) => field === col))
: allowed;

const select = [
...(options.persist && options.persist.length ? options.persist : []),
...columns,
...this.entityPrimaryColumns,
].map((col) => `${this.alias}.${col}`);
const select = new Set(
[
...(options.persist && options.persist.length ? options.persist : []),
...columns,
...this.entityPrimaryColumns,
].map((col) => `${this.alias}.${col}`),
);

return select;
return Array.from(select);
}

protected getSort(query: ParsedRequestParams, options: QueryOptions) {
Expand Down
2 changes: 1 addition & 1 deletion packages/crud-typeorm/test/c.basic-crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ describe('#crud-typeorm', () => {

describe('#findOne', () => {
it('should return one entity', async () => {
const data = await service.findOne(1);
const data = await service.findOne({ where: { id: 1 } });
expect(data.id).toBe(1);
});
});
Expand Down
10 changes: 2 additions & 8 deletions packages/crud-typeorm/test/d.crud-auth.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
Controller,
INestApplication,
Injectable,
CanActivate,
ExecutionContext,
} from '@nestjs/common';
import { Controller, INestApplication, Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { APP_FILTER, APP_GUARD } from '@nestjs/core';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
Expand All @@ -31,7 +25,7 @@ describe('#crud-typeorm', () => {

async canActivate(ctx: ExecutionContext): Promise<boolean> {
const req = ctx.switchToHttp().getRequest();
req[USER_REQUEST_KEY] = await this.usersService.findOne(1);
req[USER_REQUEST_KEY] = await this.usersService.findOne({ where: { id: 1 } });

return true;
}
Expand Down
Loading

0 comments on commit 44f2478

Please sign in to comment.