refactor: migrate api key module to DML (#10450)

Fixes: FRMW-2827
This commit is contained in:
Harminder Virk
2024-12-05 22:07:54 +05:30
committed by GitHub
parent 559fc6587a
commit 70d77ea22f
5 changed files with 115 additions and 117 deletions

View File

@@ -1,94 +1,26 @@
import {
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import { model } from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Enum,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
const TypeIndex = createPsqlIndexStatementHelper({
tableName: "api_key",
columns: "type",
})
const TokenIndex = createPsqlIndexStatementHelper({
tableName: "api_key",
columns: "token",
unique: true,
})
@Entity()
export default class ApiKey {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
@TokenIndex.MikroORMIndex()
token: string
@Property({ columnType: "text" })
salt: string
@Searchable()
@Property({ columnType: "text" })
redacted: string
@Searchable()
@Property({ columnType: "text" })
title: string
@Property({ columnType: "text" })
@Enum({ items: ["publishable", "secret"] })
@TypeIndex.MikroORMIndex()
type: "publishable" | "secret"
@Property({
columnType: "timestamptz",
nullable: true,
const ApiKey = model
.define("ApiKey", {
id: model.id({ prefix: "apk" }).primaryKey(),
token: model.text(),
salt: model.text(),
redacted: model.text().searchable(),
title: model.text().searchable(),
type: model.enum(["publishable", "secret"]),
last_used_at: model.dateTime().nullable(),
created_by: model.text(),
revoked_by: model.text().nullable(),
revoked_at: model.dateTime().nullable(),
})
last_used_at: Date | null = null
.indexes([
{
on: ["token"],
unique: true,
},
{
on: ["type"],
},
])
@Property({ columnType: "text" })
created_by: string
@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
@Property({ columnType: "text", nullable: true })
revoked_by: string | null = null
@Property({
columnType: "timestamptz",
nullable: true,
})
revoked_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "apk")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "apk")
}
}
export default ApiKey