feat(auth): Migrate auth module to DML (#10387)

* feat(auth): Migrate auth module to DML

* Create lazy-eagles-bow.md
This commit is contained in:
Adrien de Peretti
2024-12-02 11:58:04 +01:00
committed by GitHub
parent 913cf15e2b
commit 4ef353a7b9
7 changed files with 128 additions and 136 deletions
@@ -1,85 +1,21 @@
import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import { AuthIdentity } from "./auth-identity"
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import AuthIdentity from "./auth-identity"
const providerEntityIdIndexName = "IDX_provider_identity_provider_entity_id"
const providerEntityIdIndexStatement = createPsqlIndexStatementHelper({
name: providerEntityIdIndexName,
tableName: "provider_identity",
columns: ["entity_id", "provider"],
unique: true,
})
const authIdentityIndexName = "IDX_provider_identity_auth_identity_id"
const authIdentityIndexStatement = createPsqlIndexStatementHelper({
name: authIdentityIndexName,
tableName: "provider_identity",
columns: ["auth_identity_id"],
})
@Entity()
@providerEntityIdIndexStatement.MikroORMIndex()
@authIdentityIndexStatement.MikroORMIndex()
export default class ProviderIdentity {
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text" })
entity_id: string
@Property({ columnType: "text" })
provider: string
@ManyToOne(() => AuthIdentity, {
columnType: "text",
fieldName: "auth_identity_id",
mapToPk: true,
onDelete: "cascade",
export const ProviderIdentity = model
.define("provider_identity", {
id: model.id().primaryKey(),
entity_id: model.text(),
provider: model.text(),
auth_identity: model.belongsTo(() => AuthIdentity, {
mappedBy: "provider_identities",
}),
user_metadata: model.json().nullable(),
provider_metadata: model.json().nullable(),
})
auth_identity_id: string
@ManyToOne(() => AuthIdentity, {
persist: false,
})
auth_identity: Rel<AuthIdentity>
@Property({ columnType: "jsonb", nullable: true })
user_metadata: Record<string, unknown> | null
@Property({ columnType: "jsonb", nullable: true })
provider_metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@BeforeCreate()
@OnInit()
onCreate() {
this.id = generateEntityId(this.id, "provid")
this.auth_identity_id ??= this.auth_identity?.id ?? null
}
}
.indexes([
{
name: "IDX_provider_identity_provider_entity_id",
on: ["entity_id", "provider"],
unique: true,
},
])