feat(medusa): Continue create product workflow changes (#4473)

This commit is contained in:
Adrien de Peretti
2023-07-24 13:30:24 +02:00
committed by GitHub
parent edf93d972d
commit d2a8cf0378
103 changed files with 1859 additions and 524 deletions
@@ -845,8 +845,8 @@
"nullable": false,
"mappedType": "text"
},
"product_image_id": {
"name": "product_image_id",
"image_id": {
"name": "image_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
@@ -860,7 +860,10 @@
"indexes": [
{
"keyName": "product_images_pkey",
"columnNames": ["product_id", "product_image_id"],
"columnNames": [
"product_id",
"image_id"
],
"composite": true,
"primary": true,
"unique": true
@@ -877,9 +880,11 @@
"deleteRule": "cascade",
"updateRule": "cascade"
},
"product_images_product_image_id_foreign": {
"constraintName": "product_images_product_image_id_foreign",
"columnNames": ["product_image_id"],
"product_images_image_id_foreign": {
"constraintName": "product_images_image_id_foreign",
"columnNames": [
"image_id"
],
"localTableName": "public.product_images",
"referencedColumnNames": ["id"],
"referencedTableName": "public.image",
@@ -1119,6 +1124,15 @@
"default": "0",
"mappedType": "decimal"
},
"product_id": {
"name": "product_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
@@ -1148,15 +1162,6 @@
"nullable": true,
"length": 6,
"mappedType": "datetime"
},
"product_id": {
"name": "product_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
}
},
"name": "product_variant",
@@ -1,6 +1,6 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20230710091208 extends Migration {
export class Migration20230719100648 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table "product_category" ("id" text not null, "name" text not null, "description" text not null default \'\', "handle" text not null, "mpath" text not null, "is_active" boolean not null default false, "is_internal" boolean not null default false, "rank" numeric not null default 0, "parent_category_id" text null, "created_at" timestamptz not null, "updated_at" timestamptz not null, constraint "product_category_pkey" primary key ("id"));'
@@ -70,7 +70,7 @@ export class Migration20230710091208 extends Migration {
)
this.addSql(
'create table "product_images" ("product_id" text not null, "product_image_id" text not null, constraint "product_images_pkey" primary key ("product_id", "product_image_id"));'
'create table "product_images" ("product_id" text not null, "image_id" text not null, constraint "product_images_pkey" primary key ("product_id", "image_id"));'
)
this.addSql(
@@ -78,7 +78,7 @@ export class Migration20230710091208 extends Migration {
)
this.addSql(
'create table "product_variant" ("id" text not null, "title" text not null, "sku" text null, "barcode" text null, "ean" text null, "upc" text null, "inventory_quantity" numeric not null default 100, "allow_backorder" boolean not null default false, "manage_inventory" boolean not null default true, "hs_code" text null, "origin_country" text null, "mid_code" text null, "material" text null, "weight" numeric null, "length" numeric null, "height" numeric null, "width" numeric null, "metadata" jsonb null, "variant_rank" numeric null default 0, "created_at" timestamptz not null, "updated_at" timestamptz not null, "deleted_at" timestamptz null, "product_id" text not null, constraint "product_variant_pkey" primary key ("id"));'
'create table "product_variant" ("id" text not null, "title" text not null, "sku" text null, "barcode" text null, "ean" text null, "upc" text null, "inventory_quantity" numeric not null default 100, "allow_backorder" boolean not null default false, "manage_inventory" boolean not null default true, "hs_code" text null, "origin_country" text null, "mid_code" text null, "material" text null, "weight" numeric null, "length" numeric null, "height" numeric null, "width" numeric null, "metadata" jsonb null, "variant_rank" numeric null default 0, "product_id" text not null, "created_at" timestamptz not null, "updated_at" timestamptz not null, "deleted_at" timestamptz null, constraint "product_variant_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_product_variant_deleted_at" on "product_variant" ("deleted_at");'
@@ -138,7 +138,7 @@ export class Migration20230710091208 extends Migration {
'alter table "product_images" add constraint "product_images_product_id_foreign" foreign key ("product_id") references "product" ("id") on update cascade on delete cascade;'
)
this.addSql(
'alter table "product_images" add constraint "product_images_product_image_id_foreign" foreign key ("product_image_id") references "image" ("id") on update cascade on delete cascade;'
'alter table "product_images" add constraint "product_images_image_id_foreign" foreign key ("image_id") references "image" ("id") on update cascade on delete cascade;'
)
this.addSql(
@@ -32,7 +32,7 @@ class ProductOptionValue {
@Property({ columnType: "text" })
value: string
@Property({ persist: false })
@Property({ columnType: "text", nullable: true })
option_id!: string
@ManyToOne(() => ProductOption, {
@@ -41,7 +41,7 @@ class ProductOptionValue {
})
option: ProductOption
@Property({ persist: false })
@Property({ columnType: "text", nullable: true })
variant_id!: string
@ManyToOne(() => ProductVariant, {
@@ -29,7 +29,7 @@ class ProductOption {
@Property({ columnType: "text" })
title: string
@Property({ persist: false })
@Property({ columnType: "text", nullable: true })
product_id!: string
@ManyToOne(() => Product, {
@@ -105,7 +105,7 @@ class ProductVariant {
@Property({ columnType: "numeric", nullable: true, default: 0 })
variant_rank?: number | null
@Property({ persist: false })
@Property({ columnType: "text", nullable: true })
product_id!: string
@Property({ onCreate: () => new Date(), columnType: "timestamptz" })
+4 -2
View File
@@ -100,7 +100,7 @@ class Product {
@Property({ columnType: "text", nullable: true })
material?: string | null
@Property({ persist: false })
@Property({ columnType: "text", nullable: true })
collection_id!: string
@ManyToOne(() => ProductCollection, {
@@ -109,7 +109,7 @@ class Product {
})
collection!: ProductCollection
@Property({ persist: false })
@Property({ columnType: "text", nullable: true })
type_id!: string
@ManyToOne(() => ProductType, {
@@ -132,6 +132,8 @@ class Product {
pivotTable: "product_images",
index: "IDX_product_image_id",
cascade: ["soft-remove"] as any,
joinColumn: "product_id",
inverseJoinColumn: "image_id",
})
images = new Collection<ProductImage>(this)
@@ -1,5 +1,3 @@
import * as ProductModels from "@models"
import { ModuleExports } from "@medusajs/types"
import { ProductModuleService } from "@services"
import loadConnection from "./loaders/connection"
@@ -7,10 +5,8 @@ import loadContainer from "./loaders/container"
const service = ProductModuleService
const loaders = [loadContainer, loadConnection] as any
const models = Object.values(ProductModels)
export const moduleDefinition: ModuleExports = {
service,
loaders,
models,
}
+6 -18
View File
@@ -6,7 +6,6 @@ import {
MedusaContext,
} from "@medusajs/utils"
import { serialize } from "@mikro-orm/core"
import { doNotForceTransaction } from "../utils"
// TODO: Should we create a mikro orm specific package for this and the soft deletable decorator util?
@@ -28,28 +27,17 @@ async function transactionWrapper(
return await task(transaction)
}
const forkedManager = this.manager_.fork()
const options = {}
if (transaction) {
Object.assign(options, { ctx: transaction })
}
if (isolationLevel) {
Object.assign(options, { isolationLevel })
}
if (transaction) {
Object.assign(options, { ctx: transaction })
await forkedManager.begin(options)
} else {
await forkedManager.begin(options)
}
try {
const result = await task(forkedManager)
await forkedManager.commit()
return result
} catch (e) {
await forkedManager.rollback()
throw e
}
return await (this.manager_ as SqlEntityManager).transactional(task, options)
}
const updateDeletedAtRecursively = async <T extends object = any>(
@@ -126,7 +126,6 @@ export class ProductRepository extends AbstractBaseRepository<Product> {
@MedusaContext()
{ transactionManager: manager }: Context = {}
): Promise<Product[]> {
console.log((this as any).prototype)
const products = data.map((product) => {
return (manager as SqlEntityManager).create(Product, product)
})
@@ -349,12 +349,12 @@ export default class ProductModuleService<
}
if (isDefined(productData.type)) {
productData.type_id = (
productData.type = (
await this.productTypeService_.upsert(
[productData.type as ProductTypes.CreateProductTypeDTO],
sharedContext
)
)?.[0]!.id
)?.[0]!
}
return productData as CreateProductOnlyDTO