node.js - How to partially save an entity's properties to different database? - Stack Overflow

admin2025-04-16  5

Background

I'm implementing an API endpoint for creating a user with MikroORM. Requirement tells me that I need the user info saved on a PostgreSQL server—except for user password; I need the passwords stored on the Firebase Authentication backend for some reason.

Wanted Behavior

I'm trying to get the following to work.

const newUser = new User({
  email: '[email protected]',
  password: 'mypassword'
});

em.persist(newUser).flush(); // save email to Postgres, save password to Firebase

My failed attempt at the solution

I'm trying to lean on MikroORM's Virtual Properties feature.

@Entity()
class User {
    /** -- snipped -- */

    @Property({ type: 'varchar', length: 50 })
    email!: number;

    @Property({ persist: false })
    set password(value: string) {
        setPasswordOnFirebase(this, value); // password gets saved before persist and flush
                                            // how to even handle async saving and errors?
    }
}

Questions

Is it possible in MikroORM to accomplish the wanted behavior I have in mind? Or should I simply create a separate endpoint for custom storing of password?

转载请注明原文地址:http://anycun.com/QandA/1744777533a87483.html