Mikro-ORM relation to table without entity - postgresql

I have very weird case to deal with.
I have NestJs microservice which handles 3 entities related to each other.
In database there're bunch of other tables generated by other entities from other microservices (I don't have access on their codebase).
The problem is that, one of my entities is related to other table from other microservice, is there any way to create a relation between my entity and table property?
My entities: user.entity.ts:
#Entity({ tableName: 'users' })
export class UserEntity {
#PrimaryKey()
id: number;
#Property()
firstName: string;
#Property()
companyId: number;
...
}
I need to make a relation between user.companyId and companies.id, but I don't have entity for Companies, it's from another project.
Any ideas?

Related

TypeORM Postgres - there is no unique constraint matching given keys for referenced table

I am getting the error:
there is no unique constraint matching given keys for referenced table
My models are as follows:
#Entity()
export class User {
#PrimaryGeneratedColumn('uuid')
userId: string
#OneToMany(() => Position, position => position.user)
positions: Position[]
}
#Entity()
export class Position {
#PrimaryGeneratedColumn()
positionId: number
#ManyToOne(() => User, user => user.positions)
user: User
}
It seems like using the UUID strategy might be the error? But I also noted that the generated query defines the userID column with PK contraint:
ALTER TABLE "user" ADD CONSTRAINT "PK_d72ea127f30e21753c9e229891e" PRIMARY KEY ("userId")
Any help is very much appreciated.
In the end I realise there is some polluted migration files inside of /dist that was messing with the database. After deleting /dist and having all files regenerated, the problem was gone.
Polluted /dist is apparently a problem not well looked into and can affect tsnode-dev, typescript, nodemon, and nestjs dev instances that generates /dist on the fly and incrementally.

How to use Entity in Column type (TypeORM)

I have a problem with TypeORM (Postgresql) Column types. I have an entity:
#Entity()
export class Destination {
#Column({ type: 'varchar', array: true })
addresses: string[];
I want this entity to be the type for Column in other entity (this entity should be like a phonebook - a lot of numbers but you choose only one - this one for other entity). What is the best way to do this? Thanks in advice!
Before even talking about the TypeORM-specific implementation, I think what you are looking for is a many-to-one/one-to-many relation, right?
If that's the case, then here's how to annotate such relation when defining TypeORM entities: TypeORM docs;
example
You just simply set type of Column to Destination

Implementing 3 way relationship in Prisma

I also asked this question on Prisma forum.
** EDIT **: The forum has since been locked as read-only just 1 day after my question. It's sure getting scary because there is no official announcement of whether they'll stop developing Prisma 1 after implementing promised features or not. Surely they could have said something. And TBH, the unstable Prisma site does add to my shaking confidence in Prisma despite the number of stars in their repo.
I'm new to Prisma. So I have a 3-way relationship between User, Event and Role I would like to define. For each association of a User and an Event, there exists a Role for that association. If I were to design a database table for another ORM, I would have created a event_user table with user_id, event_id and role_id as columns to relate them.
Practically, a row of these 3 columns must be unique. Obviously, it would be good if Prisma can do the safeguarding of these constraints, but the obvious solution I see might not even come to Prisma 1.
My current design consists of the following:
type User {
// ...
eventUsers: [EventUser!]!
}
type Event {
// ...
eventUsers: [EventUser!]!
}
type EventUser {
role: Role!
event: Event!
user: User!
}
This design will make render all xxWhereUniquexx unusable, which is definitely a hassle to maintain relationships. upserts will certainly be unusable for maintaining the relationships.
How would one create a relationship like this in Prisma?
For some context, each user would have a list of global roles as well, so there would already be an association between User and Role. The question concerns the "local roles" for each event.
If each user already have a value for their respective roles, there would be no need for a third table (unless there is more information you'd like to be stored in your modal layer in which case the role type should be in the Role table).
Relationships are set with the #relation directive. You can apply it to either one of two tables in a relation but for clarity I'm apply them to the Event table. The example assumes users can be part of several events.
Tables:
enum ROLE_TYPE {
TYPE_ONE
TYPE_TWO
}
User {
user_id: ID! #id
events: [Event!]!
}
Event {
event_id: ID! #id
users: [User!]! #relation(link: INLINE)
}
Role {
role_id: ID! #id
type: ROLE_TYPE
event: Event! #relation(link: INLINE)
user: User! #relation(link: INLINE)
}
You can read more about relations on the Prisma site

Entity Framework: referencing entities from a model of another context

I have two Db contexts: CustomersDb which maps to a Customers database, and OrdersDb, which maps to Orders database.
I have a model for each context
Customer
{
Id,
Name
}
Order
{
Id,
Virtual Customer
}
How how do I reference the Customer entity from the Orders model correctly? When I seed these DBs, EF creates a Customers table in the Orders database, and obviously, I don't want that

How to map a JPA bidirectional relationship involving an abstract class?

Assuming the following simple schema:
Entity table - attributes common to all entities
entityId, timeCreated,...
Comments table - some entities have a collection of these
entityId, commentId, commentText,....
Person table.
pensonId (entityId), firstName, lastName,...
And the following Java inheritance structure:
BaseEntity - abstract - entity, join inheritance with a discriminator
CommentedEntity - abstract - introduces `comments` collection - mapping tbd
Person - concrete - entity - mapping trivial
How would we map bidirectional relationship between CommentedEntity and Comment? The code below is my best interpretation of examples I have found.
CommentedEntity
#OneToMany(mappedBy="owner", fetch=FetchType.EAGER)
private List<Comment> comments = new ArrayList<>();
Comment
#ManyToOne(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name="entityId", referencedColumnName="entityId")
private CommentedEntity owner;
Since CommentedEntity is an abstract class, this doesn't work.
owner references an unknown entity.
Making CommentedEntity an entity, would require giving it an id, so I don't think that makes
sense.
Since multiple concrete entities will have comments
collection, concrete entity name cannot be used in mapping.
How would we then bidirectionally map a person.commentList property?
If Person extends CommentedEntity then you wouldn't need an CommentedEntity owner for the Person entity because Person is part of aCommentedEntity`. In other words, there is no need for an owner field because a Person is a CommentedEntity.