import { BaseEntity, Collection, Entity, ManyToOne, MetadataStorage, MikroORM, OneToMany, PrimaryKey, Property, ReflectMetadataProvider, types, Utils, type Dictionary, type EntityMetadata, type Opt, } from '@mikro-orm/core'; import { defineConfig } from '@mikro-orm/sqlite'; import { mixin, type Constructor } from '@wener/utils'; import { test } from 'vitest'; let _GlobalEntityId = 0; let _map = new WeakMap(); MetadataStorage.getMetadataFromDecorator = (target: T & Dictionary): EntityMetadata => { let id = _map.get(target); if (!id) { id = _GlobalEntityId++; _map.set(target, id); } let name = target.name; // restrict to mixin entity if (!name.endsWith('__') && name.endsWith('MixinEntity')) { name = `${target.name}__${id}__`; } const path = Utils.lookupPathFromDecorator(name); const meta = MetadataStorage.getMetadata(name, path); Object.defineProperty(target, '__path', { value: path, writable: true }); Object.defineProperty(target, 'name', { value: name, writable: true }); return meta; }; test('mixins', async () => { const orm = await getOrm(); const em = orm.em.fork(); // wrong console.log(Object.keys(em.getMetadata(AccountEntity).properties)); console.log(Object.keys(Utils.getGlobalStorage('metadata'))); console.log(UserEntity.__path); console.log(AccountEntity.__path); MetadataStorage.getMetadata(); }); @Entity({ abstract: true }) abstract class MinimalBaseEntity extends BaseEntity { @PrimaryKey({ type: types.string, defaultRaw: 'public.gen_ulid()', nullable: false }) id!: string & Opt; } async function getOrm() { const orm = await MikroORM.init( defineConfig({ entities: [UserEntity, AccountEntity], discovery: { disableDynamicFileAccess: true, requireEntitiesArray: true }, dbName: ':memory:', metadataProvider: ReflectMetadataProvider, debug: true, }), ); let em = orm.em.fork(); for (const schema of [ ` create table users ( id char(32) primary key default (lower(hex(randomblob(16)))), uid char(36) not null default ( lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))), 2) || '-' || substr('89ab', abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))), 2) || '-' || lower(hex(randomblob(6)))), eid text, created_at timestamp not null default current_timestamp, updated_at timestamp not null default current_timestamp, deleted_at timestamp, state text not null default 'Active', status text not null default 'Active', attributes json not null default '{}', properties json not null default '{}', extensions json not null default '{}' ); `, ` create table account ( id char(32) primary key default (lower(hex(randomblob(16)))), uid char(36) not null default ( lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))), 2) || '-' || substr('89ab', abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))), 2) || '-' || lower(hex(randomblob(6)))), eid text, created_at timestamp not null default current_timestamp, updated_at timestamp not null default current_timestamp, deleted_at timestamp, state text not null default 'Active', status text not null default 'Active', attributes json not null default '{}', properties json not null default '{}', extensions json not null default '{}', user_id text ); `, ]) { await em.execute(schema); } return { orm, em }; } @Entity({ tableName: 'users' }) class UserEntity extends mixin(MinimalBaseEntity, createStateStatusEntity({ state: 'Active', status: 'Active' })) { @OneToMany(() => AccountEntity, (e) => e.user) accounts = new Collection(this); } @Entity({ tableName: 'account' }) class AccountEntity extends mixin( MinimalBaseEntity, withVendorRefEntity, createStateStatusEntity({ state: 'Active', status: 'Active' }), ) { @ManyToOne(() => UserEntity) user?: UserEntity; } function withVendorRefEntity(Base: TBase) { @Entity({ abstract: true }) class HasVendorRefMixinEntity extends Base { // vendor @Property({ type: types.string, nullable: true }) cid?: string; // vendor external id @Property({ type: types.string, nullable: true }) rid?: string; } console.log(`===`, HasVendorRefMixinEntity.name); // Object.defineProperty(HasVendorRefMixinEntity, 'name', { // value: `${HasVendorRefMixinEntity.name}_${ClassID++}`, // }); return HasVendorRefMixinEntity; } function createStateStatusEntity({ status, state }: { status: string; state: string }) { return function withStatusEntity(Base: TBase) { @Entity({ abstract: true }) class HasStatusMixinEntity extends Base { @Property({ type: types.string, nullable: false, default: state }) state!: string & Opt; @Property({ type: types.string, nullable: false, default: status }) status!: string & Opt; } // Object.defineProperty(HasStatusEntity, 'name', { // value: `${HasStatusEntity.name}_${_GlobalEntityId++}`, // }); // console.warn(`===`, HasStatusEntity.name); return HasStatusMixinEntity; }; }