forked from medusajs/medusa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate api key module to DML (medusajs#10450)
Fixes: FRMW-2827
- Loading branch information
1 parent
559fc65
commit 70d77ea
Showing
5 changed files
with
116 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@medusajs/api-key": patch | ||
--- | ||
|
||
refactor: migrate api key module to DML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/modules/api-key/src/migrations/Migration20241205122700.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Migration } from "@mikro-orm/migrations" | ||
|
||
export class Migration20241205122700 extends Migration { | ||
async up(): Promise<void> { | ||
this.addSql( | ||
'alter table if exists "api_key" add column if not exists "deleted_at" timestamptz null;' | ||
) | ||
this.addSql( | ||
'alter table if exists "api_key" alter column "type" type text using ("type"::text);' | ||
) | ||
this.addSql( | ||
'alter table if exists "api_key" add constraint "api_key_type_check" check ("type" in (\'publishable\', \'secret\'));' | ||
) | ||
this.addSql( | ||
'CREATE INDEX IF NOT EXISTS "IDX_api_key_deleted_at" ON "api_key" (deleted_at) WHERE deleted_at IS NULL;' | ||
) | ||
} | ||
|
||
async down(): Promise<void> { | ||
this.addSql( | ||
'alter table if exists "api_key" drop constraint if exists "api_key_type_check";' | ||
) | ||
|
||
this.addSql( | ||
'alter table if exists "api_key" alter column "type" type text using ("type"::text);' | ||
) | ||
this.addSql('drop index if exists "IDX_api_key_deleted_at";') | ||
this.addSql( | ||
'alter table if exists "api_key" drop column if exists "deleted_at";' | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,26 @@ | ||
import { | ||
Searchable, | ||
createPsqlIndexStatementHelper, | ||
generateEntityId, | ||
} 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, | ||
import { model } from "@medusajs/framework/utils" | ||
|
||
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 | ||
|
||
@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") | ||
} | ||
} | ||
.indexes([ | ||
{ | ||
on: ["token"], | ||
unique: true, | ||
}, | ||
{ | ||
on: ["type"], | ||
}, | ||
]) | ||
|
||
export default ApiKey |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters