feat(fulfillment): Initialize models work (#6328)
**What** Initialize work on the fulfillment module entities. This pr finally also include the indexes as i was working on some utilities i though it would make sense to test them directly. Also this pr add a new utility to generate proper index for our entity properties. It also include a new monkey patch for the migration generator to handle most of if exists/not exists cases. The monkey patch is a workaround the fact that the transpilation does work well with the ECMA used by mikro orm and therefore we end up with some constructor issue when mikro orm try to instanciate the custom generator class extension. **Comment** - The rule part will likely evolved when we reach the point of rule filtering based data, so no need for details review I believe FIXES CORE-1714 FIXES CORE-1715 FIXES CORE-1718 FIXES CORE-1722 FIXES CORE-1723 Current schema diagram 
This commit is contained in:
committed by
GitHub
parent
512ac1e6ed
commit
3fd68d1979
@@ -3,10 +3,10 @@ export * as DecoratorUtils from "./decorators"
|
||||
export * as DefaultsUtils from "./defaults"
|
||||
export * as EventBusUtils from "./event-bus"
|
||||
export * as FeatureFlagUtils from "./feature-flags"
|
||||
export * as FulfillmentUtils from "./fulfillment"
|
||||
export * as ModulesSdkUtils from "./modules-sdk"
|
||||
export * as OrchestrationUtils from "./orchestration"
|
||||
export * as ProductUtils from "./product"
|
||||
export * as PromotionUtils from "./promotion"
|
||||
export * as SearchUtils from "./search"
|
||||
export * as ShippingProfileUtils from "./shipping"
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { createPsqlIndexStatementHelper } from "../create-psql-index-helper"
|
||||
|
||||
describe("createPsqlIndexStatementHelper", function () {
|
||||
it("should generate a simple index", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
tableName: "table_name",
|
||||
columns: "column_name",
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS ${options.name} ON ${options.tableName} (${options.columns})`
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate a composite index", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
tableName: "table_name",
|
||||
columns: ["column_name_1", "column_name_2"],
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS ${options.name} ON ${
|
||||
options.tableName
|
||||
} (${options.columns.join(", ")})`
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate an index with where clauses", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
tableName: "table_name",
|
||||
columns: ["column_name_1", "column_name_2"],
|
||||
where: "column_name_1 IS NOT NULL",
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS ${options.name} ON ${
|
||||
options.tableName
|
||||
} (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate an index with where clauses and index type", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
tableName: "table_name",
|
||||
columns: ["column_name_1", "column_name_2"],
|
||||
type: "GIN",
|
||||
where: "column_name_1 IS NOT NULL",
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS ${options.name} ON ${
|
||||
options.tableName
|
||||
} USING GIN (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||
)
|
||||
})
|
||||
})
|
||||
47
packages/utils/src/common/create-psql-index-helper.ts
Normal file
47
packages/utils/src/common/create-psql-index-helper.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Create a PSQL index statement
|
||||
* @param name The name of the index
|
||||
* @param tableName The name of the table
|
||||
* @param columns The columns to index
|
||||
* @param type The type of index (e.g GIN, GIST, BTREE, etc)
|
||||
* @param where The where clause
|
||||
*
|
||||
* @example
|
||||
* createPsqlIndexStatementHelper({
|
||||
* name: "idx_user_email",
|
||||
* tableName: "user",
|
||||
* columns: "email",
|
||||
* type: "btree",
|
||||
* where: "email IS NOT NULL"
|
||||
* });
|
||||
*
|
||||
* // CREATE INDEX IF NOT EXISTS idx_user_email ON user USING btree (email) WHERE email IS NOT NULL;
|
||||
*
|
||||
* createPsqlIndexStatementHelper({
|
||||
* name: "idx_user_email",
|
||||
* tableName: "user",
|
||||
* columns: "email"
|
||||
* });
|
||||
*
|
||||
* // CREATE INDEX IF NOT EXISTS idx_user_email ON user (email);
|
||||
*
|
||||
*/
|
||||
export function createPsqlIndexStatementHelper({
|
||||
name,
|
||||
tableName,
|
||||
columns,
|
||||
type,
|
||||
where,
|
||||
}: {
|
||||
name: string
|
||||
tableName: string
|
||||
columns: string | string[]
|
||||
type?: string
|
||||
where?: string
|
||||
}) {
|
||||
columns = Array.isArray(columns) ? columns.join(", ") : columns
|
||||
const typeStr = type ? ` USING ${type}` : ""
|
||||
const optionsStr = where ? ` WHERE ${where}` : ""
|
||||
|
||||
return `CREATE INDEX IF NOT EXISTS ${name} ON ${tableName}${typeStr} (${columns})${optionsStr}`
|
||||
}
|
||||
@@ -3,6 +3,7 @@ export * from "./build-query"
|
||||
export * from "./camel-to-snake-case"
|
||||
export * from "./container"
|
||||
export * from "./create-container-like"
|
||||
export * from "./create-psql-index-helper"
|
||||
export * from "./deduplicate"
|
||||
export * from "./deep-equal-obj"
|
||||
export * from "./errors"
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
import { TSMigrationGenerator } from "../mikro-orm-create-connection"
|
||||
|
||||
function unwrapSql(sql: string) {
|
||||
return sql.match(/this.addSql\('(.*?)'\)/)?.[1]
|
||||
}
|
||||
|
||||
describe("TSMigrationGenerator", () => {
|
||||
it('should add "if not exists" to "create table" statements', () => {
|
||||
const sql = "create table my_table (id int)"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe("create table if not exists my_table (id int)")
|
||||
})
|
||||
|
||||
it('should add "if exists" to "alter table" statements as well as to the add column', () => {
|
||||
const sql = "alter table my_table add column name varchar(100)"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe(
|
||||
"alter table if exists my_table add column if not exists name varchar(100)"
|
||||
)
|
||||
})
|
||||
|
||||
it('should add "if exists" to "alter table" statements as well as to the drop column', () => {
|
||||
const sql = "alter table my_table drop column name"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe(
|
||||
"alter table if exists my_table drop column if exists name"
|
||||
)
|
||||
})
|
||||
|
||||
it('should add "if exists" to "alter table" statements as well as to the alter column', () => {
|
||||
const sql = "alter table my_table alter column name"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe(
|
||||
"alter table if exists my_table alter column if exists name"
|
||||
)
|
||||
})
|
||||
|
||||
it('should add "if not exists" to "create index" statements', () => {
|
||||
const sql = "create index idx_name on my_table(name)"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe("create index if not exists idx_name on my_table(name)")
|
||||
})
|
||||
|
||||
it('should add "if exists" to "drop index" statements', () => {
|
||||
const sql = "drop index idx_name"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe("drop index if exists idx_name")
|
||||
})
|
||||
|
||||
it('should add "if not exists" to "create unique index" statements', () => {
|
||||
const sql = "create unique index idx_unique_name on my_table(name)"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe(
|
||||
"create unique index if not exists idx_unique_name on my_table(name)"
|
||||
)
|
||||
})
|
||||
|
||||
it('should add "if exists" to "drop unique index" statements', () => {
|
||||
const sql = "drop unique index idx_unique_name"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe("drop unique index if exists idx_unique_name")
|
||||
})
|
||||
|
||||
it('should add "if not exists" to "add column" statements', () => {
|
||||
const sql = "add column name varchar(100)"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe("add column if not exists name varchar(100)")
|
||||
})
|
||||
|
||||
it('should add "if exists" to "drop column" statements', () => {
|
||||
const sql = "drop column name"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe("drop column if exists name")
|
||||
})
|
||||
|
||||
it('should add "if exists" to "alter column" statements', () => {
|
||||
const sql = "alter column name"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe("alter column if exists name")
|
||||
})
|
||||
|
||||
it('should add "if exists" to "drop constraint" statements', () => {
|
||||
const sql = "drop constraint fk_name"
|
||||
const result = unwrapSql(
|
||||
TSMigrationGenerator.prototype.createStatement(sql, 0)
|
||||
)
|
||||
expect(result).toBe("drop constraint if exists fk_name")
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,71 @@
|
||||
import { ModuleServiceInitializeOptions } from "@medusajs/types"
|
||||
import { TSMigrationGenerator } from "@mikro-orm/migrations"
|
||||
import { isString } from "../../common"
|
||||
|
||||
// Monkey patch due to the compilation version issue which prevents us from creating a proper class that extends the TSMigrationGenerator
|
||||
const originalCreateStatement = TSMigrationGenerator.prototype.createStatement
|
||||
|
||||
/**
|
||||
* Safe migration generation for MikroORM
|
||||
*
|
||||
* @param sql The sql statement
|
||||
* @param padLeft The padding
|
||||
*
|
||||
* @example see test file
|
||||
*/
|
||||
TSMigrationGenerator.prototype.createStatement = function (
|
||||
sql: string,
|
||||
padLeft: number
|
||||
) {
|
||||
if (isString(sql)) {
|
||||
if (!sql.includes("create table if not exists")) {
|
||||
sql = sql.replace("create table", "create table if not exists")
|
||||
}
|
||||
|
||||
if (!sql.includes("alter table if exists")) {
|
||||
sql = sql.replace("alter table", "alter table if exists")
|
||||
}
|
||||
|
||||
if (!sql.includes("create index if not exists")) {
|
||||
sql = sql.replace("create index", "create index if not exists")
|
||||
}
|
||||
|
||||
if (!sql.includes("drop index if exists")) {
|
||||
sql = sql.replace("drop index", "drop index if exists")
|
||||
}
|
||||
|
||||
if (!sql.includes("create unique index if not exists")) {
|
||||
sql = sql.replace(
|
||||
"create unique index",
|
||||
"create unique index if not exists"
|
||||
)
|
||||
}
|
||||
|
||||
if (!sql.includes("drop unique index if exists")) {
|
||||
sql = sql.replace("drop unique index", "drop unique index if exists")
|
||||
}
|
||||
|
||||
if (!sql.includes("add column if not exists")) {
|
||||
sql = sql.replace("add column", "add column if not exists")
|
||||
}
|
||||
|
||||
if (!sql.includes("alter column if exists exists")) {
|
||||
sql = sql.replace("alter column", "alter column if exists")
|
||||
}
|
||||
|
||||
if (!sql.includes("drop column if exists")) {
|
||||
sql = sql.replace("drop column", "drop column if exists")
|
||||
}
|
||||
|
||||
if (!sql.includes("drop constraint if exists")) {
|
||||
sql = sql.replace("drop constraint", "drop constraint if exists")
|
||||
}
|
||||
}
|
||||
|
||||
return originalCreateStatement(sql, padLeft)
|
||||
}
|
||||
|
||||
export { TSMigrationGenerator }
|
||||
|
||||
export async function mikroOrmCreateConnection(
|
||||
database: ModuleServiceInitializeOptions["database"] & { connection?: any },
|
||||
@@ -35,6 +102,7 @@ export async function mikroOrmCreateConnection(
|
||||
type: "postgresql",
|
||||
migrations: {
|
||||
path: pathToMigrations,
|
||||
generator: TSMigrationGenerator,
|
||||
},
|
||||
pool: database.pool as any,
|
||||
})
|
||||
|
||||
6
packages/utils/src/fulfillment/geo-zone.ts
Normal file
6
packages/utils/src/fulfillment/geo-zone.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export enum GeoZoneType {
|
||||
COUNTRY = "country",
|
||||
PROVINCE = "province",
|
||||
CITY = "city",
|
||||
ZIP = "zip",
|
||||
}
|
||||
2
packages/utils/src/fulfillment/index.ts
Normal file
2
packages/utils/src/fulfillment/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./geo-zone"
|
||||
export * from "./shipping-options"
|
||||
4
packages/utils/src/fulfillment/shipping-options.ts
Normal file
4
packages/utils/src/fulfillment/shipping-options.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export enum ShippingOptionPriceType {
|
||||
CALCULATED = "calculated",
|
||||
FLAT = "flat",
|
||||
}
|
||||
@@ -7,6 +7,7 @@ export * from "./defaults"
|
||||
export * from "./event-bus"
|
||||
export * from "./exceptions"
|
||||
export * from "./feature-flags"
|
||||
export * from "./fulfillment"
|
||||
export * from "./modules-sdk"
|
||||
export * from "./orchestration"
|
||||
export * from "./payment"
|
||||
|
||||
Reference in New Issue
Block a user