使用typeorm Nest js加入视图函数postgresql

问题描述 投票:0回答:1

我正在从另一个应用程序访问数据库。我在 Nest Js 中创建了两个视图作为实体,即“view_user”和“view_profile”。是否可以将“view_user”与“view_profile”连接起来,类似于典型类型 ORM 中的连接表?下面是我用来连接这两个视图的方法,但是我遇到了这样的错误消息:

关系 UsersView.profiles 没有连接列。

用户查看

import { JoinColumn, OneToMany, ViewColumn, ViewEntity } from 'typeorm';
import { ProfilesView } from 'src/modules/profiles/entity/profiles.entity';

@ViewEntity({
    name: 'view_users',
    expression: `SELECT * FROM users`,
})
export class UsersView {
    @ViewColumn() id: number;
    @ViewColumn() userName: string;
    @ViewColumn() email: string;
    @ViewColumn() phoneNumber: string;
    @ViewColumn() createdAt: Date;
    @ViewColumn() updatedAt: Date;
    @ViewColumn() phoneCountryCode: string;
    @ViewColumn() deletedAt: Date;
    @ViewColumn() isBlacklist: boolean;
    @ViewColumn() fcmNotificationId: string;

    @OneToMany(() => ProfilesView, (profiles) => profiles.user)
    @JoinColumn({ name: 'userId', referencedColumnName: 'id' })
    profiles: ProfilesView[];
}

个人资料查看

import { JoinColumn, ManyToOne, ViewColumn, ViewEntity } from 'typeorm';
import { UsersView } from 'src/modules/users/entity/users.entity';

@ViewEntity({
    name: 'view_profiles',
    expression: `SELECT * FROM profile`,
})
export class ProfilesView {
    @ViewColumn() id: number;
    @ViewColumn() name: string;
    @ViewColumn() gender: string;
    @ViewColumn() phoneNumber: string;
    @ViewColumn() phoneCountryCode: string;
    @ViewColumn() email: string;
    @ViewColumn() identityCardNumber: string;
    @ViewColumn() isPatient: number;
    @ViewColumn() birthDate: Date;
    @ViewColumn() userId: number;
    @ViewColumn() unlocked: number;
    @ViewColumn() linked: number;
    @ViewColumn() createdAt: Date;
    @ViewColumn() updatedAt: Date;
    @ViewColumn() bpjsCardNumber: string;
    @ViewColumn() hasBpjs: number;
    @ViewColumn() deletedAt: Date;

    @ManyToOne(() => UsersView, (user) => user.profiles)
    @JoinColumn({ name: 'userId' })
    user: UsersView;
}







用户服务

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ILike, Repository } from 'typeorm';
import { PageRequestDto } from 'src/common/request/request.options.dto';
import { UsersView } from './entity/users.entity';

@Injectable()
export class UsersService {
    constructor(
        @InjectRepository(UsersView)
        private repository: Repository<UsersView>,
    ) {}

    async getData(request: PageRequestDto) {
        const { limit, page, orderBy, orderType, search, searchBy } = request;

        const result = await this.repository.find({
            where: {
                ...(search && { phoneNumber: ILike(`%${search.trim()}%`) }),
            },
            ...(page && { skip: (page - 1) * limit }),
            ...(limit && { take: limit }),
            order: {
                ...(orderBy && { [orderBy]: orderType ? orderType : 'DESC' }),
                ...(!orderBy && { id: orderType ? orderType : 'DESC' }),
            },
            relations: {
                profiles: true,
            },
        });

        const count = await this.repository.count({
            where: {
                ...(search && { phoneNumber: ILike(`%${search.trim()}%`) }),
            },
        });
        return { result: result, count: count };
    }
}

这是我加入视图后的预期数据结果

[
    {
        id: 1,
        phoneNumber: 'xxxx', 
        profile: [
            {
                id: 1,
                name: 'xxx'
            }, 
            {
                id: 2,
                name: 'xxx'
            }
        ]
    },
    {
        id: 1,
        phoneNumber: 'xxxx', 
        profile: [
            {
                id: 1,
                name: 'xxx'
            }, 
            {
                id: 2,
                name: 'xxx'
            }
        ]
    }
]
typescript postgresql nestjs typeorm
1个回答
0
投票

您应该只在关系的一侧(多对一中的“多”侧)使用 JoinColumn 装饰器。所以你应该在你的 ProfilesView 中添加这个

    @ManyToOne(() => UsersView, (user) => user.profiles)
    @JoinColumn({ name: 'userId', referencedColumnName: 'id' })
    user: UsersView;
© www.soinside.com 2019 - 2024. All rights reserved.