0

I'm kind of new to mikro-orm, trying to wrap my head around as how to correctly define the relations between some of my entties in my collections.

The idea is to store all my pre-defined modules in ship-module collection. And then as I load the ship-entity all the related modules data would get resolved as well by the respective reference id. I'm storing these references as part of the ship creation process, I do see the IDs being saved correctly into the db, however, when I'm fetching the ship data, the modules atribute is always empty.

ship.entity.ts

@Entity()
export class ShipEntity extends BaseEntity {

  @Property({
    default: 'Ship 1'
  })
  name?: string;

  @Property()
  ownerID: ObjectId;

  @Property({
    default: 1
  })
  level?: number;

  @Property({
    default: Date.now()
  })
  createdDate?: number;

  @Property()
  coords: CellCoordinates;

  @Property({
    default: 0
  })
  galaxyId?: number;

  @ManyToMany(() => ShipModuleEntity)
  modules = new Collection<ShipModuleEntity>(this);

  constructor(ownerId: ObjectId,  galaxyId: number, name: string, coords: ICellCoordinates) {
    super();
    this.ownerID = ownerId;
    this.name = name;
    this.galaxyId = galaxyId;
  }
}

ship-module.entity.ts

@Entity()
export class ShipModuleEntity extends BaseEntity implements IShipModule {
  [OptionalProps]?: 'level' | 'description';

  @Property()
  type: ShipModuleType;

  @Property({
    default: 1
  })
  level?: number;

  @Property()
  name: string;

  @Property()
  description?: string;
}

this is how I'm trying to retrieve the data.

const shipsRepo = DI.em.fork().getRepository(ShipEntity);
const ship = await shipsRepo.findOne({ownerID: player._id }, { populate: ['modules'] })

So as part of the create ShipEntity process. All the data are populated correctly. Even the modules araay is pre-poulated with all the required module ObjectIds'. At least when I'm checking the data in the DB. The problem however, lies with the retrieval of the data, once I try look up any ship, I'm not getting anything. What I'd expect is to retrieve ShipEntity along with all modules. The moduels should be all resolved by the respective ID. SO insteaf of getting modules array width just the ObjectIds', I'd get the modules filled with all the ShipModule Entities.

But I'm not really sure if this is even posible. in Typegoose I used to do:

  @prop({
    required: true,
    ref: ShipModuleModel,
    type: Types.ObjectId
  })
  modules: Ref<ShipModuleModel>[]

Can anyone please let me know as why is this not working?

Spent days trying to solve this using AI, but no luck. Seems like SO comunity can't be replaced with AI just yet :)

4
  • 1
    This is way too unclear, can you put together a complete reproduction so its clear what you are doing? github.com/mikro-orm/reproduction Commented Oct 28 at 7:18
  • Likely unrelated, but you should never do em.fork().getRepository(), you want to keep a single map of repositories managed by the global EM, you do not want to have a repository that has its own context like this. Sounds like you don't use the RequestContext helper via middleware, which you should instead of forking manually like this. Commented Oct 28 at 7:20
  • I can't really make it all working in theat repo using sqlite. So instead I will try to describe the problem a little bit more. Commented Nov 2 at 21:16
  • You can use mongo for the reproduction too. code >>> words. Commented Nov 3 at 8:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.