From 8c420f42854ebbef77c5009265cdb0d0f642c1f4 Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Wed, 13 Apr 2022 08:16:38 +0200 Subject: [PATCH] feat: Add BatchJob entity (#1324) * added entity * better typing * make requested changes * add migration --- .../1649775522087-add_batch_job_model.ts | 19 ++++ packages/medusa/src/models/batch-job.ts | 95 +++++++++++++++++++ packages/medusa/src/types/batch-job.ts | 6 ++ 3 files changed, 120 insertions(+) create mode 100644 packages/medusa/src/migrations/1649775522087-add_batch_job_model.ts create mode 100644 packages/medusa/src/models/batch-job.ts create mode 100644 packages/medusa/src/types/batch-job.ts diff --git a/packages/medusa/src/migrations/1649775522087-add_batch_job_model.ts b/packages/medusa/src/migrations/1649775522087-add_batch_job_model.ts new file mode 100644 index 0000000000..9ffd8588dd --- /dev/null +++ b/packages/medusa/src/migrations/1649775522087-add_batch_job_model.ts @@ -0,0 +1,19 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class addBatchJobModel1649775522087 implements MigrationInterface { + name = "addBatchJobModel1649775522087" + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TYPE "batch_job_status_enum" AS ENUM('created', 'processing', 'awaiting_confirmation', 'completed')` + ) + await queryRunner.query( + `CREATE TABLE "batch_job" ("id" character varying NOT NULL, "type" text NOT NULL, "status" "public"."batch_job_status_enum" NOT NULL, "created_by" text, "context" jsonb, "result" jsonb, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, CONSTRAINT "PK_e57f84d485145d5be96bc6d871e" PRIMARY KEY ("id"))` + ) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE "batch_job"`) + await queryRunner.query(`DROP TYPE "batch_job_status_enum"`) + } +} diff --git a/packages/medusa/src/models/batch-job.ts b/packages/medusa/src/models/batch-job.ts new file mode 100644 index 0000000000..b394dc9e2a --- /dev/null +++ b/packages/medusa/src/models/batch-job.ts @@ -0,0 +1,95 @@ +import { + BeforeInsert, + Column, + CreateDateColumn, + DeleteDateColumn, + Entity, + PrimaryColumn, + UpdateDateColumn, +} from "typeorm" +import { ulid } from "ulid" +import { BatchJobStatus } from "../types/batch-job" +import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column" + +@Entity() +export class BatchJob { + @PrimaryColumn() + id: string + + @DbAwareColumn({ type: "text" }) + type: string + + @DbAwareColumn({ type: "enum", enum: BatchJobStatus }) + status: BatchJobStatus + + @Column({ type: "text", nullable: true }) + created_by: string | null + + @DbAwareColumn({ type: "jsonb", nullable: true }) + context: Record + + @DbAwareColumn({ type: "jsonb", nullable: true }) + result: Record + + @CreateDateColumn({ type: resolveDbType("timestamptz") }) + created_at: Date + + @UpdateDateColumn({ type: resolveDbType("timestamptz") }) + updated_at: Date + + @DeleteDateColumn({ type: resolveDbType("timestamptz") }) + deleted_at: Date | null + + @BeforeInsert() + private beforeInsert() { + if (this.id) return + const id = ulid() + this.id = `batch_${id}` + } +} + +/** + * @schema batch_job + * title: "Batch Job" + * description: "A Batch Job." + * x-resourceId: batch_job + * properties: + * id: + * description: "The unique identifier for the batch job." + * type: string + * type: + * description: "The type of batch job." + * type: string + * enum: + * - product_import + * - product_export + * status: + * description: "The status of the batch job." + * type: string + * enum: + * - created + * - processing + * - awaiting_confirmation + * - completed + * created_by: + * description: "The unique identifier of the user that created the batch job." + * type: string + * context: + * description: "The context of the batch job, the type of the batch job determines what the context should contain." + * type: object + * result: + * description: "The result of the batch job." + * type: object + * created_at: + * description: "The date with timezone at which the resource was created." + * type: string + * format: date-time + * updated_at: + * description: "The date with timezone at which the resource was last updated." + * type: string + * format: date-time + * deleted_at: + * description: "The date with timezone at which the resource was deleted." + * type: string + * format: date-time + */ diff --git a/packages/medusa/src/types/batch-job.ts b/packages/medusa/src/types/batch-job.ts new file mode 100644 index 0000000000..87624fe26e --- /dev/null +++ b/packages/medusa/src/types/batch-job.ts @@ -0,0 +1,6 @@ +export enum BatchJobStatus { + CREATED = "created", + PROCESSING = "processing", + AWAITING_CONFIRMATION = "awaiting_confirmation", + COMPLETED = "completed", +}