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
![fulfillment-models](https://github.com/medusajs/medusa/assets/25098370/0d7900ad-2cdc-4879-9f49-90d1577eb516)
This commit is contained in:
Adrien de Peretti
2024-02-08 17:39:05 +01:00
committed by GitHub
parent 512ac1e6ed
commit 3fd68d1979
26 changed files with 1618 additions and 3 deletions

View File

@@ -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}`
)
})
})

View 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}`
}

View File

@@ -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"