feat: Add basic endpoints and workflows for Store module (#6515)

This commit is contained in:
Stevche Radevski
2024-02-28 10:08:11 +00:00
committed by GitHub
parent 70aeb602c9
commit d60f3adc03
26 changed files with 596 additions and 13 deletions
@@ -22,6 +22,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "'Medusa Store'",
"mappedType": "text"
},
"default_sales_channel_id": {
@@ -70,11 +71,42 @@
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "store",
"schema": "public",
"indexes": [
{
"keyName": "IDX_store_deleted_at",
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_store_deleted_at\" ON \"store\" (deleted_at) WHERE deleted_at IS NOT NULL"
},
{
"keyName": "store_pkey",
"columnNames": [
@@ -1,9 +0,0 @@
import { Migration } from "@mikro-orm/migrations"
export class InitialSetup20240226130829 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table if not exists "store" ("id" text not null, "name" text not null, "default_sales_channel_id" text null, "default_region_id" text null, "default_location_id" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), constraint "store_pkey" primary key ("id"));'
)
}
}
@@ -0,0 +1,48 @@
import { Migration } from "@mikro-orm/migrations"
export class InitialSetup20240226130829 extends Migration {
async up(): Promise<void> {
// TODO: The migration needs to take care of moving data before dropping columns, among other things
const storeTables = await this.execute(
"select * from information_schema.tables where table_name = 'store' and table_schema = 'public'"
)
if (storeTables.length > 0) {
this.addSql(`alter table "store" alter column "id" TYPE text;`)
this.addSql(`alter table "store" alter column "name" TYPE text;`)
this.addSql(
`alter table "store" alter column "name" SET DEFAULT 'Medusa Store';`
)
this.addSql(
`alter table "store" alter column "default_sales_channel_id" TYPE text;`
)
this.addSql(
`alter table "store" alter column "default_location_id" TYPE text;`
)
this.addSql(`alter table "store" add column "default_region_id" text;`)
this.addSql(
`alter table "store" add column "deleted_at" timestamptz null;`
)
this.addSql(
'create index if not exists "IDX_store_deleted_at" on "store" (deleted_at) where deleted_at is not null;'
)
// this.addSql(`alter table "store" drop column "default_currency_code";`)
// this.addSql(`alter table "store" drop column "swap_link_template";`)
// this.addSql(`alter table "store" drop column "payment_link_template";`)
// this.addSql(`alter table "store" drop column "invite_link_template";`)
} else {
this.addSql(`create table if not exists "store"
("id" text not null, "name" text not null default \'Medusa Store\',
"default_sales_channel_id" text null, "default_region_id" text null, "default_location_id" text null,
"metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null,
constraint "store_pkey" primary key ("id"));`)
this.addSql(
'create index if not exists "IDX_store_deleted_at" on "store" (deleted_at) where deleted_at is not null;'
)
}
}
}
+33 -2
View File
@@ -1,4 +1,10 @@
import { generateEntityId } from "@medusajs/utils"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import { DAL } from "@medusajs/types"
import {
BeforeCreate,
@@ -6,14 +12,27 @@ import {
OnInit,
PrimaryKey,
Property,
Filter,
OptionalProps,
} from "@mikro-orm/core"
type StoreOptionalProps = DAL.SoftDeletableEntityDateColumns
const StoreDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "store",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Store {
[OptionalProps]?: StoreOptionalProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
@Property({ columnType: "text", default: "Medusa Store" })
name: string
@Property({ columnType: "text", nullable: true })
@@ -35,6 +54,18 @@ export default class Store {
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@StoreDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "store")