Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAPPER - MikroORM: Circular dependency, Cannot access 'Person' before initialization

I am unable to make relationships work with MikroORM

I keep getting this error

ReferenceError: Cannot access 'Person' before initialization
    at Object.<anonymous> (/Users/mauroinsacco/Documents/tests/svelte/my-site/__sapper__/dev/server/server.js:74:31)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47

This is the source code where the problem lies, they rely on each other



import { Connection, IDatabaseDriver, MikroORM } from "@mikro-orm/core";
import { Entity, Property, OneToOne, PrimaryKey } from "@mikro-orm/core";

export class BaseEntity {
  @PrimaryKey()
  id!: number;
}

@Entity()
export class Account extends BaseEntity {
  @Property()
  username: string;

  @OneToOne(() => "Person", "account")
  person: Person;
}

@Entity()
export class Person extends BaseEntity {
  @Property()
  name!: string;

  @OneToOne("Account")
  account: Account;

  constructor() {
    super();
  }
}

export let orm: MikroORM<IDatabaseDriver<Connection>>;
export async function initDatabase() {
  orm = await MikroORM.init({
    entities: [Person, Account],
    dbName: "postgres",
    type: "postgresql", // one of `mongo` | `mysql` | `mariadb` | `postgresql` | `sqlite`
    clientUrl: "connectionstring", // defaults to 'mongodb://localhost:27017' for mongodb driver
  });
}

This is the generated build/devbuild output

class BaseEntity {
}
__decorate([
    core.PrimaryKey(),
    __metadata("design:type", Number)
], BaseEntity.prototype, "id", void 0);

let orm;
let Account = class Account extends BaseEntity {
};
__decorate([
    core.Property(),
    __metadata("design:type", String)
], Account.prototype, "username", void 0);
__decorate([
    core.OneToOne(() => "Person", "account"),
    __metadata("design:type", Person) // <------------ IT CRASHES HERE, PERSON IS NOT DEFINED
], Account.prototype, "person", void 0);
Account = __decorate([
    core.Entity()
], Account);
let Person = class Person extends BaseEntity {
    constructor() {
        super();
    }
};
__decorate([
    core.Property(),
    __metadata("design:type", String)
], Person.prototype, "name", void 0);
__decorate([
    core.OneToOne("Account"),
    __metadata("design:type", Account)
], Person.prototype, "account", void 0);
Person = __decorate([
    core.Entity(),
    __metadata("design:paramtypes", [])
], Person);

I'm using the default sapper rollup configuration and default typescript configuration with added "experimentalDecorators": true, "emitDecoratorMetadata": true, "esModuleInterop": true

I cant seem to find any solution online, I've no clue if its a sapper problem or rollup problem and have no idea what to touch, any help is highly appreciated

like image 760
Mauro Insacco Avatar asked Apr 29 '26 10:04

Mauro Insacco


1 Answers

for anyone stumbling upon this issue: use Rel wrapper as described here

import { Rel } from '@mikro-orm/core';

@Entity()
export class Book {

  @ManyToOne(() => Author)
  author!: Rel<Author>; // <<<---- this one is your friend

}
like image 84
grreeenn Avatar answered May 01 '26 01:05

grreeenn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!