Replies: 2 comments 5 replies
-
|
I prefer to use the simplest possible steps: export const User = defineEntity({
name: 'User',
tableName: 'users',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' },
birthDate: { type: 'date' },
loyaltyPoints: { type: 'number' },
},
methods: {
getFullName: (user) => `${user.firstName} ${user.lastName}`,
calculateAge: (user) => differenceInYears(new Date(), user.birthDate)
},
});If you want to declare methods for both Class-Based and defineEntity, you can promote the relevant methods: const getFullName = (this: User) => `${user.firstName} ${user.lastName}`
const calculateAge = (this: User) => differenceInYears(new Date(), user.birthDate)
// in defineEntity
export const User = defineEntity({
name: 'User',
tableName: 'users',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' },
birthDate: { type: 'date' },
loyaltyPoints: { type: 'number' },
},
methods: {
getFullName,
calculateAge
},
});
// in Class-Based
@Entity('users', { methods: () => UserMethods })
export class User extends BaseEntity {
@Property()
firstName!: string;
@Property()
lastName!: string;
@Property()
birthDate!: Date;
@Property()
loyaltyPoints!: number;
getFullName = getFullName
calculateAge = calculateAge
} |
Beta Was this translation helpful? Give feedback.
5 replies
-
|
This should now be possible with #7104: https://mikro-orm.io/docs/next/entity-schema#adding-custom-methods The intellisense felt super slow, that should be hopefully improved in general with #7108 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
Love the direction MikroORM is heading! I'd like to propose adding entity methods support that works with both the decorator-based class entities and the new defineEntity API.
The Problem
Current State:
What We Want:
Proposed Solution
Core Concept: First-Parameter Binding
Methods are defined as pure functions where the first parameter is the entity instance. MikroORM automatically binds this parameter when called on an entity.
API: For Class-Based Entities
Alternative: Multiple Method Classes
API: For defineEntity
Why First-Parameter Approach?
Would love to hear thoughts from the community and maintainers!
Beta Was this translation helpful? Give feedback.
All reactions