After experimenting with MikroORM, I've encountered a situation that has left me perplexed.
Here is the snippet of code in question:
const user = await appCtx.em.findOne(User, {email, accessToken})
if (!user) {
return
}
console.log('!* user before', user);
user.accessToken = undefined
console.log('!* user after', user);
This code produces the following output in the console:
!* user before User {
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5061623132636236637d313669647d643536617d693369657d336564646531666336356768103120397d243523247e333f3d">[email protected]</a>',
name: 'Username',
verifyToken: null,
verifyTokenCreatedAt: null,
verifiedAt: 2024-10-24T06:07:22.417Z,
accessToken: '80b1b652-2f45-43db-8200-b9d003ad1ddb',
createdAt: 2024-10-24T06:07:22.400Z,
updatedAt: 2024-10-24T06:07:22.417Z,
id: '3eab5128-5549-401d-88f6-98c2ff9f517c'
}
!* user after User {
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8e9eab9baebeabeebf5b9bee1ecf5ecbdbee9f5e1bbe1edf5bbedececedb9eeebbebdefe098b9a8b1f5acbdabacf6bbb7b5">[email protected]</a>',
name: 'Username',
verifyToken: null,
verifyTokenCreatedAt: null,
verifiedAt: 2024-10-24T06:07:22.417Z,
createdAt: 2024-10-24T06:07:22.400Z,
updatedAt: 2024-10-24T06:07:22.417Z,
id: '3eab5128-5549-401d-88f6-98c2ff9f517c'
}
The issue at hand is my confusion over why the accessToken
property is missing from the 'user after' log.
I expect to see this property returned with a value of null
in my tests. What am I overlooking?
To provide a comprehensive overview, here is my Entity definition:
@Entity()
export class User {
[OptionalProps]?: 'createdAt' | 'updatedAt'
@PrimaryKey({type: "string"})
id = crypto.randomUUID()
@Property({type: "string", unique: true})
email: string
@Property({type: "string"})
name: string
@Property({type: "string", nullable: true})
verifyToken?: string
@Property({type: "Date", nullable: true})
verifyTokenCreatedAt?: Date
@Property({type: "Date", nullable: true})
verifiedAt?: Date
@Property({type: "String", nullable: true, index: true})
accessToken?: string
@Property({type: "Date"})
createdAt = new Date();
@Property({type: "Date", onUpdate: () => new Date() })
updatedAt = new Date();
constructor({email, name}: User) {
this.email = email
this.name = name
}
}
And here is how I initialize it:
export const orm = await MikroORM.init({
entities: [User],
dbName: mikroConfig.dbName,
host: mikroConfig.host,
user: mikroConfig.user,
password: mikroConfig.password,
logger: (message: string) => console.debug(message), // defaults to `console.log()`
})